From 9b301f11372017eca8149acebfea1059d051c29e Mon Sep 17 00:00:00 2001 From: brg468 Date: Thu, 2 Apr 2020 15:29:43 -0400 Subject: [PATCH 1/6] Async fix --- homeassistant/components/rachio/switch.py | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index 86b6097ad13f5a..2d52f87ce1b0ba 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -105,17 +105,17 @@ def is_on(self) -> bool: def _poll_update(self, data=None) -> bool: """Poll the API.""" - def _handle_any_update(self, *args, **kwargs) -> None: + def _async_handle_any_update(self, *args, **kwargs) -> None: """Determine whether an update event applies to this device.""" if args[0][KEY_DEVICE_ID] != self._controller.controller_id: # For another device return # For this device - self._handle_update(args, kwargs) + self._async_handle_update(args, kwargs) - @abstractmethod - def _handle_update(self, *args, **kwargs) -> None: + @callback + def _async_handle_update(self, *args, **kwargs) -> None: """Handle incoming webhook data.""" @@ -149,14 +149,15 @@ def _poll_update(self, data=None) -> bool: return not data[KEY_ON] - def _handle_update(self, *args, **kwargs) -> None: + @callback + def _async_handle_update(self, *args, **kwargs) -> None: """Update the state using webhook data.""" if args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_ON: self._state = True elif args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_OFF: self._state = False - self.schedule_update_ha_state() + self.async_write_ha_state() def turn_on(self, **kwargs) -> None: """Put the controller in standby mode.""" @@ -168,10 +169,8 @@ def turn_off(self, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - self.async_on_remove( - async_dispatcher_connect( - self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update - ) + async_dispatcher_connect( + self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._async_handle_any_update ) @@ -266,7 +265,8 @@ def _poll_update(self, data=None) -> bool: self._current_schedule = self._controller.current_schedule return self.zone_id == self._current_schedule.get(KEY_ZONE_ID) - def _handle_update(self, *args, **kwargs) -> None: + @callback + def _async_handle_update(self, *args, **kwargs) -> None: """Handle incoming webhook zone data.""" if args[0][KEY_ZONE_ID] != self.zone_id: return @@ -278,12 +278,12 @@ def _handle_update(self, *args, **kwargs) -> None: elif args[0][KEY_SUBTYPE] in [SUBTYPE_ZONE_STOPPED, SUBTYPE_ZONE_COMPLETED]: self._state = False - self.schedule_update_ha_state() + self.async_write_ha_state() async def async_added_to_hass(self): """Subscribe to updates.""" self._undo_dispatcher = async_dispatcher_connect( - self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._handle_update + self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update ) async def async_will_remove_from_hass(self): @@ -354,7 +354,7 @@ def _poll_update(self, data=None) -> bool: return self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID) @callback - async def _handle_update(self, *args, **kwargs) -> None: + def _async_handle_update(self, *args, **kwargs) -> None: """Handle incoming webhook schedule data.""" # Schedule ID not passed when running individual zones, so we catch that error try: @@ -369,12 +369,12 @@ async def _handle_update(self, *args, **kwargs) -> None: except KeyError: pass - self.schedule_update_ha_state() + self.async_write_ha_state() async def async_added_to_hass(self): """Subscribe to updates.""" self._undo_dispatcher = async_dispatcher_connect( - self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._handle_update + self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update ) async def async_will_remove_from_hass(self): From 8f691ac1478a7d788c7fae98ac5766ef5030a88a Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Thu, 2 Apr 2020 18:52:36 -0400 Subject: [PATCH 2/6] Update homeassistant/components/rachio/switch.py Co-Authored-By: J. Nick Koston --- homeassistant/components/rachio/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index 2d52f87ce1b0ba..eebf1fb414f4a0 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -282,7 +282,7 @@ def _async_handle_update(self, *args, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - self._undo_dispatcher = async_dispatcher_connect( + self.async_on_remove(async_dispatcher_connect(self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update)) self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update ) From 2bc6499b21c101f9f4e368441482288b6a034fd0 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Thu, 2 Apr 2020 18:52:44 -0400 Subject: [PATCH 3/6] Update homeassistant/components/rachio/switch.py Co-Authored-By: J. Nick Koston --- homeassistant/components/rachio/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index eebf1fb414f4a0..8dd8031b9763ae 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -373,7 +373,7 @@ def _async_handle_update(self, *args, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - self._undo_dispatcher = async_dispatcher_connect( + self.async_on_remove(async_dispatcher_connect(self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update)) self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update ) From 92d6b7d935875b93b1dbd4ce3785d70600f1cbdb Mon Sep 17 00:00:00 2001 From: brg468 Date: Thu, 2 Apr 2020 20:43:48 -0400 Subject: [PATCH 4/6] Fix format --- homeassistant/components/rachio/switch.py | 25 +++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index 8dd8031b9763ae..039f2f7e8046f2 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -105,6 +105,7 @@ def is_on(self) -> bool: def _poll_update(self, data=None) -> bool: """Poll the API.""" + @callback def _async_handle_any_update(self, *args, **kwargs) -> None: """Determine whether an update event applies to this device.""" if args[0][KEY_DEVICE_ID] != self._controller.controller_id: @@ -114,7 +115,7 @@ def _async_handle_any_update(self, *args, **kwargs) -> None: # For this device self._async_handle_update(args, kwargs) - @callback + @abstractmethod def _async_handle_update(self, *args, **kwargs) -> None: """Handle incoming webhook data.""" @@ -169,8 +170,12 @@ def turn_off(self, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - async_dispatcher_connect( - self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._async_handle_any_update + self.async_on_remove( + async_dispatcher_connect( + self.hass, + SIGNAL_RACHIO_CONTROLLER_UPDATE, + self._async_handle_any_update, + ) ) @@ -228,7 +233,7 @@ def entity_picture(self): return self._entity_picture @property - def state_attributes(self) -> dict: + def device_state_attributes(self) -> dict: """Return the optional state attributes.""" props = {ATTR_ZONE_NUMBER: self._zone_number, ATTR_ZONE_SUMMARY: self._summary} if self._shade_type: @@ -282,8 +287,10 @@ def _async_handle_update(self, *args, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - self.async_on_remove(async_dispatcher_connect(self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update)) - self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update + self.async_on_remove( + async_dispatcher_connect( + self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update + ) ) async def async_will_remove_from_hass(self): @@ -373,8 +380,10 @@ def _async_handle_update(self, *args, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - self.async_on_remove(async_dispatcher_connect(self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update)) - self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update + self.async_on_remove( + async_dispatcher_connect( + self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update + ) ) async def async_will_remove_from_hass(self): From f0b40e6957dd6d7432000086ae351f5bde7b5bbc Mon Sep 17 00:00:00 2001 From: brg468 Date: Thu, 2 Apr 2020 21:40:06 -0400 Subject: [PATCH 5/6] Remove from hass --- homeassistant/components/rachio/switch.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index 039f2f7e8046f2..526b14dc75c164 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -293,11 +293,6 @@ async def async_added_to_hass(self): ) ) - async def async_will_remove_from_hass(self): - """Unsubscribe from updates.""" - if self._undo_dispatcher: - self._undo_dispatcher() - class RachioSchedule(RachioSwitch): """Representation of one fixed schedule on the Rachio Iro.""" @@ -385,8 +380,3 @@ async def async_added_to_hass(self): self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update ) ) - - async def async_will_remove_from_hass(self): - """Unsubscribe from updates.""" - if self._undo_dispatcher: - self._undo_dispatcher() From 2ca0689ad183fcd2888dda48f095d5119848a87b Mon Sep 17 00:00:00 2001 From: brg468 Date: Thu, 2 Apr 2020 21:54:36 -0400 Subject: [PATCH 6/6] undo dispatcher --- homeassistant/components/rachio/switch.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index 526b14dc75c164..2b9a959deb2c9e 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -196,7 +196,6 @@ def __init__(self, person, controller, data, current_schedule): self._current_schedule = current_schedule super().__init__(controller, poll=False) self._state = self.zone_id == self._current_schedule.get(KEY_ZONE_ID) - self._undo_dispatcher = None def __str__(self): """Display the zone as a string.""" @@ -307,7 +306,6 @@ def __init__(self, person, controller, data, current_schedule): self._current_schedule = current_schedule super().__init__(controller, poll=False) self._state = self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID) - self._undo_dispatcher = None @property def name(self) -> str: