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,
BinarySensorDevice,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
@ -18,7 +19,7 @@ from .const import (
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__)
@ -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:
@ -54,34 +54,34 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorDevice):
"""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."""
@ -94,7 +94,7 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
@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:
@ -124,11 +124,15 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
'"%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()