From d5e076135ccdb1779cd2a5bd2981882db20b7f5e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 15 Mar 2020 07:52:45 +0000 Subject: [PATCH] Address review items, retest zone updates back and forth, and standby mode --- homeassistant/components/rachio/__init__.py | 7 +++- .../components/rachio/binary_sensor.py | 25 +++++++------ homeassistant/components/rachio/switch.py | 35 +++++++++++-------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/rachio/__init__.py b/homeassistant/components/rachio/__init__.py index 095e5a6864a..7eaa76dedd4 100644 --- a/homeassistant/components/rachio/__init__.py +++ b/homeassistant/components/rachio/__init__.py @@ -348,9 +348,14 @@ class RachioIro: _LOGGER.info("Stopped watering of all zones on %s", str(self)) -class RachioDeviceMixIn(Entity): +class RachioDeviceInfoProvider(Entity): """Mixin to provide device_info.""" + def __init__(self, controller): + """Initialize a Rachio device.""" + super().__init__() + self._controller = controller + @property def device_info(self): """Return the device_info of the device.""" diff --git a/homeassistant/components/rachio/binary_sensor.py b/homeassistant/components/rachio/binary_sensor.py index 82912d3b07a..e457e5c6cf3 100644 --- a/homeassistant/components/rachio/binary_sensor.py +++ b/homeassistant/components/rachio/binary_sensor.py @@ -3,7 +3,7 @@ from abc import abstractmethod import logging from homeassistant.components.binary_sensor import BinarySensorDevice -from homeassistant.helpers.dispatcher import dispatcher_connect +from homeassistant.helpers.dispatcher import async_dispatcher_connect from . import ( SIGNAL_RACHIO_CONTROLLER_UPDATE, @@ -11,7 +11,7 @@ from . import ( STATUS_ONLINE, SUBTYPE_OFFLINE, SUBTYPE_ONLINE, - RachioDeviceMixIn, + RachioDeviceInfoProvider, ) from .const import DOMAIN as DOMAIN_RACHIO, KEY_DEVICE_ID, KEY_STATUS, KEY_SUBTYPE @@ -28,15 +28,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities): def _create_entities(hass, config_entry): entities = [] for controller in hass.data[DOMAIN_RACHIO][config_entry.entry_id].controllers: - entities.append(RachioControllerOnlineBinarySensor(hass, controller)) + entities.append(RachioControllerOnlineBinarySensor(controller)) return entities -class RachioControllerBinarySensor(RachioDeviceMixIn, BinarySensorDevice): +class RachioControllerBinarySensor(RachioDeviceInfoProvider, BinarySensorDevice): """Represent a binary sensor that reflects a Rachio state.""" - def __init__(self, hass, controller, poll=True): + def __init__(self, controller, poll=True): """Set up a new Rachio controller binary sensor.""" + super().__init__(controller) self._controller = controller if poll: @@ -44,10 +45,6 @@ class RachioControllerBinarySensor(RachioDeviceMixIn, BinarySensorDevice): else: self._state = None - dispatcher_connect( - hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update - ) - @property def should_poll(self) -> bool: """Declare that this entity pushes its state to HA.""" @@ -77,13 +74,19 @@ class RachioControllerBinarySensor(RachioDeviceMixIn, BinarySensorDevice): """Handle an update to the state of this sensor.""" pass + async def async_added_to_hass(self): + """Subscribe to updates.""" + async_dispatcher_connect( + self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update + ) + class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor): """Represent a binary sensor that reflects if the controller is online.""" - def __init__(self, hass, controller): + def __init__(self, controller): """Set up a new Rachio controller online binary sensor.""" - super().__init__(hass, controller, poll=False) + super().__init__(controller, poll=False) self._state = self._poll_update(controller.init_data) @property diff --git a/homeassistant/components/rachio/switch.py b/homeassistant/components/rachio/switch.py index c69f7047e35..2311b2668a9 100644 --- a/homeassistant/components/rachio/switch.py +++ b/homeassistant/components/rachio/switch.py @@ -4,7 +4,7 @@ from datetime import timedelta import logging from homeassistant.components.switch import SwitchDevice -from homeassistant.helpers.dispatcher import dispatcher_connect +from homeassistant.helpers.dispatcher import async_dispatcher_connect from . import ( SIGNAL_RACHIO_CONTROLLER_UPDATE, @@ -14,7 +14,7 @@ from . import ( SUBTYPE_ZONE_COMPLETED, SUBTYPE_ZONE_STARTED, SUBTYPE_ZONE_STOPPED, - RachioDeviceMixIn, + RachioDeviceInfoProvider, ) from .const import ( CONF_MANUAL_RUN_MINS, @@ -52,23 +52,22 @@ def _create_entities(hass, config_entry): # Fetch the schedule once at startup # in order to avoid every zone doing it for controller in person.controllers: - entities.append(RachioStandbySwitch(hass, controller)) + entities.append(RachioStandbySwitch(controller)) zones = controller.list_zones() current_schedule = controller.current_schedule _LOGGER.debug("Rachio setting up zones: %s", zones) for zone in zones: _LOGGER.debug("Rachio setting up zone: %s", zone) - entities.append( - RachioZone(hass, person, controller, zone, current_schedule) - ) + entities.append(RachioZone(person, controller, zone, current_schedule)) return entities -class RachioSwitch(RachioDeviceMixIn, SwitchDevice): +class RachioSwitch(RachioDeviceInfoProvider, SwitchDevice): """Represent a Rachio state that can be toggled.""" def __init__(self, controller, poll=True): """Initialize a new Rachio switch.""" + super().__init__(controller) self._controller = controller if poll: @@ -114,11 +113,8 @@ class RachioSwitch(RachioDeviceMixIn, SwitchDevice): class RachioStandbySwitch(RachioSwitch): """Representation of a standby status/button.""" - def __init__(self, hass, controller): + def __init__(self, controller): """Instantiate a new Rachio standby mode switch.""" - dispatcher_connect( - hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update - ) super().__init__(controller, poll=True) self._poll_update(controller.init_data) @@ -161,11 +157,17 @@ class RachioStandbySwitch(RachioSwitch): """Resume controller functionality.""" self._controller.rachio.device.on(self._controller.controller_id) + async def async_added_to_hass(self): + """Subscribe to updates.""" + async_dispatcher_connect( + self.hass, SIGNAL_RACHIO_CONTROLLER_UPDATE, self._handle_any_update + ) + class RachioZone(RachioSwitch): """Representation of one zone of sprinklers connected to the Rachio Iro.""" - def __init__(self, hass, person, controller, data, current_schedule): + def __init__(self, person, controller, data, current_schedule): """Initialize a new Rachio Zone.""" self._id = data[KEY_ID] self._zone_name = data[KEY_NAME] @@ -178,9 +180,6 @@ class RachioZone(RachioSwitch): super().__init__(controller, poll=False) self._state = self.zone_id == self._current_schedule.get(KEY_ZONE_ID) - # Listen for all zone updates - dispatcher_connect(hass, SIGNAL_RACHIO_ZONE_UPDATE, self._handle_update) - def __str__(self): """Display the zone as a string.""" return 'Rachio Zone "{}" on {}'.format(self.name, str(self._controller)) @@ -261,3 +260,9 @@ class RachioZone(RachioSwitch): self._state = False self.schedule_update_ha_state() + + async def async_added_to_hass(self): + """Subscribe to updates.""" + async_dispatcher_connect( + self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._handle_update + )