2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Hydrawise sprinkler sensors."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-15 23:59:37 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from pydrawise.schema import Zone
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-14 12:06:55 +00:00
|
|
|
SensorDeviceClass,
|
2021-09-27 15:35:09 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2023-09-23 22:03:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-04-23 16:08:07 +00:00
|
|
|
from homeassistant.const import UnitOfTime
|
2023-10-30 14:18:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-03 18:13:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-05-29 21:01:00 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2023-10-30 14:18:59 +00:00
|
|
|
from .const import DOMAIN
|
2023-05-24 12:07:37 +00:00
|
|
|
from .coordinator import HydrawiseDataUpdateCoordinator
|
2023-05-22 13:47:32 +00:00
|
|
|
from .entity import HydrawiseEntity
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="next_cycle",
|
2023-09-26 12:34:43 +00:00
|
|
|
translation_key="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",
|
2023-09-26 12:34:43 +00:00
|
|
|
translation_key="watering_time",
|
2022-12-20 12:21:14 +00:00
|
|
|
native_unit_of_measurement=UnitOfTime.MINUTES,
|
2021-09-27 15:35:09 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
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
|
|
|
|
2023-09-23 22:03:07 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Hydrawise sensor platform."""
|
|
|
|
coordinator: HydrawiseDataUpdateCoordinator = hass.data[DOMAIN][
|
|
|
|
config_entry.entry_id
|
|
|
|
]
|
2023-11-15 23:59:37 +00:00
|
|
|
async_add_entities(
|
|
|
|
HydrawiseSensor(coordinator, description, controller, zone)
|
2024-01-02 11:46:39 +00:00
|
|
|
for controller in coordinator.data.controllers.values()
|
2023-11-15 23:59:37 +00:00
|
|
|
for zone in controller.zones
|
2021-09-27 15:35:09 +00:00
|
|
|
for description in SENSOR_TYPES
|
2023-11-15 23:59:37 +00:00
|
|
|
)
|
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."""
|
|
|
|
|
2023-11-15 23:59:37 +00:00
|
|
|
zone: Zone
|
|
|
|
|
2023-10-30 14:18:59 +00:00
|
|
|
def _update_attrs(self) -> None:
|
|
|
|
"""Update state attributes."""
|
2021-09-27 15:35:09 +00:00
|
|
|
if self.entity_description.key == "watering_time":
|
2023-11-15 23:59:37 +00:00
|
|
|
if (current_run := self.zone.scheduled_runs.current_run) is not None:
|
|
|
|
self._attr_native_value = int(
|
|
|
|
current_run.remaining_time.total_seconds() / 60
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
else:
|
2021-09-27 15:35:09 +00:00
|
|
|
self._attr_native_value = 0
|
2023-11-15 23:59:37 +00:00
|
|
|
elif self.entity_description.key == "next_cycle":
|
|
|
|
if (next_run := self.zone.scheduled_runs.next_run) is not None:
|
|
|
|
self._attr_native_value = dt_util.as_utc(next_run.start_time)
|
|
|
|
else:
|
|
|
|
self._attr_native_value = datetime.max.replace(tzinfo=dt_util.UTC)
|