Rachio Async fixes (#33549)
* Async fix * Update homeassistant/components/rachio/switch.py Co-Authored-By: J. Nick Koston <nick@koston.org> * Update homeassistant/components/rachio/switch.py Co-Authored-By: J. Nick Koston <nick@koston.org> * Fix format * Remove from hass * undo dispatcher Co-authored-by: J. Nick Koston <nick@koston.org>pull/33585/head
parent
081b822d25
commit
2065039f65
|
@ -105,17 +105,18 @@ class RachioSwitch(RachioDevice, SwitchDevice):
|
|||
def _poll_update(self, data=None) -> bool:
|
||||
"""Poll the API."""
|
||||
|
||||
def _handle_any_update(self, *args, **kwargs) -> None:
|
||||
@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:
|
||||
# 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:
|
||||
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||
"""Handle incoming webhook data."""
|
||||
|
||||
|
||||
|
@ -149,14 +150,15 @@ class RachioStandbySwitch(RachioSwitch):
|
|||
|
||||
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."""
|
||||
|
@ -170,7 +172,9 @@ class RachioStandbySwitch(RachioSwitch):
|
|||
"""Subscribe to updates."""
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update
|
||||
self.hass,
|
||||
SIGNAL_RACHIO_CONTROLLER_UPDATE,
|
||||
self._async_handle_any_update,
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -192,7 +196,6 @@ class RachioZone(RachioSwitch):
|
|||
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."""
|
||||
|
@ -229,7 +232,7 @@ class RachioZone(RachioSwitch):
|
|||
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:
|
||||
|
@ -266,7 +269,8 @@ class RachioZone(RachioSwitch):
|
|||
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,19 +282,16 @@ class RachioZone(RachioSwitch):
|
|||
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.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update
|
||||
)
|
||||
)
|
||||
|
||||
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."""
|
||||
|
@ -305,7 +306,6 @@ class RachioSchedule(RachioSwitch):
|
|||
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:
|
||||
|
@ -354,7 +354,7 @@ class RachioSchedule(RachioSwitch):
|
|||
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,15 +369,12 @@ class RachioSchedule(RachioSwitch):
|
|||
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.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue