2020-05-26 13:47:25 +00:00
|
|
|
"""Binary sensors for the Elexa Guardian integration."""
|
2021-03-18 07:02:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2020-09-12 18:21:57 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2022-09-17 21:01:57 +00:00
|
|
|
DOMAIN as BINARY_SENSOR_DOMAIN,
|
2021-12-13 14:06:19 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 18:21:57 +00:00
|
|
|
BinarySensorEntity,
|
2021-08-25 08:34:02 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-09-12 18:21:57 +00:00
|
|
|
)
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2020-10-13 03:41:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
from . import (
|
2022-07-22 02:32:42 +00:00
|
|
|
GuardianData,
|
2022-07-18 15:18:07 +00:00
|
|
|
PairedSensorEntity,
|
|
|
|
ValveControllerEntity,
|
|
|
|
ValveControllerEntityDescription,
|
|
|
|
)
|
2020-05-26 13:47:25 +00:00
|
|
|
from .const import (
|
2020-07-05 22:09:40 +00:00
|
|
|
API_SYSTEM_ONBOARD_SENSOR_STATUS,
|
2020-10-13 03:41:57 +00:00
|
|
|
CONF_UID,
|
2020-05-26 13:47:25 +00:00
|
|
|
DOMAIN,
|
2020-10-13 03:41:57 +00:00
|
|
|
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
|
2020-05-26 13:47:25 +00:00
|
|
|
)
|
2022-09-17 21:01:57 +00:00
|
|
|
from .util import (
|
|
|
|
EntityDomainReplacementStrategy,
|
|
|
|
GuardianDataUpdateCoordinator,
|
|
|
|
async_finish_entity_domain_replacements,
|
|
|
|
)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
ATTR_CONNECTED_CLIENTS = "connected_clients"
|
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
SENSOR_KIND_LEAK_DETECTED = "leak_detected"
|
2020-10-13 03:41:57 +00:00
|
|
|
SENSOR_KIND_MOVED = "moved"
|
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ValveControllerBinarySensorDescription(
|
|
|
|
BinarySensorEntityDescription, ValveControllerEntityDescription
|
|
|
|
):
|
|
|
|
"""Describe a Guardian valve controller binary sensor."""
|
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
|
2021-08-25 08:34:02 +00:00
|
|
|
PAIRED_SENSOR_DESCRIPTIONS = (
|
2022-07-18 15:18:07 +00:00
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key=SENSOR_KIND_LEAK_DETECTED,
|
2023-06-27 20:24:41 +00:00
|
|
|
translation_key="leak",
|
2022-07-18 15:18:07 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key=SENSOR_KIND_MOVED,
|
2023-06-27 20:24:41 +00:00
|
|
|
translation_key="moved",
|
2022-07-18 15:18:07 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOVING,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
2021-08-25 08:34:02 +00:00
|
|
|
)
|
2022-07-18 15:18:07 +00:00
|
|
|
|
2021-08-25 08:34:02 +00:00
|
|
|
VALVE_CONTROLLER_DESCRIPTIONS = (
|
2022-07-18 15:18:07 +00:00
|
|
|
ValveControllerBinarySensorDescription(
|
|
|
|
key=SENSOR_KIND_LEAK_DETECTED,
|
2023-06-27 20:24:41 +00:00
|
|
|
translation_key="leak",
|
2022-07-18 15:18:07 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
|
|
|
api_category=API_SYSTEM_ONBOARD_SENSOR_STATUS,
|
|
|
|
),
|
2021-08-25 08:34:02 +00:00
|
|
|
)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-29 10:28:14 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-07-05 22:09:40 +00:00
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Set up Guardian switches based on a config entry."""
|
2022-07-22 02:32:42 +00:00
|
|
|
data: GuardianData = hass.data[DOMAIN][entry.entry_id]
|
2022-09-17 21:01:57 +00:00
|
|
|
uid = entry.data[CONF_UID]
|
|
|
|
|
|
|
|
async_finish_entity_domain_replacements(
|
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
(
|
|
|
|
EntityDomainReplacementStrategy(
|
|
|
|
BINARY_SENSOR_DOMAIN,
|
|
|
|
f"{uid}_ap_enabled",
|
|
|
|
f"switch.guardian_valve_controller_{uid}_onboard_ap",
|
|
|
|
"2022.12.0",
|
2022-10-27 08:19:07 +00:00
|
|
|
remove_old_entity=True,
|
2022-09-17 21:01:57 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2020-10-13 03:41:57 +00:00
|
|
|
|
2020-10-13 20:01:10 +00:00
|
|
|
@callback
|
|
|
|
def add_new_paired_sensor(uid: str) -> None:
|
2020-10-13 03:41:57 +00:00
|
|
|
"""Add a new paired sensor."""
|
2021-08-25 08:34:02 +00:00
|
|
|
async_add_entities(
|
2022-07-18 15:18:07 +00:00
|
|
|
PairedSensorBinarySensor(
|
2022-07-22 02:32:42 +00:00
|
|
|
entry, data.paired_sensor_manager.coordinators[uid], description
|
2022-07-18 15:18:07 +00:00
|
|
|
)
|
|
|
|
for description in PAIRED_SENSOR_DESCRIPTIONS
|
2021-08-25 08:34:02 +00:00
|
|
|
)
|
2020-10-13 03:41:57 +00:00
|
|
|
|
|
|
|
# Handle adding paired sensors after HASS startup:
|
2021-10-22 10:25:05 +00:00
|
|
|
entry.async_on_unload(
|
2020-10-13 03:41:57 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]),
|
|
|
|
add_new_paired_sensor,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add all valve controller-specific binary sensors:
|
2021-08-25 08:34:02 +00:00
|
|
|
sensors: list[PairedSensorBinarySensor | ValveControllerBinarySensor] = [
|
2022-07-22 02:32:42 +00:00
|
|
|
ValveControllerBinarySensor(
|
|
|
|
entry, data.valve_controller_coordinators, description
|
|
|
|
)
|
2021-08-25 08:34:02 +00:00
|
|
|
for description in VALVE_CONTROLLER_DESCRIPTIONS
|
|
|
|
]
|
2020-10-13 03:41:57 +00:00
|
|
|
|
|
|
|
# Add all paired sensor-specific binary sensors:
|
2021-08-25 08:34:02 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
PairedSensorBinarySensor(entry, coordinator, description)
|
2022-07-22 02:32:42 +00:00
|
|
|
for coordinator in data.paired_sensor_manager.coordinators.values()
|
2021-08-25 08:34:02 +00:00
|
|
|
for description in PAIRED_SENSOR_DESCRIPTIONS
|
|
|
|
]
|
|
|
|
)
|
2020-10-13 03:41:57 +00:00
|
|
|
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
|
|
|
|
|
|
class PairedSensorBinarySensor(PairedSensorEntity, BinarySensorEntity):
|
|
|
|
"""Define a binary sensor related to a Guardian valve controller."""
|
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
entity_description: BinarySensorEntityDescription
|
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entry: ConfigEntry,
|
2022-07-18 15:18:07 +00:00
|
|
|
coordinator: GuardianDataUpdateCoordinator,
|
2021-08-25 08:34:02 +00:00
|
|
|
description: BinarySensorEntityDescription,
|
2020-10-13 03:41:57 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
2021-08-25 08:34:02 +00:00
|
|
|
super().__init__(entry, coordinator, description)
|
2020-10-13 03:41:57 +00:00
|
|
|
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = True
|
2020-10-13 03:41:57 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_from_latest_data(self) -> None:
|
2022-07-31 20:10:29 +00:00
|
|
|
"""Update the entity's underlying data."""
|
2021-08-25 08:34:02 +00:00
|
|
|
if self.entity_description.key == SENSOR_KIND_LEAK_DETECTED:
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = self.coordinator.data["wet"]
|
2021-08-25 08:34:02 +00:00
|
|
|
elif self.entity_description.key == SENSOR_KIND_MOVED:
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = self.coordinator.data["moved"]
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
class ValveControllerBinarySensor(ValveControllerEntity, BinarySensorEntity):
|
|
|
|
"""Define a binary sensor related to a Guardian valve controller."""
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
entity_description: ValveControllerBinarySensorDescription
|
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entry: ConfigEntry,
|
2022-07-18 15:18:07 +00:00
|
|
|
coordinators: dict[str, GuardianDataUpdateCoordinator],
|
|
|
|
description: ValveControllerBinarySensorDescription,
|
2020-07-05 22:09:40 +00:00
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Initialize."""
|
2021-08-25 08:34:02 +00:00
|
|
|
super().__init__(entry, coordinators, description)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = True
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
@callback
|
2020-07-05 22:09:40 +00:00
|
|
|
def _async_update_from_latest_data(self) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Update the entity."""
|
2022-10-27 08:19:07 +00:00
|
|
|
if self.entity_description.key == SENSOR_KIND_LEAK_DETECTED:
|
2022-07-18 15:18:07 +00:00
|
|
|
self._attr_is_on = self.coordinator.data["wet"]
|