1 """YoLink BinarySensor."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
9 from yolink.const
import (
10 ATTR_DEVICE_CO_SMOKE_SENSOR,
11 ATTR_DEVICE_DOOR_SENSOR,
12 ATTR_DEVICE_LEAK_SENSOR,
13 ATTR_DEVICE_MOTION_SENSOR,
14 ATTR_DEVICE_VIBRATION_SENSOR,
16 from yolink.device
import YoLinkDevice
19 BinarySensorDeviceClass,
21 BinarySensorEntityDescription,
27 from .const
import DOMAIN
28 from .coordinator
import YoLinkCoordinator
29 from .entity
import YoLinkEntity
32 @dataclass(frozen=True)
34 """YoLink BinarySensorEntityDescription."""
36 exists_fn: Callable[[YoLinkDevice], bool] =
lambda _:
True
37 state_key: str =
"state"
38 value: Callable[[Any], bool |
None] =
lambda _:
None
41 SENSOR_DEVICE_TYPE = [
42 ATTR_DEVICE_DOOR_SENSOR,
43 ATTR_DEVICE_MOTION_SENSOR,
44 ATTR_DEVICE_LEAK_SENSOR,
45 ATTR_DEVICE_VIBRATION_SENSOR,
46 ATTR_DEVICE_CO_SMOKE_SENSOR,
50 SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = (
53 device_class=BinarySensorDeviceClass.DOOR,
54 value=
lambda value: value ==
"open" if value
is not None else None,
55 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_DOOR_SENSOR,
59 device_class=BinarySensorDeviceClass.MOTION,
60 value=
lambda value: value ==
"alert" if value
is not None else None,
61 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MOTION_SENSOR,
65 device_class=BinarySensorDeviceClass.MOISTURE,
66 value=
lambda value: value
in (
"alert",
"full")
if value
is not None else None,
67 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_LEAK_SENSOR,
70 key=
"vibration_state",
71 device_class=BinarySensorDeviceClass.VIBRATION,
72 value=
lambda value: value ==
"alert" if value
is not None else None,
73 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_VIBRATION_SENSOR,
77 device_class=BinarySensorDeviceClass.CO,
78 value=
lambda state: state.get(
"gasAlarm"),
79 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_CO_SMOKE_SENSOR,
83 device_class=BinarySensorDeviceClass.SMOKE,
84 value=
lambda state: state.get(
"smokeAlarm"),
85 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_CO_SMOKE_SENSOR,
92 config_entry: ConfigEntry,
93 async_add_entities: AddEntitiesCallback,
95 """Set up YoLink Sensor from a config entry."""
96 device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
97 binary_sensor_device_coordinators = [
99 for device_coordinator
in device_coordinators.values()
100 if device_coordinator.device.device_type
in SENSOR_DEVICE_TYPE
104 config_entry, binary_sensor_device_coordinator, description
106 for binary_sensor_device_coordinator
in binary_sensor_device_coordinators
107 for description
in SENSOR_TYPES
108 if description.exists_fn(binary_sensor_device_coordinator.device)
113 """YoLink Sensor Entity."""
115 entity_description: YoLinkBinarySensorEntityDescription
119 config_entry: ConfigEntry,
120 coordinator: YoLinkCoordinator,
121 description: YoLinkBinarySensorEntityDescription,
123 """Init YoLink Sensor."""
124 super().
__init__(config_entry, coordinator)
127 f
"{coordinator.device.device_id} {self.entity_description.key}"
132 """Update HA Entity State."""
140 """Return true is device is available."""
141 return super().available
and self.coordinator.dev_online
None __init__(self, ConfigEntry config_entry, YoLinkCoordinator coordinator, YoLinkBinarySensorEntityDescription description)
None update_entity_state(self, dict[str, Any] state)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)