diff --git a/custom_components/mail_and_packages/binary_sensor.py b/custom_components/mail_and_packages/binary_sensor.py index 3950616e..3188d6a9 100644 --- a/custom_components/mail_and_packages/binary_sensor.py +++ b/custom_components/mail_and_packages/binary_sensor.py @@ -18,6 +18,7 @@ from .const import ( ATTR_AMAZON_IMAGE, ATTR_IMAGE_NAME, + ATTR_IMAGE_PATH, BINARY_SENSORS, COORDINATOR, DOMAIN, @@ -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) @@ -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) diff --git a/tests/const.py b/tests/const.py index 15c9cdb4..46f30083 100644 --- a/tests/const.py +++ b/tests/const.py @@ -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, diff --git a/tests/test_binary_sensor.py b/tests/test_binary_sensor.py index 1e242aa5..ea9e3a54 100644 --- a/tests/test_binary_sensor.py +++ b/tests/test_binary_sensor.py @@ -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" \ No newline at end of file