2019-04-03 15:40:03 +00:00
|
|
|
"""This platform provides binary sensors for key RainMachine data."""
|
2020-11-06 09:58:50 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from regenmaschine.controller import Controller
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2020-11-06 09:58:50 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-11-06 09:58:50 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-01-26 03:27:35 +00:00
|
|
|
from . import RainMachineEntity
|
|
|
|
from .const import (
|
2020-11-06 09:58:50 +00:00
|
|
|
DATA_CONTROLLER,
|
|
|
|
DATA_COORDINATOR,
|
2020-01-26 03:27:35 +00:00
|
|
|
DATA_PROVISION_SETTINGS,
|
|
|
|
DATA_RESTRICTIONS_CURRENT,
|
|
|
|
DATA_RESTRICTIONS_UNIVERSAL,
|
2020-11-06 09:58:50 +00:00
|
|
|
DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2020-01-23 04:49:47 +00:00
|
|
|
TYPE_FLOW_SENSOR = "flow_sensor"
|
|
|
|
TYPE_FREEZE = "freeze"
|
|
|
|
TYPE_FREEZE_PROTECTION = "freeze_protection"
|
|
|
|
TYPE_HOT_DAYS = "extra_water_on_hot_days"
|
|
|
|
TYPE_HOURLY = "hourly"
|
|
|
|
TYPE_MONTH = "month"
|
|
|
|
TYPE_RAINDELAY = "raindelay"
|
|
|
|
TYPE_RAINSENSOR = "rainsensor"
|
|
|
|
TYPE_WEEKDAY = "weekday"
|
|
|
|
|
|
|
|
BINARY_SENSORS = {
|
2020-01-26 03:27:35 +00:00
|
|
|
TYPE_FLOW_SENSOR: ("Flow Sensor", "mdi:water-pump", True, DATA_PROVISION_SETTINGS),
|
|
|
|
TYPE_FREEZE: ("Freeze Restrictions", "mdi:cancel", True, DATA_RESTRICTIONS_CURRENT),
|
2020-01-25 05:31:14 +00:00
|
|
|
TYPE_FREEZE_PROTECTION: (
|
|
|
|
"Freeze Protection",
|
|
|
|
"mdi:weather-snowy",
|
|
|
|
True,
|
2020-01-26 03:27:35 +00:00
|
|
|
DATA_RESTRICTIONS_UNIVERSAL,
|
2020-01-25 05:31:14 +00:00
|
|
|
),
|
|
|
|
TYPE_HOT_DAYS: (
|
|
|
|
"Extra Water on Hot Days",
|
|
|
|
"mdi:thermometer-lines",
|
|
|
|
True,
|
2020-01-26 03:27:35 +00:00
|
|
|
DATA_RESTRICTIONS_UNIVERSAL,
|
2020-01-25 05:31:14 +00:00
|
|
|
),
|
2020-01-26 03:27:35 +00:00
|
|
|
TYPE_HOURLY: (
|
|
|
|
"Hourly Restrictions",
|
|
|
|
"mdi:cancel",
|
|
|
|
False,
|
|
|
|
DATA_RESTRICTIONS_CURRENT,
|
|
|
|
),
|
|
|
|
TYPE_MONTH: ("Month Restrictions", "mdi:cancel", False, DATA_RESTRICTIONS_CURRENT),
|
2020-01-25 05:31:14 +00:00
|
|
|
TYPE_RAINDELAY: (
|
|
|
|
"Rain Delay Restrictions",
|
|
|
|
"mdi:cancel",
|
|
|
|
False,
|
2020-01-26 03:27:35 +00:00
|
|
|
DATA_RESTRICTIONS_CURRENT,
|
2020-01-25 05:31:14 +00:00
|
|
|
),
|
|
|
|
TYPE_RAINSENSOR: (
|
|
|
|
"Rain Sensor Restrictions",
|
|
|
|
"mdi:cancel",
|
|
|
|
False,
|
2020-01-26 03:27:35 +00:00
|
|
|
DATA_RESTRICTIONS_CURRENT,
|
|
|
|
),
|
|
|
|
TYPE_WEEKDAY: (
|
|
|
|
"Weekday Restrictions",
|
|
|
|
"mdi:cancel",
|
|
|
|
False,
|
|
|
|
DATA_RESTRICTIONS_CURRENT,
|
2020-01-25 05:31:14 +00:00
|
|
|
),
|
2020-01-23 04:49:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2020-11-06 09:58:50 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-30 18:38:59 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-11-06 09:58:50 +00:00
|
|
|
) -> None:
|
2018-11-14 20:23:49 +00:00
|
|
|
"""Set up RainMachine binary sensors based on a config entry."""
|
2020-11-06 09:58:50 +00:00
|
|
|
controller = hass.data[DOMAIN][DATA_CONTROLLER][entry.entry_id]
|
|
|
|
coordinators = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_get_sensor(api_category: str) -> partial:
|
|
|
|
"""Generate the appropriate sensor object for an API category."""
|
|
|
|
if api_category == DATA_PROVISION_SETTINGS:
|
|
|
|
return partial(
|
|
|
|
ProvisionSettingsBinarySensor,
|
|
|
|
coordinators[DATA_PROVISION_SETTINGS],
|
|
|
|
)
|
|
|
|
|
|
|
|
if api_category == DATA_RESTRICTIONS_CURRENT:
|
|
|
|
return partial(
|
|
|
|
CurrentRestrictionsBinarySensor,
|
|
|
|
coordinators[DATA_RESTRICTIONS_CURRENT],
|
|
|
|
)
|
|
|
|
|
|
|
|
return partial(
|
|
|
|
UniversalRestrictionsBinarySensor,
|
|
|
|
coordinators[DATA_RESTRICTIONS_UNIVERSAL],
|
|
|
|
)
|
|
|
|
|
2020-01-25 05:31:14 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2020-11-06 09:58:50 +00:00
|
|
|
async_get_sensor(api_category)(
|
|
|
|
controller, sensor_type, name, icon, enabled_by_default
|
2020-01-23 04:49:47 +00:00
|
|
|
)
|
2020-01-25 05:31:14 +00:00
|
|
|
for (
|
|
|
|
sensor_type,
|
|
|
|
(name, icon, enabled_by_default, api_category),
|
|
|
|
) in BINARY_SENSORS.items()
|
2020-11-06 09:58:50 +00:00
|
|
|
]
|
2020-01-25 05:31:14 +00:00
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class RainMachineBinarySensor(RainMachineEntity, BinarySensorEntity):
|
2020-11-06 09:58:50 +00:00
|
|
|
"""Define a general RainMachine binary sensor."""
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2020-01-25 05:31:14 +00:00
|
|
|
def __init__(
|
2020-11-06 09:58:50 +00:00
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
controller: Controller,
|
|
|
|
sensor_type: str,
|
|
|
|
name: str,
|
|
|
|
icon: str,
|
|
|
|
enabled_by_default: bool,
|
|
|
|
) -> None:
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-07-03 16:23:52 +00:00
|
|
|
super().__init__(coordinator, controller, sensor_type)
|
|
|
|
|
|
|
|
self._attr_entity_registry_enabled_default = enabled_by_default
|
|
|
|
self._attr_icon = icon
|
|
|
|
self._attr_name = name
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
|
2020-11-06 09:58:50 +00:00
|
|
|
class CurrentRestrictionsBinarySensor(RainMachineBinarySensor):
|
|
|
|
"""Define a binary sensor that handles current restrictions data."""
|
2020-03-17 11:00:54 +00:00
|
|
|
|
|
|
|
@callback
|
2020-11-06 09:58:50 +00:00
|
|
|
def update_from_latest_data(self) -> None:
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Update the state."""
|
2021-07-03 16:23:52 +00:00
|
|
|
if self._entity_type == TYPE_FREEZE:
|
|
|
|
self._attr_is_on = self.coordinator.data["freeze"]
|
|
|
|
elif self._entity_type == TYPE_HOURLY:
|
|
|
|
self._attr_is_on = self.coordinator.data["hourly"]
|
|
|
|
elif self._entity_type == TYPE_MONTH:
|
|
|
|
self._attr_is_on = self.coordinator.data["month"]
|
|
|
|
elif self._entity_type == TYPE_RAINDELAY:
|
|
|
|
self._attr_is_on = self.coordinator.data["rainDelay"]
|
|
|
|
elif self._entity_type == TYPE_RAINSENSOR:
|
|
|
|
self._attr_is_on = self.coordinator.data["rainSensor"]
|
|
|
|
elif self._entity_type == TYPE_WEEKDAY:
|
|
|
|
self._attr_is_on = self.coordinator.data["weekDay"]
|
2020-11-06 09:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProvisionSettingsBinarySensor(RainMachineBinarySensor):
|
|
|
|
"""Define a binary sensor that handles provisioning data."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
|
|
|
"""Update the state."""
|
2021-07-03 16:23:52 +00:00
|
|
|
if self._entity_type == TYPE_FLOW_SENSOR:
|
|
|
|
self._attr_is_on = self.coordinator.data["system"].get("useFlowSensor")
|
2020-11-06 09:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UniversalRestrictionsBinarySensor(RainMachineBinarySensor):
|
|
|
|
"""Define a binary sensor that handles universal restrictions data."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
|
|
|
"""Update the state."""
|
2021-07-03 16:23:52 +00:00
|
|
|
if self._entity_type == TYPE_FREEZE_PROTECTION:
|
|
|
|
self._attr_is_on = self.coordinator.data["freezeProtectEnabled"]
|
|
|
|
elif self._entity_type == TYPE_HOT_DAYS:
|
|
|
|
self._attr_is_on = self.coordinator.data["hotDaysExtraWatering"]
|