Address review items, retest zone updates back and forth, and standby mode

pull/32814/head
J. Nick Koston 2020-03-15 07:52:45 +00:00
parent 22480e6f3c
commit d5e076135c
3 changed files with 40 additions and 27 deletions

View File

@ -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."""

View File

@ -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

View File

@ -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
)