2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Hydrawise sprinkler sensors."""
|
2021-09-27 15:35:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-05-26 16:42:52 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2021-12-14 12:06:55 +00:00
|
|
|
SensorDeviceClass,
|
2021-09-27 15:35:09 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2021-12-14 12:06:55 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS, TIME_MINUTES
|
2022-01-03 18:13:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:13:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2020-06-21 21:55:47 +00:00
|
|
|
from homeassistant.util import dt
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from . import DATA_HYDRAWISE, HydrawiseEntity
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="next_cycle",
|
|
|
|
name="Next Cycle",
|
2021-12-14 12:06:55 +00:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2021-09-27 15:35:09 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="watering_time",
|
|
|
|
name="Watering Time",
|
|
|
|
icon="mdi:water-pump",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
2021-09-27 15:35:09 +00:00
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2020-06-21 21:55:47 +00:00
|
|
|
TWO_YEAR_SECONDS = 60 * 60 * 24 * 365 * 2
|
|
|
|
WATERING_TIME_ICON = "mdi:water-pump"
|
|
|
|
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2022-01-03 18:13:59 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-05-26 16:42:52 +00:00
|
|
|
"""Set up a sensor for a Hydrawise device."""
|
|
|
|
hydrawise = hass.data[DATA_HYDRAWISE].data
|
2021-09-27 15:35:09 +00:00
|
|
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
entities = [
|
|
|
|
HydrawiseSensor(zone, description)
|
|
|
|
for zone in hydrawise.relays
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in monitored_conditions
|
|
|
|
]
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
add_entities(entities, True)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class HydrawiseSensor(HydrawiseEntity, SensorEntity):
|
2018-05-26 16:42:52 +00:00
|
|
|
"""A sensor implementation for Hydrawise device."""
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
|
|
|
mydata = self.hass.data[DATA_HYDRAWISE].data
|
2021-09-27 15:35:09 +00:00
|
|
|
_LOGGER.debug("Updating Hydrawise sensor: %s", self.name)
|
2020-06-21 21:55:47 +00:00
|
|
|
relay_data = mydata.relays[self.data["relay"] - 1]
|
2021-09-27 15:35:09 +00:00
|
|
|
if self.entity_description.key == "watering_time":
|
2020-06-21 21:55:47 +00:00
|
|
|
if relay_data["timestr"] == "Now":
|
2021-09-27 15:35:09 +00:00
|
|
|
self._attr_native_value = int(relay_data["run"] / 60)
|
2018-05-26 16:42:52 +00:00
|
|
|
else:
|
2021-09-27 15:35:09 +00:00
|
|
|
self._attr_native_value = 0
|
2018-05-26 16:42:52 +00:00
|
|
|
else: # _sensor_type == 'next_cycle'
|
2020-06-21 21:55:47 +00:00
|
|
|
next_cycle = min(relay_data["time"], TWO_YEAR_SECONDS)
|
|
|
|
_LOGGER.debug("New cycle time: %s", next_cycle)
|
2021-09-27 15:35:09 +00:00
|
|
|
self._attr_native_value = dt.utc_from_timestamp(
|
2020-06-21 21:55:47 +00:00
|
|
|
dt.as_timestamp(dt.now()) + next_cycle
|
2021-12-08 06:56:22 +00:00
|
|
|
)
|