2023-03-03 11:26:13 +01:00
|
|
|
"""Binary sensors for key RainMachine data."""
|
2024-03-08 15:05:07 +01:00
|
|
|
|
2021-08-25 08:36:25 -06:00
|
|
|
from dataclasses import dataclass
|
2020-11-06 02:58:50 -07:00
|
|
|
|
2021-08-25 08:36:25 -06:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2022-09-23 17:05:07 -06:00
|
|
|
DOMAIN as BINARY_SENSOR_DOMAIN,
|
2021-08-25 08:36:25 -06:00
|
|
|
BinarySensorEntity,
|
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
|
)
|
2023-02-09 20:15:37 +01:00
|
|
|
from homeassistant.const import EntityCategory
|
2020-11-06 02:58:50 -07:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2025-02-10 21:08:03 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2024-09-23 12:42:22 +02:00
|
|
|
from . import RainMachineConfigEntry
|
2024-07-24 09:52:14 -05:00
|
|
|
from .const import DATA_PROVISION_SETTINGS, DATA_RESTRICTIONS_CURRENT
|
2024-09-23 12:42:22 +02:00
|
|
|
from .entity import RainMachineEntity, RainMachineEntityDescription
|
2022-09-23 17:05:07 -06:00
|
|
|
from .util import (
|
|
|
|
|
EntityDomainReplacementStrategy,
|
|
|
|
|
async_finish_entity_domain_replacements,
|
|
|
|
|
key_exists,
|
|
|
|
|
)
|
2018-05-29 13:02:16 -06:00
|
|
|
|
2020-01-22 21:49:47 -07:00
|
|
|
TYPE_FLOW_SENSOR = "flow_sensor"
|
|
|
|
|
TYPE_FREEZE = "freeze"
|
|
|
|
|
TYPE_HOURLY = "hourly"
|
|
|
|
|
TYPE_MONTH = "month"
|
|
|
|
|
TYPE_RAINDELAY = "raindelay"
|
|
|
|
|
TYPE_RAINSENSOR = "rainsensor"
|
|
|
|
|
TYPE_WEEKDAY = "weekday"
|
|
|
|
|
|
2021-08-25 08:36:25 -06:00
|
|
|
|
2024-01-16 18:04:32 -07:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
2021-08-25 08:36:25 -06:00
|
|
|
class RainMachineBinarySensorDescription(
|
2024-01-16 18:04:32 -07:00
|
|
|
BinarySensorEntityDescription, RainMachineEntityDescription
|
2021-08-25 08:36:25 -06:00
|
|
|
):
|
|
|
|
|
"""Describe a RainMachine binary sensor."""
|
|
|
|
|
|
2024-01-16 18:04:32 -07:00
|
|
|
data_key: str
|
|
|
|
|
|
2021-08-25 08:36:25 -06:00
|
|
|
|
|
|
|
|
BINARY_SENSOR_DESCRIPTIONS = (
|
|
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_FLOW_SENSOR,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_FLOW_SENSOR,
|
2021-08-25 08:36:25 -06:00
|
|
|
api_category=DATA_PROVISION_SETTINGS,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="useFlowSensor",
|
2021-08-25 08:36:25 -06:00
|
|
|
),
|
|
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_FREEZE,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_FREEZE,
|
2021-12-16 10:36:16 -05:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-25 08:36:25 -06:00
|
|
|
api_category=DATA_RESTRICTIONS_CURRENT,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="freeze",
|
2021-08-25 08:36:25 -06:00
|
|
|
),
|
|
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_HOURLY,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_HOURLY,
|
2021-12-16 10:36:16 -05:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-25 08:36:25 -06:00
|
|
|
api_category=DATA_RESTRICTIONS_CURRENT,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="hourly",
|
2020-01-25 20:27:35 -07:00
|
|
|
),
|
2021-08-25 08:36:25 -06:00
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_MONTH,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_MONTH,
|
2021-12-16 10:36:16 -05:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-25 08:36:25 -06:00
|
|
|
api_category=DATA_RESTRICTIONS_CURRENT,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="month",
|
2020-01-24 22:31:14 -07:00
|
|
|
),
|
2021-08-25 08:36:25 -06:00
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_RAINDELAY,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_RAINDELAY,
|
2021-12-16 10:36:16 -05:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-25 08:36:25 -06:00
|
|
|
api_category=DATA_RESTRICTIONS_CURRENT,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="rainDelay",
|
2020-01-25 20:27:35 -07:00
|
|
|
),
|
2021-08-25 08:36:25 -06:00
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_RAINSENSOR,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_RAINSENSOR,
|
2021-12-16 10:36:16 -05:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-25 08:36:25 -06:00
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
|
api_category=DATA_RESTRICTIONS_CURRENT,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="rainSensor",
|
2020-01-24 22:31:14 -07:00
|
|
|
),
|
2021-08-25 08:36:25 -06:00
|
|
|
RainMachineBinarySensorDescription(
|
|
|
|
|
key=TYPE_WEEKDAY,
|
2023-07-07 18:27:44 +02:00
|
|
|
translation_key=TYPE_WEEKDAY,
|
2021-12-16 10:36:16 -05:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-25 08:36:25 -06:00
|
|
|
api_category=DATA_RESTRICTIONS_CURRENT,
|
2022-05-31 13:09:07 -06:00
|
|
|
data_key="weekDay",
|
2021-08-25 08:36:25 -06:00
|
|
|
),
|
|
|
|
|
)
|
2020-01-22 21:49:47 -07:00
|
|
|
|
2018-05-29 13:02:16 -06:00
|
|
|
|
2020-11-06 02:58:50 -07:00
|
|
|
async def async_setup_entry(
|
2024-07-24 09:52:14 -05:00
|
|
|
hass: HomeAssistant,
|
|
|
|
|
entry: RainMachineConfigEntry,
|
2025-02-10 21:08:03 +01:00
|
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
2020-11-06 02:58:50 -07:00
|
|
|
) -> None:
|
2018-11-14 13:23:49 -07:00
|
|
|
"""Set up RainMachine binary sensors based on a config entry."""
|
2024-07-24 09:52:14 -05:00
|
|
|
data = entry.runtime_data
|
2020-11-06 02:58:50 -07:00
|
|
|
|
2022-09-23 17:05:07 -06:00
|
|
|
async_finish_entity_domain_replacements(
|
|
|
|
|
hass,
|
|
|
|
|
entry,
|
|
|
|
|
(
|
|
|
|
|
EntityDomainReplacementStrategy(
|
|
|
|
|
BINARY_SENSOR_DOMAIN,
|
|
|
|
|
f"{data.controller.mac}_freeze_protection",
|
|
|
|
|
f"switch.{data.controller.name.lower()}_freeze_protect_enabled",
|
|
|
|
|
breaks_in_ha_version="2022.12.0",
|
2022-10-27 02:25:07 -06:00
|
|
|
remove_old_entity=True,
|
2022-09-23 17:05:07 -06:00
|
|
|
),
|
|
|
|
|
EntityDomainReplacementStrategy(
|
|
|
|
|
BINARY_SENSOR_DOMAIN,
|
|
|
|
|
f"{data.controller.mac}_extra_water_on_hot_days",
|
|
|
|
|
f"switch.{data.controller.name.lower()}_hot_days_extra_watering",
|
|
|
|
|
breaks_in_ha_version="2022.12.0",
|
2022-10-27 02:25:07 -06:00
|
|
|
remove_old_entity=True,
|
2022-09-23 17:05:07 -06:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-31 13:09:07 -06:00
|
|
|
api_category_sensor_map = {
|
|
|
|
|
DATA_PROVISION_SETTINGS: ProvisionSettingsBinarySensor,
|
|
|
|
|
DATA_RESTRICTIONS_CURRENT: CurrentRestrictionsBinarySensor,
|
|
|
|
|
}
|
2020-11-06 02:58:50 -07:00
|
|
|
|
2020-01-24 22:31:14 -07:00
|
|
|
async_add_entities(
|
2024-07-24 09:52:14 -05:00
|
|
|
api_category_sensor_map[description.api_category](entry, data, description)
|
|
|
|
|
for description in BINARY_SENSOR_DESCRIPTIONS
|
|
|
|
|
if (
|
|
|
|
|
(coordinator := data.coordinators[description.api_category]) is not None
|
|
|
|
|
and coordinator.data
|
|
|
|
|
and key_exists(coordinator.data, description.data_key)
|
|
|
|
|
)
|
2020-01-24 22:31:14 -07:00
|
|
|
)
|
2018-05-29 13:02:16 -06:00
|
|
|
|
|
|
|
|
|
2021-08-25 08:36:25 -06:00
|
|
|
class CurrentRestrictionsBinarySensor(RainMachineEntity, BinarySensorEntity):
|
2020-11-06 02:58:50 -07:00
|
|
|
"""Define a binary sensor that handles current restrictions data."""
|
2020-03-17 05:00:54 -06:00
|
|
|
|
2022-08-03 16:23:42 -06:00
|
|
|
entity_description: RainMachineBinarySensorDescription
|
|
|
|
|
|
2020-03-17 05:00:54 -06:00
|
|
|
@callback
|
2020-11-06 02:58:50 -07:00
|
|
|
def update_from_latest_data(self) -> None:
|
2018-05-29 13:02:16 -06:00
|
|
|
"""Update the state."""
|
2021-08-25 08:36:25 -06:00
|
|
|
if self.entity_description.key == TYPE_FREEZE:
|
2022-05-30 15:36:58 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("freeze")
|
2021-08-25 08:36:25 -06:00
|
|
|
elif self.entity_description.key == TYPE_HOURLY:
|
2022-05-30 15:36:58 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("hourly")
|
2021-08-25 08:36:25 -06:00
|
|
|
elif self.entity_description.key == TYPE_MONTH:
|
2022-05-30 15:36:58 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("month")
|
2021-08-25 08:36:25 -06:00
|
|
|
elif self.entity_description.key == TYPE_RAINDELAY:
|
2022-05-30 15:36:58 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("rainDelay")
|
2021-08-25 08:36:25 -06:00
|
|
|
elif self.entity_description.key == TYPE_RAINSENSOR:
|
2022-05-30 15:36:58 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("rainSensor")
|
2021-08-25 08:36:25 -06:00
|
|
|
elif self.entity_description.key == TYPE_WEEKDAY:
|
2022-05-30 15:36:58 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("weekDay")
|
2020-11-06 02:58:50 -07:00
|
|
|
|
|
|
|
|
|
2021-08-25 08:36:25 -06:00
|
|
|
class ProvisionSettingsBinarySensor(RainMachineEntity, BinarySensorEntity):
|
2020-11-06 02:58:50 -07:00
|
|
|
"""Define a binary sensor that handles provisioning data."""
|
|
|
|
|
|
2022-08-03 16:23:42 -06:00
|
|
|
entity_description: RainMachineBinarySensorDescription
|
|
|
|
|
|
2020-11-06 02:58:50 -07:00
|
|
|
@callback
|
|
|
|
|
def update_from_latest_data(self) -> None:
|
|
|
|
|
"""Update the state."""
|
2021-08-25 08:36:25 -06:00
|
|
|
if self.entity_description.key == TYPE_FLOW_SENSOR:
|
2022-09-08 14:41:09 -06:00
|
|
|
self._attr_is_on = self.coordinator.data.get("system", {}).get(
|
|
|
|
|
"useFlowSensor"
|
|
|
|
|
)
|