1 """Binary sensors for the Elexa Guardian integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
10 DOMAIN
as BINARY_SENSOR_DOMAIN,
11 BinarySensorDeviceClass,
13 BinarySensorEntityDescription,
21 from .
import GuardianData
23 API_SYSTEM_ONBOARD_SENSOR_STATUS,
26 SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
28 from .coordinator
import GuardianDataUpdateCoordinator
31 ValveControllerEntity,
32 ValveControllerEntityDescription,
35 EntityDomainReplacementStrategy,
36 async_finish_entity_domain_replacements,
39 ATTR_CONNECTED_CLIENTS =
"connected_clients"
41 SENSOR_KIND_LEAK_DETECTED =
"leak_detected"
42 SENSOR_KIND_MOVED =
"moved"
45 @dataclass(frozen=True, kw_only=True)
47 """Describe a Guardian paired sensor binary sensor."""
49 is_on_fn: Callable[[dict[str, Any]], bool]
52 @dataclass(frozen=True, kw_only=True)
54 BinarySensorEntityDescription, ValveControllerEntityDescription
56 """Describe a Guardian valve controller binary sensor."""
58 is_on_fn: Callable[[dict[str, Any]], bool]
61 PAIRED_SENSOR_DESCRIPTIONS = (
63 key=SENSOR_KIND_LEAK_DETECTED,
64 translation_key=
"leak",
65 device_class=BinarySensorDeviceClass.MOISTURE,
66 is_on_fn=
lambda data: data[
"wet"],
69 key=SENSOR_KIND_MOVED,
70 translation_key=
"moved",
71 device_class=BinarySensorDeviceClass.MOVING,
72 entity_category=EntityCategory.DIAGNOSTIC,
73 is_on_fn=
lambda data: data[
"moved"],
77 VALVE_CONTROLLER_DESCRIPTIONS = (
79 key=SENSOR_KIND_LEAK_DETECTED,
80 translation_key=
"leak",
81 device_class=BinarySensorDeviceClass.MOISTURE,
82 api_category=API_SYSTEM_ONBOARD_SENSOR_STATUS,
83 is_on_fn=
lambda data: data[
"wet"],
89 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
91 """Set up Guardian switches based on a config entry."""
92 data: GuardianData = hass.data[DOMAIN][entry.entry_id]
93 uid = entry.data[CONF_UID]
100 BINARY_SENSOR_DOMAIN,
107 def add_new_paired_sensor(uid: str) ->
None:
108 """Add a new paired sensor."""
111 entry, data.paired_sensor_manager.coordinators[uid], description
113 for description
in PAIRED_SENSOR_DESCRIPTIONS
117 entry.async_on_unload(
120 SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]),
121 add_new_paired_sensor,
126 sensors: list[PairedSensorBinarySensor | ValveControllerBinarySensor] = [
128 entry, data.valve_controller_coordinators, description
130 for description
in VALVE_CONTROLLER_DESCRIPTIONS
137 for coordinator
in data.paired_sensor_manager.coordinators.values()
138 for description
in PAIRED_SENSOR_DESCRIPTIONS
146 """Define a binary sensor related to a Guardian valve controller."""
148 entity_description: PairedSensorBinarySensorDescription
153 coordinator: GuardianDataUpdateCoordinator,
154 description: BinarySensorEntityDescription,
157 super().
__init__(entry, coordinator, description)
163 """Return true if the binary sensor is on."""
168 """Define a binary sensor related to a Guardian valve controller."""
170 entity_description: ValveControllerBinarySensorDescription
175 coordinators: dict[str, GuardianDataUpdateCoordinator],
176 description: ValveControllerBinarySensorDescription,
179 super().
__init__(entry, coordinators, description)
185 """Return true if the binary sensor is on."""
None __init__(self, ConfigEntry entry, GuardianDataUpdateCoordinator coordinator, BinarySensorEntityDescription description)
None __init__(self, ConfigEntry entry, dict[str, GuardianDataUpdateCoordinator] coordinators, ValveControllerBinarySensorDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None async_finish_entity_domain_replacements(HomeAssistant hass, ConfigEntry entry, Iterable[EntityDomainReplacementStrategy] entity_replacement_strategies)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)