From 60c85d997096f60bb8dd078033e583e54efffe03 Mon Sep 17 00:00:00 2001 From: brg468 Date: Tue, 28 Apr 2020 16:24:52 -0400 Subject: [PATCH 1/2] Update binary sensor --- .../components/rachio/binary_sensor.py | 32 +++---------------- homeassistant/components/rachio/const.py | 1 - 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/rachio/binary_sensor.py b/homeassistant/components/rachio/binary_sensor.py index 4976714f0a2d0..b9707c44e4286 100644 --- a/homeassistant/components/rachio/binary_sensor.py +++ b/homeassistant/components/rachio/binary_sensor.py @@ -15,7 +15,6 @@ KEY_STATUS, KEY_SUBTYPE, SIGNAL_RACHIO_CONTROLLER_UPDATE, - STATUS_OFFLINE, STATUS_ONLINE, ) from .entity import RachioDevice @@ -41,13 +40,10 @@ def _create_entities(hass, config_entry): class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity): """Represent a binary sensor that reflects a Rachio state.""" - def __init__(self, controller, poll=True): + def __init__(self, controller): """Set up a new Rachio controller binary sensor.""" super().__init__(controller) - if poll: - self._state = self._poll_update() - else: - self._state = None + self._state = None @property def is_on(self) -> bool: @@ -64,16 +60,14 @@ def _async_handle_any_update(self, *args, **kwargs) -> None: # For this device self._async_handle_update(args, kwargs) - @abstractmethod - def _poll_update(self, data=None) -> bool: - """Request the state from the API.""" - @abstractmethod 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._state = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE + self.async_on_remove( async_dispatcher_connect( self.hass, @@ -86,11 +80,6 @@ async def async_added_to_hass(self): class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor): """Represent a binary sensor that reflects if the controller is online.""" - def __init__(self, controller): - """Set up a new Rachio controller online binary sensor.""" - super().__init__(controller, poll=False) - self._state = self._poll_update(controller.init_data) - @property def name(self) -> str: """Return the name of this sensor including the controller name.""" @@ -111,19 +100,6 @@ def icon(self) -> str: """Return the name of an icon for this sensor.""" return "mdi:wifi-strength-4" if self.is_on else "mdi:wifi-strength-off-outline" - def _poll_update(self, data=None) -> bool: - """Request the state from the API.""" - if data is None: - data = self._controller.rachio.device.get(self._controller.controller_id)[1] - - if data[KEY_STATUS] == STATUS_ONLINE: - return True - if data[KEY_STATUS] == STATUS_OFFLINE: - return False - _LOGGER.warning( - '"%s" reported in unknown state "%s"', self.name, data[KEY_STATUS] - ) - @callback def _async_handle_update(self, *args, **kwargs) -> None: """Handle an update to the state of this sensor.""" diff --git a/homeassistant/components/rachio/const.py b/homeassistant/components/rachio/const.py index 99e26f6383566..016218906f4b3 100644 --- a/homeassistant/components/rachio/const.py +++ b/homeassistant/components/rachio/const.py @@ -53,7 +53,6 @@ ) STATUS_ONLINE = "ONLINE" -STATUS_OFFLINE = "OFFLINE" SIGNAL_RACHIO_UPDATE = f"{DOMAIN}_update" SIGNAL_RACHIO_CONTROLLER_UPDATE = f"{SIGNAL_RACHIO_UPDATE}_controller" From ed8aecbfac603b757ff10ef7c831056c4b694667 Mon Sep 17 00:00:00 2001 From: brg468 Date: Wed, 29 Apr 2020 11:08:59 -0400 Subject: [PATCH 2/2] Move online state to subclass --- homeassistant/components/rachio/binary_sensor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/rachio/binary_sensor.py b/homeassistant/components/rachio/binary_sensor.py index b9707c44e4286..49f46578f76c3 100644 --- a/homeassistant/components/rachio/binary_sensor.py +++ b/homeassistant/components/rachio/binary_sensor.py @@ -66,8 +66,6 @@ def _async_handle_update(self, *args, **kwargs) -> None: async def async_added_to_hass(self): """Subscribe to updates.""" - self._state = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE - self.async_on_remove( async_dispatcher_connect( self.hass, @@ -100,6 +98,11 @@ def icon(self) -> str: """Return the name of an icon for this sensor.""" return "mdi:wifi-strength-4" if self.is_on else "mdi:wifi-strength-off-outline" + async def async_added_to_hass(self): + """Get initial state.""" + self._state = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE + await super().async_added_to_hass() + @callback def _async_handle_update(self, *args, **kwargs) -> None: """Handle an update to the state of this sensor."""