Add Rachio zone moisture service (#38817)

* Add zone moisture percent service

* Fix tests

* Add flex const

* Add fixed constant
This commit is contained in:
Brian Rogers 2020-08-18 22:51:24 -04:00 committed by GitHub
parent 81b4c6956b
commit 111c2006c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -60,6 +60,10 @@ RACHIO_API_EXCEPTIONS = (
STATUS_ONLINE = "ONLINE"
SCHEDULE_TYPE_FIXED = "FIXED"
SCHEDULE_TYPE_FLEX = "FLEX"
SERVICE_SET_ZONE_MOISTURE = "set_zone_moisture_percent"
SIGNAL_RACHIO_UPDATE = f"{DOMAIN}_update"
SIGNAL_RACHIO_CONTROLLER_UPDATE = f"{SIGNAL_RACHIO_UPDATE}_controller"
SIGNAL_RACHIO_RAIN_DELAY_UPDATE = f"{SIGNAL_RACHIO_UPDATE}_rain_delay"

View File

@ -0,0 +1,9 @@
set_zone_moisture_percent:
description: Set the moisture percentage of a zone or group of zones.
fields:
entity_id:
description: Name of the zone entity. Can also be a group of zones. [Required]
example: "switch.front_yard"
percent:
description: Set the desired zone moisture percentage from 0 to 100. [Required]
example: 50

View File

@ -3,8 +3,11 @@ from abc import abstractmethod
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.util.dt import as_timestamp, now, parse_datetime, utc_from_timestamp
@ -28,8 +31,12 @@ from .const import (
KEY_SCHEDULE_ID,
KEY_SUBTYPE,
KEY_SUMMARY,
KEY_TYPE,
KEY_ZONE_ID,
KEY_ZONE_NUMBER,
SCHEDULE_TYPE_FIXED,
SCHEDULE_TYPE_FLEX,
SERVICE_SET_ZONE_MOISTURE,
SIGNAL_RACHIO_CONTROLLER_UPDATE,
SIGNAL_RACHIO_RAIN_DELAY_UPDATE,
SIGNAL_RACHIO_SCHEDULE_UPDATE,
@ -55,9 +62,11 @@ from .webhooks import (
_LOGGER = logging.getLogger(__name__)
ATTR_PERCENT = "percent"
ATTR_SCHEDULE_SUMMARY = "Summary"
ATTR_SCHEDULE_ENABLED = "Enabled"
ATTR_SCHEDULE_DURATION = "Duration"
ATTR_SCHEDULE_TYPE = "Type"
ATTR_ZONE_NUMBER = "Zone number"
ATTR_ZONE_SHADE = "Shade"
ATTR_ZONE_SLOPE = "Slope"
@ -67,11 +76,24 @@ ATTR_ZONE_TYPE = "Type"
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Rachio switches."""
# Add all zones from all controllers as switches
has_flex_sched = False
entities = await hass.async_add_executor_job(_create_entities, hass, config_entry)
for entity in entities:
if isinstance(entity, RachioSchedule) and entity.type == SCHEDULE_TYPE_FLEX:
has_flex_sched = True
break
async_add_entities(entities)
_LOGGER.info("%d Rachio switch(es) added", len(entities))
platform = entity_platform.current_platform.get()
if has_flex_sched:
platform.async_register_entity_service(
SERVICE_SET_ZONE_MOISTURE,
{vol.Required(ATTR_PERCENT): cv.positive_int},
"set_moisture_percent",
)
def _create_entities(hass, config_entry):
entities = []
@ -355,6 +377,11 @@ class RachioZone(RachioSwitch):
"""Stop watering all zones."""
self._controller.stop_watering()
def set_moisture_percent(self, percent) -> None:
"""Set the zone moisture percent."""
_LOGGER.debug("Setting %s moisture to %s percent", self._zone_name, percent)
self._controller.rachio.zone.setMoisturePercent(self._id, percent / 100)
@callback
def _async_handle_update(self, *args, **kwargs) -> None:
"""Handle incoming webhook zone data."""
@ -391,6 +418,7 @@ class RachioSchedule(RachioSwitch):
self._duration = data[KEY_DURATION]
self._schedule_enabled = data[KEY_ENABLED]
self._summary = data[KEY_SUMMARY]
self.type = data.get(KEY_TYPE, SCHEDULE_TYPE_FIXED)
self._current_schedule = current_schedule
super().__init__(controller)
@ -416,6 +444,7 @@ class RachioSchedule(RachioSwitch):
ATTR_SCHEDULE_SUMMARY: self._summary,
ATTR_SCHEDULE_ENABLED: self.schedule_is_enabled,
ATTR_SCHEDULE_DURATION: f"{round(self._duration / 60)} minutes",
ATTR_SCHEDULE_TYPE: self.type,
}
@property