2019-04-03 15:40:03 +00:00
|
|
|
"""Integration with the Rachio Iro sprinkler system controller."""
|
2018-07-01 15:54:51 +00:00
|
|
|
from abc import abstractmethod
|
2017-05-29 09:15:27 +00:00
|
|
|
from datetime import timedelta
|
2018-01-21 06:35:38 +00:00
|
|
|
import logging
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2020-04-02 18:43:40 +00:00
|
|
|
from homeassistant.core import callback
|
2020-03-16 02:01:41 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2020-04-27 17:23:16 +00:00
|
|
|
from homeassistant.util.dt import as_timestamp, now
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MANUAL_RUN_MINS,
|
2020-03-14 05:46:17 +00:00
|
|
|
DEFAULT_MANUAL_RUN_MINS,
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN as DOMAIN_RACHIO,
|
2020-03-31 21:46:30 +00:00
|
|
|
KEY_CUSTOM_CROP,
|
|
|
|
KEY_CUSTOM_SHADE,
|
2020-05-31 15:26:56 +00:00
|
|
|
KEY_CUSTOM_SLOPE,
|
2019-07-31 19:25:30 +00:00
|
|
|
KEY_DEVICE_ID,
|
2020-04-01 19:27:56 +00:00
|
|
|
KEY_DURATION,
|
2019-07-31 19:25:30 +00:00
|
|
|
KEY_ENABLED,
|
|
|
|
KEY_ID,
|
2020-03-14 05:46:17 +00:00
|
|
|
KEY_IMAGE_URL,
|
2019-07-31 19:25:30 +00:00
|
|
|
KEY_NAME,
|
|
|
|
KEY_ON,
|
2020-04-27 17:23:16 +00:00
|
|
|
KEY_RAIN_DELAY,
|
2020-04-01 19:27:56 +00:00
|
|
|
KEY_SCHEDULE_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
KEY_SUBTYPE,
|
|
|
|
KEY_SUMMARY,
|
|
|
|
KEY_ZONE_ID,
|
|
|
|
KEY_ZONE_NUMBER,
|
2020-03-31 21:46:30 +00:00
|
|
|
SIGNAL_RACHIO_CONTROLLER_UPDATE,
|
2020-04-27 17:23:16 +00:00
|
|
|
SIGNAL_RACHIO_RAIN_DELAY_UPDATE,
|
2020-04-01 19:27:56 +00:00
|
|
|
SIGNAL_RACHIO_SCHEDULE_UPDATE,
|
2020-03-31 21:46:30 +00:00
|
|
|
SIGNAL_RACHIO_ZONE_UPDATE,
|
2020-05-31 15:26:56 +00:00
|
|
|
SLOPE_FLAT,
|
|
|
|
SLOPE_MODERATE,
|
|
|
|
SLOPE_SLIGHT,
|
|
|
|
SLOPE_STEEP,
|
2020-03-31 21:46:30 +00:00
|
|
|
)
|
|
|
|
from .entity import RachioDevice
|
|
|
|
from .webhooks import (
|
2020-04-27 17:23:16 +00:00
|
|
|
SUBTYPE_RAIN_DELAY_OFF,
|
|
|
|
SUBTYPE_RAIN_DELAY_ON,
|
2020-04-01 19:27:56 +00:00
|
|
|
SUBTYPE_SCHEDULE_COMPLETED,
|
|
|
|
SUBTYPE_SCHEDULE_STARTED,
|
|
|
|
SUBTYPE_SCHEDULE_STOPPED,
|
2020-03-31 21:46:30 +00:00
|
|
|
SUBTYPE_SLEEP_MODE_OFF,
|
|
|
|
SUBTYPE_SLEEP_MODE_ON,
|
|
|
|
SUBTYPE_ZONE_COMPLETED,
|
|
|
|
SUBTYPE_ZONE_STARTED,
|
|
|
|
SUBTYPE_ZONE_STOPPED,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-04-01 19:27:56 +00:00
|
|
|
ATTR_SCHEDULE_SUMMARY = "Summary"
|
|
|
|
ATTR_SCHEDULE_ENABLED = "Enabled"
|
|
|
|
ATTR_SCHEDULE_DURATION = "Duration"
|
2020-05-31 15:26:56 +00:00
|
|
|
ATTR_ZONE_NUMBER = "Zone number"
|
|
|
|
ATTR_ZONE_SHADE = "Shade"
|
|
|
|
ATTR_ZONE_SLOPE = "Slope"
|
|
|
|
ATTR_ZONE_SUMMARY = "Summary"
|
|
|
|
ATTR_ZONE_TYPE = "Type"
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the Rachio switches."""
|
2018-07-01 15:54:51 +00:00
|
|
|
# Add all zones from all controllers as switches
|
2020-03-16 02:01:41 +00:00
|
|
|
entities = await hass.async_add_executor_job(_create_entities, hass, config_entry)
|
|
|
|
async_add_entities(entities)
|
|
|
|
_LOGGER.info("%d Rachio switch(es) added", len(entities))
|
2017-05-29 09:15:27 +00:00
|
|
|
|
|
|
|
|
2020-03-16 02:01:41 +00:00
|
|
|
def _create_entities(hass, config_entry):
|
|
|
|
entities = []
|
2020-03-14 05:46:17 +00:00
|
|
|
person = hass.data[DOMAIN_RACHIO][config_entry.entry_id]
|
|
|
|
# Fetch the schedule once at startup
|
|
|
|
# in order to avoid every zone doing it
|
|
|
|
for controller in person.controllers:
|
2020-03-16 02:01:41 +00:00
|
|
|
entities.append(RachioStandbySwitch(controller))
|
2020-04-27 17:23:16 +00:00
|
|
|
entities.append(RachioRainDelay(controller))
|
2020-03-14 05:46:17 +00:00
|
|
|
zones = controller.list_zones()
|
2020-04-01 19:27:56 +00:00
|
|
|
schedules = controller.list_schedules()
|
2020-04-02 18:43:40 +00:00
|
|
|
flex_schedules = controller.list_flex_schedules()
|
2020-03-14 05:46:17 +00:00
|
|
|
current_schedule = controller.current_schedule
|
|
|
|
for zone in zones:
|
2020-03-16 02:01:41 +00:00
|
|
|
entities.append(RachioZone(person, controller, zone, current_schedule))
|
2020-04-02 18:43:40 +00:00
|
|
|
for sched in schedules + flex_schedules:
|
2020-04-01 19:27:56 +00:00
|
|
|
entities.append(RachioSchedule(person, controller, sched, current_schedule))
|
2020-04-02 18:43:40 +00:00
|
|
|
_LOGGER.debug("Added %s", entities)
|
2020-03-16 02:01:41 +00:00
|
|
|
return entities
|
2018-01-21 06:35:38 +00:00
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class RachioSwitch(RachioDevice, SwitchEntity):
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Represent a Rachio state that can be toggled."""
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-29 16:34:45 +00:00
|
|
|
def __init__(self, controller):
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Initialize a new Rachio switch."""
|
2020-03-16 02:01:41 +00:00
|
|
|
super().__init__(controller)
|
2020-04-29 16:34:45 +00:00
|
|
|
self._state = None
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Get a name for this switch."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"Switch on {self._controller.name}"
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return whether the switch is currently on."""
|
|
|
|
return self._state
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
@callback
|
|
|
|
def _async_handle_any_update(self, *args, **kwargs) -> None:
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Determine whether an update event applies to this device."""
|
|
|
|
if args[0][KEY_DEVICE_ID] != self._controller.controller_id:
|
|
|
|
# For another device
|
|
|
|
return
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
# For this device
|
2020-04-03 03:55:17 +00:00
|
|
|
self._async_handle_update(args, kwargs)
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
@abstractmethod
|
2020-04-03 03:55:17 +00:00
|
|
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Handle incoming webhook data."""
|
2017-05-29 09:15:27 +00:00
|
|
|
|
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
class RachioStandbySwitch(RachioSwitch):
|
|
|
|
"""Representation of a standby status/button."""
|
2017-05-29 09:15:27 +00:00
|
|
|
|
|
|
|
@property
|
2018-07-01 15:54:51 +00:00
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the standby switch."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._controller.name} in standby mode"
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-09-27 21:17:15 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
2019-08-02 21:20:07 +00:00
|
|
|
"""Return a unique id by combining controller id and purpose."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._controller.controller_id}-standby"
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
@property
|
2018-07-01 15:54:51 +00:00
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return an icon for the standby switch."""
|
|
|
|
return "mdi:power"
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
@callback
|
|
|
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Update the state using webhook data."""
|
2019-12-18 20:04:54 +00:00
|
|
|
if args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_ON:
|
2018-07-01 15:54:51 +00:00
|
|
|
self._state = True
|
2019-12-18 20:04:54 +00:00
|
|
|
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_OFF:
|
2018-07-01 15:54:51 +00:00
|
|
|
self._state = False
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
self.async_write_ha_state()
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
def turn_on(self, **kwargs) -> None:
|
|
|
|
"""Put the controller in standby mode."""
|
|
|
|
self._controller.rachio.device.off(self._controller.controller_id)
|
2017-07-06 06:30:01 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Resume controller functionality."""
|
|
|
|
self._controller.rachio.device.on(self._controller.controller_id)
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-03-16 02:01:41 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to updates."""
|
2020-04-29 16:34:45 +00:00
|
|
|
if KEY_ON in self._controller.init_data:
|
|
|
|
self._state = not self._controller.init_data[KEY_ON]
|
|
|
|
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
2020-04-03 03:55:17 +00:00
|
|
|
self.hass,
|
|
|
|
SIGNAL_RACHIO_CONTROLLER_UPDATE,
|
|
|
|
self._async_handle_any_update,
|
2020-04-02 16:25:33 +00:00
|
|
|
)
|
2020-03-16 02:01:41 +00:00
|
|
|
)
|
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-27 17:23:16 +00:00
|
|
|
class RachioRainDelay(RachioSwitch):
|
|
|
|
"""Representation of a rain delay status/switch."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the switch."""
|
|
|
|
return f"{self._controller.name} rain delay"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique id by combining controller id and purpose."""
|
|
|
|
return f"{self._controller.controller_id}-delay"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return an icon for rain delay."""
|
|
|
|
return "mdi:camera-timer"
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
|
|
|
"""Update the state using webhook data."""
|
|
|
|
if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_ON:
|
|
|
|
self._state = True
|
|
|
|
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_OFF:
|
|
|
|
self._state = False
|
|
|
|
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs) -> None:
|
|
|
|
"""Activate a 24 hour rain delay on the controller."""
|
|
|
|
self._controller.rachio.device.rainDelay(self._controller.controller_id, 86400)
|
|
|
|
_LOGGER.debug("Starting rain delay for 24 hours")
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Resume controller functionality."""
|
|
|
|
self._controller.rachio.device.rainDelay(self._controller.controller_id, 0)
|
|
|
|
_LOGGER.debug("Canceling rain delay")
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to updates."""
|
2020-04-29 16:34:45 +00:00
|
|
|
if KEY_RAIN_DELAY in self._controller.init_data:
|
|
|
|
self._state = self._controller.init_data[
|
|
|
|
KEY_RAIN_DELAY
|
|
|
|
] / 1000 > as_timestamp(now())
|
|
|
|
|
2020-04-27 17:23:16 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
SIGNAL_RACHIO_RAIN_DELAY_UPDATE,
|
|
|
|
self._async_handle_any_update,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
class RachioZone(RachioSwitch):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Representation of one zone of sprinklers connected to the Rachio Iro."""
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-03-16 02:01:41 +00:00
|
|
|
def __init__(self, person, controller, data, current_schedule):
|
2017-05-29 09:15:27 +00:00
|
|
|
"""Initialize a new Rachio Zone."""
|
2018-07-01 15:54:51 +00:00
|
|
|
self._id = data[KEY_ID]
|
|
|
|
self._zone_name = data[KEY_NAME]
|
|
|
|
self._zone_number = data[KEY_ZONE_NUMBER]
|
|
|
|
self._zone_enabled = data[KEY_ENABLED]
|
2020-03-14 05:46:17 +00:00
|
|
|
self._entity_picture = data.get(KEY_IMAGE_URL)
|
|
|
|
self._person = person
|
2020-03-31 21:46:30 +00:00
|
|
|
self._shade_type = data.get(KEY_CUSTOM_SHADE, {}).get(KEY_NAME)
|
|
|
|
self._zone_type = data.get(KEY_CUSTOM_CROP, {}).get(KEY_NAME)
|
2020-05-31 15:26:56 +00:00
|
|
|
self._slope_type = data.get(KEY_CUSTOM_SLOPE, {}).get(KEY_NAME)
|
2020-04-04 20:08:49 +00:00
|
|
|
self._summary = ""
|
2020-03-14 05:46:17 +00:00
|
|
|
self._current_schedule = current_schedule
|
2020-04-29 16:34:45 +00:00
|
|
|
super().__init__(controller)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
"""Display the zone as a string."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return 'Rachio Zone "{}" on {}'.format(self.name, str(self._controller))
|
2017-05-29 09:15:27 +00:00
|
|
|
|
|
|
|
@property
|
2018-07-01 15:54:51 +00:00
|
|
|
def zone_id(self) -> str:
|
2017-05-29 09:15:27 +00:00
|
|
|
"""How the Rachio API refers to the zone."""
|
2018-07-01 15:54:51 +00:00
|
|
|
return self._id
|
2017-05-29 09:15:27 +00:00
|
|
|
|
|
|
|
@property
|
2018-07-01 15:54:51 +00:00
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the friendly name of the zone."""
|
|
|
|
return self._zone_name
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-09-27 21:17:15 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
2019-08-02 21:20:07 +00:00
|
|
|
"""Return a unique id by combining controller id and zone number."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._controller.controller_id}-zone-{self.zone_id}"
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
@property
|
2018-07-01 15:54:51 +00:00
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon to display."""
|
|
|
|
return "mdi:water"
|
2017-05-29 09:15:27 +00:00
|
|
|
|
|
|
|
@property
|
2018-07-01 15:54:51 +00:00
|
|
|
def zone_is_enabled(self) -> bool:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return whether the zone is allowed to run."""
|
2018-07-01 15:54:51 +00:00
|
|
|
return self._zone_enabled
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
@property
|
|
|
|
def entity_picture(self):
|
|
|
|
"""Return the entity picture to use in the frontend, if any."""
|
|
|
|
return self._entity_picture
|
|
|
|
|
2017-05-29 09:15:27 +00:00
|
|
|
@property
|
2020-04-03 03:55:17 +00:00
|
|
|
def device_state_attributes(self) -> dict:
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Return the optional state attributes."""
|
2020-03-31 21:46:30 +00:00
|
|
|
props = {ATTR_ZONE_NUMBER: self._zone_number, ATTR_ZONE_SUMMARY: self._summary}
|
|
|
|
if self._shade_type:
|
|
|
|
props[ATTR_ZONE_SHADE] = self._shade_type
|
|
|
|
if self._zone_type:
|
|
|
|
props[ATTR_ZONE_TYPE] = self._zone_type
|
2020-05-31 15:26:56 +00:00
|
|
|
if self._slope_type:
|
|
|
|
if self._slope_type == SLOPE_FLAT:
|
|
|
|
props[ATTR_ZONE_SLOPE] = "Flat"
|
|
|
|
elif self._slope_type == SLOPE_SLIGHT:
|
|
|
|
props[ATTR_ZONE_SLOPE] = "Slight"
|
|
|
|
elif self._slope_type == SLOPE_MODERATE:
|
|
|
|
props[ATTR_ZONE_SLOPE] = "Moderate"
|
|
|
|
elif self._slope_type == SLOPE_STEEP:
|
|
|
|
props[ATTR_ZONE_SLOPE] = "Steep"
|
2020-03-31 21:46:30 +00:00
|
|
|
return props
|
2018-07-01 15:54:51 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs) -> None:
|
|
|
|
"""Start watering this zone."""
|
|
|
|
# Stop other zones first
|
|
|
|
self.turn_off()
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
# Start this zone
|
2020-03-14 05:46:17 +00:00
|
|
|
manual_run_time = timedelta(
|
|
|
|
minutes=self._person.config_entry.options.get(
|
|
|
|
CONF_MANUAL_RUN_MINS, DEFAULT_MANUAL_RUN_MINS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self._controller.rachio.zone.start(self.zone_id, manual_run_time.seconds)
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Watering %s on %s for %s",
|
|
|
|
self.name,
|
|
|
|
self._controller.name,
|
|
|
|
str(manual_run_time),
|
|
|
|
)
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Stop watering all zones."""
|
|
|
|
self._controller.stop_watering()
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
@callback
|
|
|
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
2018-07-01 15:54:51 +00:00
|
|
|
"""Handle incoming webhook zone data."""
|
|
|
|
if args[0][KEY_ZONE_ID] != self.zone_id:
|
|
|
|
return
|
|
|
|
|
2020-05-31 15:26:56 +00:00
|
|
|
self._summary = args[0][KEY_SUMMARY]
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
if args[0][KEY_SUBTYPE] == SUBTYPE_ZONE_STARTED:
|
|
|
|
self._state = True
|
2019-07-31 19:25:30 +00:00
|
|
|
elif args[0][KEY_SUBTYPE] in [SUBTYPE_ZONE_STOPPED, SUBTYPE_ZONE_COMPLETED]:
|
2018-07-01 15:54:51 +00:00
|
|
|
self._state = False
|
2017-05-29 09:15:27 +00:00
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
self.async_write_ha_state()
|
2020-03-16 02:01:41 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to updates."""
|
2020-04-29 16:34:45 +00:00
|
|
|
self._state = self.zone_id == self._current_schedule.get(KEY_ZONE_ID)
|
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update
|
|
|
|
)
|
2020-03-16 02:01:41 +00:00
|
|
|
)
|
2020-03-31 21:46:30 +00:00
|
|
|
|
2020-04-01 19:27:56 +00:00
|
|
|
|
|
|
|
class RachioSchedule(RachioSwitch):
|
|
|
|
"""Representation of one fixed schedule on the Rachio Iro."""
|
|
|
|
|
|
|
|
def __init__(self, person, controller, data, current_schedule):
|
|
|
|
"""Initialize a new Rachio Schedule."""
|
2020-04-02 18:43:40 +00:00
|
|
|
self._schedule_id = data[KEY_ID]
|
2020-04-01 19:27:56 +00:00
|
|
|
self._schedule_name = data[KEY_NAME]
|
|
|
|
self._duration = data[KEY_DURATION]
|
|
|
|
self._schedule_enabled = data[KEY_ENABLED]
|
|
|
|
self._summary = data[KEY_SUMMARY]
|
|
|
|
self._current_schedule = current_schedule
|
2020-04-29 16:34:45 +00:00
|
|
|
super().__init__(controller)
|
2020-04-01 19:27:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the friendly name of the schedule."""
|
|
|
|
return f"{self._schedule_name} Schedule"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique id by combining controller id and schedule."""
|
2020-04-02 18:43:40 +00:00
|
|
|
return f"{self._controller.controller_id}-schedule-{self._schedule_id}"
|
2020-04-01 19:27:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon to display."""
|
2020-04-27 17:23:16 +00:00
|
|
|
return "mdi:water" if self.schedule_is_enabled else "mdi:water-off"
|
2020-04-01 19:27:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self) -> dict:
|
|
|
|
"""Return the optional state attributes."""
|
|
|
|
return {
|
|
|
|
ATTR_SCHEDULE_SUMMARY: self._summary,
|
|
|
|
ATTR_SCHEDULE_ENABLED: self.schedule_is_enabled,
|
2020-04-02 18:43:40 +00:00
|
|
|
ATTR_SCHEDULE_DURATION: f"{round(self._duration / 60)} minutes",
|
2020-04-01 19:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def schedule_is_enabled(self) -> bool:
|
|
|
|
"""Return whether the schedule is allowed to run."""
|
|
|
|
return self._schedule_enabled
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs) -> None:
|
|
|
|
"""Start this schedule."""
|
|
|
|
|
2020-04-02 18:43:40 +00:00
|
|
|
self._controller.rachio.schedulerule.start(self._schedule_id)
|
2020-04-01 19:27:56 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Schedule %s started on %s", self.name, self._controller.name,
|
|
|
|
)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Stop watering all zones."""
|
|
|
|
self._controller.stop_watering()
|
|
|
|
|
2020-04-02 18:43:40 +00:00
|
|
|
@callback
|
2020-04-03 03:55:17 +00:00
|
|
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
2020-04-01 19:27:56 +00:00
|
|
|
"""Handle incoming webhook schedule data."""
|
|
|
|
# Schedule ID not passed when running individual zones, so we catch that error
|
|
|
|
try:
|
2020-04-02 18:43:40 +00:00
|
|
|
if args[0][KEY_SCHEDULE_ID] == self._schedule_id:
|
2020-04-01 19:27:56 +00:00
|
|
|
if args[0][KEY_SUBTYPE] in [SUBTYPE_SCHEDULE_STARTED]:
|
|
|
|
self._state = True
|
|
|
|
elif args[0][KEY_SUBTYPE] in [
|
|
|
|
SUBTYPE_SCHEDULE_STOPPED,
|
|
|
|
SUBTYPE_SCHEDULE_COMPLETED,
|
|
|
|
]:
|
|
|
|
self._state = False
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
self.async_write_ha_state()
|
2020-04-01 19:27:56 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to updates."""
|
2020-04-29 16:34:45 +00:00
|
|
|
self._state = self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
|
|
|
|
|
2020-04-03 03:55:17 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update
|
|
|
|
)
|
2020-04-01 19:27:56 +00:00
|
|
|
)
|