Skip to content

Commit

Permalink
Fix Rachio binary sensor cold reboot (#33959)
Browse files Browse the repository at this point in the history
  • Loading branch information
brg468 committed Apr 11, 2020
1 parent c8aa554 commit 1db6e9f
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions homeassistant/components/rachio/binary_sensor.py
Expand Up @@ -6,6 +6,7 @@
DEVICE_CLASS_CONNECTIVITY,
BinarySensorDevice,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect

from .const import (
Expand All @@ -18,7 +19,7 @@
STATUS_ONLINE,
)
from .entity import RachioDevice
from .webhooks import SUBTYPE_OFFLINE, SUBTYPE_ONLINE
from .webhooks import SUBTYPE_COLD_REBOOT, SUBTYPE_OFFLINE, SUBTYPE_ONLINE

_LOGGER = logging.getLogger(__name__)

Expand All @@ -43,7 +44,6 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorDevice):
def __init__(self, controller, poll=True):
"""Set up a new Rachio controller binary sensor."""
super().__init__(controller)
self._undo_dispatcher = None
if poll:
self._state = self._poll_update()
else:
Expand All @@ -54,34 +54,34 @@ def is_on(self) -> bool:
"""Return whether the sensor has a 'true' value."""
return self._state

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 _poll_update(self, data=None) -> bool:
"""Request the state from the API."""

@abstractmethod
def _handle_update(self, *args, **kwargs) -> None:
def _async_handle_update(self, *args, **kwargs) -> None:
"""Handle an update to the state of this sensor."""

async def async_added_to_hass(self):
"""Subscribe to updates."""
self._undo_dispatcher = async_dispatcher_connect(
self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update
self.async_on_remove(
async_dispatcher_connect(
self.hass,
SIGNAL_RACHIO_CONTROLLER_UPDATE,
self._async_handle_any_update,
)
)

async def async_will_remove_from_hass(self):
"""Unsubscribe from updates."""
if self._undo_dispatcher:
self._undo_dispatcher()


class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
"""Represent a binary sensor that reflects if the controller is online."""
Expand All @@ -94,7 +94,7 @@ def __init__(self, controller):
@property
def name(self) -> str:
"""Return the name of this sensor including the controller name."""
return f"{self._controller.name} online"
return self._controller.name

@property
def unique_id(self) -> str:
Expand Down Expand Up @@ -124,11 +124,15 @@ def _poll_update(self, data=None) -> bool:
'"%s" reported in unknown state "%s"', self.name, data[KEY_STATUS]
)

def _handle_update(self, *args, **kwargs) -> None:
@callback
def _async_handle_update(self, *args, **kwargs) -> None:
"""Handle an update to the state of this sensor."""
if args[0][0][KEY_SUBTYPE] == SUBTYPE_ONLINE:
if (
args[0][0][KEY_SUBTYPE] == SUBTYPE_ONLINE
or args[0][0][KEY_SUBTYPE] == SUBTYPE_COLD_REBOOT
):
self._state = True
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_OFFLINE:
self._state = False

self.schedule_update_ha_state()
self.async_write_ha_state()

0 comments on commit 1db6e9f

Please sign in to comment.