Fix Rachio binary sensor cold reboot (#33959)

This commit is contained in:
Brian Rogers 2020-04-10 20:01:22 -04:00 committed by GitHub
parent c8aa55439f
commit 1db6e9fcb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY, DEVICE_CLASS_CONNECTIVITY,
BinarySensorDevice, BinarySensorDevice,
) )
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import ( from .const import (
@ -18,7 +19,7 @@ from .const import (
STATUS_ONLINE, STATUS_ONLINE,
) )
from .entity import RachioDevice 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__) _LOGGER = logging.getLogger(__name__)
@ -43,7 +44,6 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorDevice):
def __init__(self, controller, poll=True): def __init__(self, controller, poll=True):
"""Set up a new Rachio controller binary sensor.""" """Set up a new Rachio controller binary sensor."""
super().__init__(controller) super().__init__(controller)
self._undo_dispatcher = None
if poll: if poll:
self._state = self._poll_update() self._state = self._poll_update()
else: else:
@ -54,33 +54,33 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorDevice):
"""Return whether the sensor has a 'true' value.""" """Return whether the sensor has a 'true' value."""
return self._state 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.""" """Determine whether an update event applies to this device."""
if args[0][KEY_DEVICE_ID] != self._controller.controller_id: if args[0][KEY_DEVICE_ID] != self._controller.controller_id:
# For another device # For another device
return return
# For this device # For this device
self._handle_update(args, kwargs) self._async_handle_update(args, kwargs)
@abstractmethod @abstractmethod
def _poll_update(self, data=None) -> bool: def _poll_update(self, data=None) -> bool:
"""Request the state from the API.""" """Request the state from the API."""
@abstractmethod @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.""" """Handle an update to the state of this sensor."""
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Subscribe to updates.""" """Subscribe to updates."""
self._undo_dispatcher = async_dispatcher_connect( self.async_on_remove(
self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update 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): class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
@ -94,7 +94,7 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of this sensor including the controller name.""" """Return the name of this sensor including the controller name."""
return f"{self._controller.name} online" return self._controller.name
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
@ -124,11 +124,15 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
'"%s" reported in unknown state "%s"', self.name, data[KEY_STATUS] '"%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.""" """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 self._state = True
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_OFFLINE: elif args[0][0][KEY_SUBTYPE] == SUBTYPE_OFFLINE:
self._state = False self._state = False
self.schedule_update_ha_state() self.async_write_ha_state()