2019-04-03 15:40:03 +00:00
|
|
|
"""This component provides support for RainMachine programs and zones."""
|
2019-03-13 14:19:26 +00:00
|
|
|
from datetime import datetime
|
2019-03-21 05:56:46 +00:00
|
|
|
import logging
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2019-12-05 05:12:44 +00:00
|
|
|
from regenmaschine.errors import RequestError
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import ATTR_ID
|
2020-03-17 11:00:54 +00:00
|
|
|
from homeassistant.core import callback
|
2020-01-26 03:27:35 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
from . import RainMachineEntity
|
|
|
|
from .const import (
|
2020-10-28 21:52:42 +00:00
|
|
|
CONF_ZONE_RUN_TIME,
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_CLIENT,
|
2020-01-26 03:27:35 +00:00
|
|
|
DATA_PROGRAMS,
|
|
|
|
DATA_ZONES,
|
|
|
|
DATA_ZONES_DETAILS,
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN as RAINMACHINE_DOMAIN,
|
|
|
|
PROGRAM_UPDATE_TOPIC,
|
|
|
|
ZONE_UPDATE_TOPIC,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-05-01 19:36:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_AREA = "area"
|
|
|
|
ATTR_CS_ON = "cs_on"
|
|
|
|
ATTR_CURRENT_CYCLE = "current_cycle"
|
|
|
|
ATTR_CYCLES = "cycles"
|
|
|
|
ATTR_DELAY = "delay"
|
|
|
|
ATTR_DELAY_ON = "delay_on"
|
|
|
|
ATTR_FIELD_CAPACITY = "field_capacity"
|
2020-06-02 03:49:02 +00:00
|
|
|
ATTR_NEXT_RUN = "next_run"
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_NO_CYCLES = "number_of_cycles"
|
|
|
|
ATTR_PRECIP_RATE = "sprinkler_head_precipitation_rate"
|
|
|
|
ATTR_RESTRICTIONS = "restrictions"
|
|
|
|
ATTR_SLOPE = "slope"
|
|
|
|
ATTR_SOAK = "soak"
|
|
|
|
ATTR_SOIL_TYPE = "soil_type"
|
|
|
|
ATTR_SPRINKLER_TYPE = "sprinkler_head_type"
|
|
|
|
ATTR_STATUS = "status"
|
|
|
|
ATTR_SUN_EXPOSURE = "sun_exposure"
|
2020-06-02 03:49:02 +00:00
|
|
|
ATTR_TIME_REMAINING = "time_remaining"
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_VEGETATION_TYPE = "vegetation_type"
|
|
|
|
ATTR_ZONES = "zones"
|
|
|
|
|
|
|
|
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
|
|
|
|
2020-08-22 21:19:20 +00:00
|
|
|
RUN_STATUS_MAP = {0: "Not Running", 1: "Running", 2: "Queued"}
|
2018-05-08 22:10:03 +00:00
|
|
|
|
|
|
|
SOIL_TYPE_MAP = {
|
2019-07-31 19:25:30 +00:00
|
|
|
0: "Not Set",
|
|
|
|
1: "Clay Loam",
|
|
|
|
2: "Silty Clay",
|
|
|
|
3: "Clay",
|
|
|
|
4: "Loam",
|
|
|
|
5: "Sandy Loam",
|
|
|
|
6: "Loamy Sand",
|
|
|
|
7: "Sand",
|
|
|
|
8: "Sandy Clay",
|
|
|
|
9: "Silt Loam",
|
|
|
|
10: "Silt",
|
|
|
|
99: "Other",
|
2018-05-08 22:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SLOPE_TYPE_MAP = {
|
2019-07-31 19:25:30 +00:00
|
|
|
0: "Not Set",
|
|
|
|
1: "Flat",
|
|
|
|
2: "Moderate",
|
|
|
|
3: "High",
|
|
|
|
4: "Very High",
|
|
|
|
99: "Other",
|
2018-05-08 22:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SPRINKLER_TYPE_MAP = {
|
2019-07-31 19:25:30 +00:00
|
|
|
0: "Not Set",
|
|
|
|
1: "Popup Spray",
|
|
|
|
2: "Rotors",
|
|
|
|
3: "Surface Drip",
|
|
|
|
4: "Bubblers Drip",
|
|
|
|
99: "Other",
|
2018-05-08 22:10:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUN_EXPOSURE_MAP = {0: "Not Set", 1: "Full Sun", 2: "Partial Shade", 3: "Full Shade"}
|
2018-05-08 22:10:03 +00:00
|
|
|
|
|
|
|
VEGETATION_MAP = {
|
2019-07-31 19:25:30 +00:00
|
|
|
0: "Not Set",
|
|
|
|
2: "Cool Season Grass",
|
|
|
|
3: "Fruit Trees",
|
|
|
|
4: "Flowers",
|
|
|
|
5: "Vegetables",
|
|
|
|
6: "Citrus",
|
|
|
|
7: "Trees and Bushes",
|
|
|
|
9: "Drought Tolerant Plants",
|
|
|
|
10: "Warm Season Grass",
|
|
|
|
99: "Other",
|
2018-05-08 22:10:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
SWITCH_TYPE_PROGRAM = "program"
|
|
|
|
SWITCH_TYPE_ZONE = "zone"
|
|
|
|
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-11-14 20:23:49 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up RainMachine switches based on a config entry."""
|
|
|
|
rainmachine = hass.data[RAINMACHINE_DOMAIN][DATA_CLIENT][entry.entry_id]
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-04-28 13:46:58 +00:00
|
|
|
entities = []
|
2020-01-26 03:27:35 +00:00
|
|
|
for program in rainmachine.data[DATA_PROGRAMS]:
|
2018-05-08 22:10:03 +00:00
|
|
|
entities.append(RainMachineProgram(rainmachine, program))
|
2020-01-26 03:27:35 +00:00
|
|
|
for zone in rainmachine.data[DATA_ZONES]:
|
|
|
|
entities.append(RainMachineZone(rainmachine, zone))
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities, True)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class RainMachineSwitch(RainMachineEntity, SwitchEntity):
|
2018-05-29 19:02:16 +00:00
|
|
|
"""A class to represent a generic RainMachine switch."""
|
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
def __init__(self, rainmachine, switch_data):
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Initialize a generic RainMachine switch."""
|
|
|
|
super().__init__(rainmachine)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
self._is_on = False
|
|
|
|
self._name = switch_data["name"]
|
|
|
|
self._switch_data = switch_data
|
|
|
|
self._rainmachine_entity_id = switch_data["uid"]
|
|
|
|
self._switch_type = None
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2019-03-13 14:19:26 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
2020-01-26 03:27:35 +00:00
|
|
|
return self._switch_data["active"]
|
2019-03-13 14:19:26 +00:00
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:water"
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return whether the program is running."""
|
|
|
|
return self._is_on
|
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2020-04-04 16:21:14 +00:00
|
|
|
return "{}_{}_{}".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rainmachine.device_mac.replace(":", ""),
|
|
|
|
self._switch_type,
|
|
|
|
self._rainmachine_entity_id,
|
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
async def _async_run_switch_coroutine(self, api_coro) -> None:
|
|
|
|
"""Run a coroutine to toggle the switch."""
|
|
|
|
try:
|
|
|
|
resp = await api_coro
|
|
|
|
except RequestError as err:
|
|
|
|
_LOGGER.error(
|
|
|
|
'Error while toggling %s "%s": %s',
|
|
|
|
self._switch_type,
|
|
|
|
self.unique_id,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
if resp["statusCode"] != 0:
|
|
|
|
_LOGGER.error(
|
|
|
|
'Error while toggling %s "%s": %s',
|
|
|
|
self._switch_type,
|
|
|
|
self.unique_id,
|
|
|
|
resp["message"],
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.hass.async_create_task(self.rainmachine.async_update_programs_and_zones())
|
2018-06-10 08:23:07 +00:00
|
|
|
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-05-08 22:10:03 +00:00
|
|
|
class RainMachineProgram(RainMachineSwitch):
|
2017-08-08 07:49:25 +00:00
|
|
|
"""A RainMachine program."""
|
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
def __init__(self, rainmachine, switch_data):
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Initialize a generic RainMachine switch."""
|
2020-01-26 03:27:35 +00:00
|
|
|
super().__init__(rainmachine, switch_data)
|
|
|
|
self._switch_type = SWITCH_TYPE_PROGRAM
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-02-12 03:55:51 +00:00
|
|
|
@property
|
2018-05-08 22:10:03 +00:00
|
|
|
def zones(self) -> list:
|
|
|
|
"""Return a list of active zones associated with this program."""
|
2020-01-26 03:27:35 +00:00
|
|
|
return [z for z in self._switch_data["wateringTimes"] if z["active"]]
|
2018-02-12 03:55:51 +00:00
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2020-04-06 05:36:23 +00:00
|
|
|
self.async_on_remove(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect(
|
2020-01-26 03:27:35 +00:00
|
|
|
self.hass, PROGRAM_UPDATE_TOPIC, self._update_state
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-14 20:23:49 +00:00
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Turn the program off."""
|
2020-01-26 03:27:35 +00:00
|
|
|
await self._async_run_switch_coroutine(
|
2020-01-27 01:01:59 +00:00
|
|
|
self.rainmachine.controller.programs.stop(self._rainmachine_entity_id)
|
2020-01-26 03:27:35 +00:00
|
|
|
)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Turn the program on."""
|
2020-01-26 03:27:35 +00:00
|
|
|
await self._async_run_switch_coroutine(
|
2020-01-27 01:01:59 +00:00
|
|
|
self.rainmachine.controller.programs.start(self._rainmachine_entity_id)
|
2020-01-26 03:27:35 +00:00
|
|
|
)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2020-03-17 11:00:54 +00:00
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Update info for the program."""
|
2020-01-26 03:27:35 +00:00
|
|
|
[self._switch_data] = [
|
|
|
|
p
|
|
|
|
for p in self.rainmachine.data[DATA_PROGRAMS]
|
|
|
|
if p["uid"] == self._rainmachine_entity_id
|
|
|
|
]
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
self._is_on = bool(self._switch_data["status"])
|
2018-05-08 22:10:03 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
try:
|
|
|
|
next_run = datetime.strptime(
|
2020-04-04 16:21:14 +00:00
|
|
|
"{} {}".format(
|
2020-01-26 03:27:35 +00:00
|
|
|
self._switch_data["nextRun"], self._switch_data["startTime"]
|
|
|
|
),
|
|
|
|
"%Y-%m-%d %H:%M",
|
|
|
|
).isoformat()
|
|
|
|
except ValueError:
|
|
|
|
next_run = None
|
|
|
|
|
|
|
|
self._attrs.update(
|
|
|
|
{
|
|
|
|
ATTR_ID: self._switch_data["uid"],
|
|
|
|
ATTR_NEXT_RUN: next_run,
|
|
|
|
ATTR_SOAK: self._switch_data.get("soak"),
|
2020-08-22 21:19:20 +00:00
|
|
|
ATTR_STATUS: RUN_STATUS_MAP[self._switch_data["status"]],
|
2020-01-26 03:27:35 +00:00
|
|
|
ATTR_ZONES: ", ".join(z["name"] for z in self.zones),
|
|
|
|
}
|
|
|
|
)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
|
|
|
|
2018-05-08 22:10:03 +00:00
|
|
|
class RainMachineZone(RainMachineSwitch):
|
2017-08-08 07:49:25 +00:00
|
|
|
"""A RainMachine zone."""
|
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
def __init__(self, rainmachine, switch_data):
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Initialize a RainMachine zone."""
|
2020-01-26 03:27:35 +00:00
|
|
|
super().__init__(rainmachine, switch_data)
|
|
|
|
self._switch_type = SWITCH_TYPE_ZONE
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-05-08 22:10:03 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2020-04-06 05:36:23 +00:00
|
|
|
self.async_on_remove(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect(
|
2020-01-26 03:27:35 +00:00
|
|
|
self.hass, PROGRAM_UPDATE_TOPIC, self._update_state
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2020-04-06 05:36:23 +00:00
|
|
|
self.async_on_remove(
|
2020-01-26 03:27:35 +00:00
|
|
|
async_dispatcher_connect(self.hass, ZONE_UPDATE_TOPIC, self._update_state)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-02-12 03:55:51 +00:00
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Turn the zone off."""
|
2020-01-26 03:27:35 +00:00
|
|
|
await self._async_run_switch_coroutine(
|
2020-01-27 01:01:59 +00:00
|
|
|
self.rainmachine.controller.zones.stop(self._rainmachine_entity_id)
|
2020-01-26 03:27:35 +00:00
|
|
|
)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Turn the zone on."""
|
2020-01-26 03:27:35 +00:00
|
|
|
await self._async_run_switch_coroutine(
|
2020-01-27 01:01:59 +00:00
|
|
|
self.rainmachine.controller.zones.start(
|
2020-10-28 21:52:42 +00:00
|
|
|
self._rainmachine_entity_id,
|
|
|
|
self.rainmachine.config_entry.options[CONF_ZONE_RUN_TIME],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-26 03:27:35 +00:00
|
|
|
)
|
2017-08-08 07:49:25 +00:00
|
|
|
|
2020-03-17 11:00:54 +00:00
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
2017-08-08 07:49:25 +00:00
|
|
|
"""Update info for the zone."""
|
2020-01-26 03:27:35 +00:00
|
|
|
[self._switch_data] = [
|
|
|
|
z
|
|
|
|
for z in self.rainmachine.data[DATA_ZONES]
|
|
|
|
if z["uid"] == self._rainmachine_entity_id
|
|
|
|
]
|
|
|
|
[details] = [
|
|
|
|
z
|
|
|
|
for z in self.rainmachine.data[DATA_ZONES_DETAILS]
|
|
|
|
if z["uid"] == self._rainmachine_entity_id
|
|
|
|
]
|
|
|
|
|
|
|
|
self._is_on = bool(self._switch_data["state"])
|
|
|
|
|
|
|
|
self._attrs.update(
|
|
|
|
{
|
2020-08-22 21:19:20 +00:00
|
|
|
ATTR_STATUS: RUN_STATUS_MAP[self._switch_data["state"]],
|
2020-01-26 03:27:35 +00:00
|
|
|
ATTR_AREA: details.get("waterSense").get("area"),
|
|
|
|
ATTR_CURRENT_CYCLE: self._switch_data.get("cycle"),
|
|
|
|
ATTR_FIELD_CAPACITY: details.get("waterSense").get("fieldCapacity"),
|
2020-06-02 03:49:02 +00:00
|
|
|
ATTR_ID: self._switch_data["uid"],
|
2020-01-26 03:27:35 +00:00
|
|
|
ATTR_NO_CYCLES: self._switch_data.get("noOfCycles"),
|
|
|
|
ATTR_PRECIP_RATE: details.get("waterSense").get("precipitationRate"),
|
|
|
|
ATTR_RESTRICTIONS: self._switch_data.get("restriction"),
|
|
|
|
ATTR_SLOPE: SLOPE_TYPE_MAP.get(details.get("slope")),
|
|
|
|
ATTR_SOIL_TYPE: SOIL_TYPE_MAP.get(details.get("sun")),
|
|
|
|
ATTR_SPRINKLER_TYPE: SPRINKLER_TYPE_MAP.get(details.get("group_id")),
|
|
|
|
ATTR_SUN_EXPOSURE: SUN_EXPOSURE_MAP.get(details.get("sun")),
|
2020-06-02 03:49:02 +00:00
|
|
|
ATTR_TIME_REMAINING: self._switch_data.get("remaining"),
|
2020-01-26 03:27:35 +00:00
|
|
|
ATTR_VEGETATION_TYPE: VEGETATION_MAP.get(self._switch_data.get("type")),
|
|
|
|
}
|
|
|
|
)
|