Skip to content

Commit

Permalink
fix: binary sensors not updating (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Jun 17, 2022
2 parents b4f3a5b + df90c17 commit b52b1e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
21 changes: 19 additions & 2 deletions custom_components/mail_and_packages/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .const import (
ATTR_AMAZON_IMAGE,
ATTR_IMAGE_NAME,
ATTR_IMAGE_PATH,
BINARY_SENSORS,
COORDINATOR,
DOMAIN,
Expand Down Expand Up @@ -70,14 +71,27 @@ def device_info(self) -> dict:
"sw_version": VERSION,
}

@property
def should_poll(self) -> bool:
"""No need to poll. Coordinator notifies entity of updates."""
return False

@property
def available(self) -> bool:
"""Return if entity is available."""
return self.coordinator.last_update_success

@property
def is_on(self) -> bool:
"""Return True if the image is updated."""
if self._type == "usps_update":
if ATTR_IMAGE_NAME in self.coordinator.data.keys():
usps_image = self.coordinator.data[ATTR_IMAGE_NAME]
image = self.coordinator.data[ATTR_IMAGE_NAME]
path = self.coordinator.data[ATTR_IMAGE_PATH]
usps_image = f"{self.hass.config.path()}/{path}{image}"
usps_none = f"{os.path.dirname(__file__)}/mail_none.gif"
usps_check = os.path.exists(usps_image)
_LOGGER.debug("USPS Check: %s", usps_check)
if usps_check:
image_hash = hash_file(usps_image)
none_hash = hash_file(usps_none)
Expand All @@ -91,9 +105,12 @@ def is_on(self) -> bool:

if self._type == "amazon_update":
if ATTR_AMAZON_IMAGE in self.coordinator.data.keys():
amazon_image = self.coordinator.data[ATTR_AMAZON_IMAGE]
image = self.coordinator.data[ATTR_IMAGE_NAME]
path = f"{self.coordinator.data[ATTR_IMAGE_PATH]}amazon/"
amazon_image = f"{self.hass.config.path()}/{path}{image}"
amazon_none = f"{os.path.dirname(__file__)}/no_deliveries.jpg"
amazon_check = os.path.exists(amazon_image)
_LOGGER.debug("Amazon Check: %s", amazon_check)
if amazon_check:
image_hash = hash_file(amazon_image)
none_hash = hash_file(amazon_none)
Expand Down
1 change: 1 addition & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@
FAKE_UPDATE_DATA_BIN = {
"amazon_image": "fake_amazon_image.gif",
"image_name": "mail_today.gif",
"image_path": "custom_components/mail_and_packages/images/",
"mail_updated": "2022-01-06T12:14:38+00:00",
"usps_mail": 6,
"usps_delivered": 3,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def hash_side_effect(value):
"""Side effect value."""
if "mail_none.gif" in value:
return "633d7356947eec543c50b76a1852f92427f4dca9"
elif "fake_amazon_image.gif" in value:
elif "no_deliveries.jpg" in value:
return "633d7356947ffc643c50b76a1852f92427f4dca9"
else:
return "133d7356947fec542c50b76b1856f92427f5dca9"

0 comments on commit b52b1e4

Please sign in to comment.