Skip to content

Commit

Permalink
Rachio Async fixes (home-assistant#33549)
Browse files Browse the repository at this point in the history
* Async fix

* Update homeassistant/components/rachio/switch.py

Co-Authored-By: J. Nick Koston <[email protected]>

* Update homeassistant/components/rachio/switch.py

Co-Authored-By: J. Nick Koston <[email protected]>

* Fix format

* Remove from hass

* undo dispatcher

Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
brg468 and bdraco authored Apr 3, 2020
1 parent 081b822 commit 2065039
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions homeassistant/components/rachio/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ def is_on(self) -> bool:
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."""


Expand Down Expand Up @@ -149,14 +150,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."""
Expand All @@ -170,7 +172,9 @@ 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
self.hass,
SIGNAL_RACHIO_CONTROLLER_UPDATE,
self._async_handle_any_update,
)
)

Expand All @@ -192,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."""
Expand Down Expand Up @@ -229,7 +232,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:
Expand Down Expand Up @@ -266,7 +269,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
Expand All @@ -278,19 +282,16 @@ 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.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."""
Expand All @@ -305,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:
Expand Down Expand Up @@ -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:
Expand All @@ -369,15 +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.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()

0 comments on commit 2065039

Please sign in to comment.