Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: binary sensors not updating #686

Merged
merged 3 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"