2019-04-03 15:40:03 +00:00
|
|
|
"""This platform provides support for sensor data from RainMachine."""
|
2021-08-23 17:23:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
2022-04-13 20:41:48 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-11-06 09:58:50 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2021-09-14 20:06:40 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-16 15:36:16 +00:00
|
|
|
SensorDeviceClass,
|
2021-09-14 20:06:40 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-16 15:36:16 +00:00
|
|
|
SensorStateClass,
|
2021-09-14 20:06:40 +00:00
|
|
|
)
|
2020-11-06 09:58:50 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-16 15:36:16 +00:00
|
|
|
from homeassistant.const import TEMP_CELSIUS, VOLUME_CUBIC_METERS
|
2020-11-06 09:58:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-12-16 15:36:16 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-04-13 20:41:48 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2018-05-29 19:02:16 +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_UNIVERSAL,
|
2022-04-13 20:41:48 +00:00
|
|
|
DATA_ZONES,
|
2020-11-06 09:58:50 +00:00
|
|
|
DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-04-13 20:41:48 +00:00
|
|
|
from .model import (
|
|
|
|
RainMachineDescriptionMixinApiCategory,
|
|
|
|
RainMachineDescriptionMixinUid,
|
|
|
|
)
|
|
|
|
|
|
|
|
DEFAULT_ZONE_COMPLETION_TIME_WOBBLE_TOLERANCE = timedelta(seconds=5)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-01-23 04:49:47 +00:00
|
|
|
TYPE_FLOW_SENSOR_CLICK_M3 = "flow_sensor_clicks_cubic_meter"
|
|
|
|
TYPE_FLOW_SENSOR_CONSUMED_LITERS = "flow_sensor_consumed_liters"
|
|
|
|
TYPE_FLOW_SENSOR_START_INDEX = "flow_sensor_start_index"
|
|
|
|
TYPE_FLOW_SENSOR_WATERING_CLICKS = "flow_sensor_watering_clicks"
|
|
|
|
TYPE_FREEZE_TEMP = "freeze_protect_temp"
|
2022-04-13 20:41:48 +00:00
|
|
|
TYPE_ZONE_RUN_COMPLETION_TIME = "zone_run_completion_time"
|
|
|
|
|
|
|
|
ZONE_STATE_NOT_RUNNING = "not_running"
|
|
|
|
ZONE_STATE_PENDING = "pending"
|
|
|
|
ZONE_STATE_RUNNING = "running"
|
|
|
|
ZONE_STATE_MAP = {
|
|
|
|
0: ZONE_STATE_NOT_RUNNING,
|
|
|
|
1: ZONE_STATE_RUNNING,
|
|
|
|
2: ZONE_STATE_PENDING,
|
|
|
|
}
|
2020-01-23 04:49:47 +00:00
|
|
|
|
2021-08-23 17:23:35 +00:00
|
|
|
|
|
|
|
@dataclass
|
2022-04-13 20:41:48 +00:00
|
|
|
class RainMachineSensorDescriptionApiCategory(
|
|
|
|
SensorEntityDescription, RainMachineDescriptionMixinApiCategory
|
|
|
|
):
|
|
|
|
"""Describe a RainMachine sensor."""
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class RainMachineSensorDescriptionUid(
|
|
|
|
SensorEntityDescription, RainMachineDescriptionMixinUid
|
2021-08-23 17:23:35 +00:00
|
|
|
):
|
2021-08-25 14:36:25 +00:00
|
|
|
"""Describe a RainMachine sensor."""
|
2021-08-23 17:23:35 +00:00
|
|
|
|
|
|
|
|
2021-08-25 14:36:25 +00:00
|
|
|
SENSOR_DESCRIPTIONS = (
|
2022-04-13 20:41:48 +00:00
|
|
|
RainMachineSensorDescriptionApiCategory(
|
2021-08-23 17:23:35 +00:00
|
|
|
key=TYPE_FLOW_SENSOR_CLICK_M3,
|
2021-10-19 20:10:51 +00:00
|
|
|
name="Flow Sensor Clicks per Cubic Meter",
|
2021-08-23 17:23:35 +00:00
|
|
|
icon="mdi:water-pump",
|
|
|
|
native_unit_of_measurement=f"clicks/{VOLUME_CUBIC_METERS}",
|
2021-12-16 15:36:16 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-23 17:23:35 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 15:36:16 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-23 17:23:35 +00:00
|
|
|
api_category=DATA_PROVISION_SETTINGS,
|
2020-01-23 04:49:47 +00:00
|
|
|
),
|
2022-04-13 20:41:48 +00:00
|
|
|
RainMachineSensorDescriptionApiCategory(
|
2021-08-23 17:23:35 +00:00
|
|
|
key=TYPE_FLOW_SENSOR_CONSUMED_LITERS,
|
|
|
|
name="Flow Sensor Consumed Liters",
|
|
|
|
icon="mdi:water-pump",
|
2021-12-16 15:36:16 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-23 17:23:35 +00:00
|
|
|
native_unit_of_measurement="liter",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 15:36:16 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-08-23 17:23:35 +00:00
|
|
|
api_category=DATA_PROVISION_SETTINGS,
|
2020-01-23 04:49:47 +00:00
|
|
|
),
|
2022-04-13 20:41:48 +00:00
|
|
|
RainMachineSensorDescriptionApiCategory(
|
2021-08-23 17:23:35 +00:00
|
|
|
key=TYPE_FLOW_SENSOR_START_INDEX,
|
|
|
|
name="Flow Sensor Start Index",
|
|
|
|
icon="mdi:water-pump",
|
2021-12-16 15:36:16 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-23 17:23:35 +00:00
|
|
|
native_unit_of_measurement="index",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
api_category=DATA_PROVISION_SETTINGS,
|
2020-01-23 04:49:47 +00:00
|
|
|
),
|
2022-04-13 20:41:48 +00:00
|
|
|
RainMachineSensorDescriptionApiCategory(
|
2021-08-23 17:23:35 +00:00
|
|
|
key=TYPE_FLOW_SENSOR_WATERING_CLICKS,
|
|
|
|
name="Flow Sensor Clicks",
|
|
|
|
icon="mdi:water-pump",
|
2021-12-16 15:36:16 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-23 17:23:35 +00:00
|
|
|
native_unit_of_measurement="clicks",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 15:36:16 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-23 17:23:35 +00:00
|
|
|
api_category=DATA_PROVISION_SETTINGS,
|
2020-01-23 04:49:47 +00:00
|
|
|
),
|
2022-04-13 20:41:48 +00:00
|
|
|
RainMachineSensorDescriptionApiCategory(
|
2021-08-23 17:23:35 +00:00
|
|
|
key=TYPE_FREEZE_TEMP,
|
|
|
|
name="Freeze Protect Temperature",
|
|
|
|
icon="mdi:thermometer",
|
2021-12-16 15:36:16 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-23 17:23:35 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-16 15:36:16 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-23 17:23:35 +00:00
|
|
|
api_category=DATA_RESTRICTIONS_UNIVERSAL,
|
2020-01-23 04:49:47 +00:00
|
|
|
),
|
2021-08-23 17:23:35 +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 sensors based on a config entry."""
|
2021-10-22 10:14:58 +00:00
|
|
|
controller = hass.data[DOMAIN][entry.entry_id][DATA_CONTROLLER]
|
|
|
|
coordinators = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
2020-11-06 09:58:50 +00:00
|
|
|
|
|
|
|
@callback
|
2022-04-13 20:41:48 +00:00
|
|
|
def async_get_sensor_by_api_category(api_category: str) -> partial:
|
2020-11-06 09:58:50 +00:00
|
|
|
"""Generate the appropriate sensor object for an API category."""
|
|
|
|
if api_category == DATA_PROVISION_SETTINGS:
|
|
|
|
return partial(
|
|
|
|
ProvisionSettingsSensor,
|
2021-10-17 15:44:48 +00:00
|
|
|
entry,
|
2020-11-06 09:58:50 +00:00
|
|
|
coordinators[DATA_PROVISION_SETTINGS],
|
|
|
|
)
|
|
|
|
|
|
|
|
return partial(
|
|
|
|
UniversalRestrictionsSensor,
|
2021-10-17 15:44:48 +00:00
|
|
|
entry,
|
2020-11-06 09:58:50 +00:00
|
|
|
coordinators[DATA_RESTRICTIONS_UNIVERSAL],
|
|
|
|
)
|
|
|
|
|
2022-04-13 20:41:48 +00:00
|
|
|
sensors = [
|
|
|
|
async_get_sensor_by_api_category(description.api_category)(
|
|
|
|
controller, description
|
|
|
|
)
|
|
|
|
for description in SENSOR_DESCRIPTIONS
|
|
|
|
]
|
|
|
|
|
|
|
|
zone_coordinator = coordinators[DATA_ZONES]
|
|
|
|
for uid, zone in zone_coordinator.data.items():
|
|
|
|
sensors.append(
|
|
|
|
ZoneTimeRemainingSensor(
|
|
|
|
entry,
|
|
|
|
zone_coordinator,
|
|
|
|
controller,
|
|
|
|
RainMachineSensorDescriptionUid(
|
|
|
|
key=f"{TYPE_ZONE_RUN_COMPLETION_TIME}_{uid}",
|
|
|
|
name=f"{zone['name']} Run Completion Time",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
uid=uid,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(sensors)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
|
2021-08-25 14:36:25 +00:00
|
|
|
class ProvisionSettingsSensor(RainMachineEntity, SensorEntity):
|
2020-11-06 09:58:50 +00:00
|
|
|
"""Define a sensor that handles provisioning 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:
|
|
|
|
"""Update the state."""
|
2021-08-25 14:36:25 +00:00
|
|
|
if self.entity_description.key == TYPE_FLOW_SENSOR_CLICK_M3:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self.coordinator.data["system"].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorClicksPerCubicMeter"
|
|
|
|
)
|
2021-08-25 14:36:25 +00:00
|
|
|
elif self.entity_description.key == TYPE_FLOW_SENSOR_CONSUMED_LITERS:
|
2020-11-06 09:58:50 +00:00
|
|
|
clicks = self.coordinator.data["system"].get("flowSensorWateringClicks")
|
|
|
|
clicks_per_m3 = self.coordinator.data["system"].get(
|
|
|
|
"flowSensorClicksPerCubicMeter"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
|
|
|
|
if clicks and clicks_per_m3:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = (clicks * 1000) / clicks_per_m3
|
2019-05-02 07:45:51 +00:00
|
|
|
else:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = None
|
2021-08-25 14:36:25 +00:00
|
|
|
elif self.entity_description.key == TYPE_FLOW_SENSOR_START_INDEX:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self.coordinator.data["system"].get(
|
2021-07-03 16:23:52 +00:00
|
|
|
"flowSensorStartIndex"
|
|
|
|
)
|
2021-08-25 14:36:25 +00:00
|
|
|
elif self.entity_description.key == TYPE_FLOW_SENSOR_WATERING_CLICKS:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self.coordinator.data["system"].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorWateringClicks"
|
|
|
|
)
|
2020-11-06 09:58:50 +00:00
|
|
|
|
|
|
|
|
2021-08-25 14:36:25 +00:00
|
|
|
class UniversalRestrictionsSensor(RainMachineEntity, SensorEntity):
|
2020-11-06 09:58:50 +00:00
|
|
|
"""Define a sensor that handles universal restrictions data."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
|
|
|
"""Update the state."""
|
2021-08-25 14:36:25 +00:00
|
|
|
if self.entity_description.key == TYPE_FREEZE_TEMP:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self.coordinator.data["freezeProtectTemp"]
|
2022-04-13 20:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZoneTimeRemainingSensor(RainMachineEntity, SensorEntity):
|
|
|
|
"""Define a sensor that shows the amount of time remaining for a zone."""
|
|
|
|
|
|
|
|
entity_description: RainMachineSensorDescriptionUid
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
|
|
|
"""Update the state."""
|
|
|
|
data = self.coordinator.data[self.entity_description.uid]
|
|
|
|
now = utcnow()
|
|
|
|
|
|
|
|
if ZONE_STATE_MAP.get(data["state"]) != ZONE_STATE_RUNNING:
|
|
|
|
# If the zone isn't actively running, return immediately:
|
|
|
|
return
|
|
|
|
|
|
|
|
new_timestamp = now + timedelta(seconds=data["remaining"])
|
|
|
|
|
|
|
|
if self._attr_native_value:
|
|
|
|
assert isinstance(self._attr_native_value, datetime)
|
|
|
|
if (
|
|
|
|
new_timestamp - self._attr_native_value
|
|
|
|
) < DEFAULT_ZONE_COMPLETION_TIME_WOBBLE_TOLERANCE:
|
|
|
|
# If the deviation between the previous and new timestamps is less than
|
|
|
|
# a "wobble tolerance," don't spam the state machine:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._attr_native_value = new_timestamp
|