From 6f8a66b15511d3890d36ea36189bbf9046f83cfb Mon Sep 17 00:00:00 2001 From: Aaron Godfrey Date: Wed, 27 May 2020 20:43:08 -0700 Subject: [PATCH] Updated sensor entities. This adds a method to allow a manual update and properly subscribes to entities to coordinator updates. --- .../nintendo_wishlist/binary_sensor.py | 14 ++++++++++++++ custom_components/nintendo_wishlist/sensor.py | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/custom_components/nintendo_wishlist/binary_sensor.py b/custom_components/nintendo_wishlist/binary_sensor.py index 053df68..9cc1e6e 100644 --- a/custom_components/nintendo_wishlist/binary_sensor.py +++ b/custom_components/nintendo_wishlist/binary_sensor.py @@ -73,3 +73,17 @@ def name(self) -> str: @property def device_state_attributes(self): return {"matches": self.matches} + + async def async_update(self): + """Update the entity. + + This is only used by the generic entity update service. Normal updates + happen via the coordinator. + """ + await self.coordinator.async_request_refresh() + + async def async_added_to_hass(self): + """Subscribe entity to updates when added to hass.""" + self.async_on_remove( + self.coordinator.async_add_listener(self.async_write_ha_state) + ) diff --git a/custom_components/nintendo_wishlist/sensor.py b/custom_components/nintendo_wishlist/sensor.py index 634a2f1..9b9a0d7 100644 --- a/custom_components/nintendo_wishlist/sensor.py +++ b/custom_components/nintendo_wishlist/sensor.py @@ -55,3 +55,19 @@ def state(self): @property def device_state_attributes(self): return self.attrs + + async def async_update(self): + """Update the entity. + + This is only used by the generic entity update service. Normal updates + happen via the coordinator. + """ + await self.coordinator.async_request_refresh() + + async def async_added_to_hass(self): + """Subscribe entity to updates when added to hass.""" + self.async_on_remove( + self.coordinator.async_add_listener( + self.async_write_ha_state + ) + )