-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathbinary_sensor.py
243 lines (213 loc) · 7.73 KB
/
binary_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""Support for deCONZ binary sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from pydeconz.interfaces.sensors import SensorResources
from pydeconz.models.event import EventType
from pydeconz.models.sensor import SensorBase as PydeconzSensorBase
from pydeconz.models.sensor.alarm import Alarm
from pydeconz.models.sensor.carbon_monoxide import CarbonMonoxide
from pydeconz.models.sensor.fire import Fire
from pydeconz.models.sensor.generic_flag import GenericFlag
from pydeconz.models.sensor.open_close import OpenClose
from pydeconz.models.sensor.presence import Presence
from pydeconz.models.sensor.vibration import Vibration
from pydeconz.models.sensor.water import Water
from homeassistant.components.binary_sensor import (
DOMAIN as BINARY_SENSOR_DOMAIN,
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ATTR_DARK, ATTR_ON
from .entity import DeconzDevice
from .hub import DeconzHub
ATTR_ORIENTATION = "orientation"
ATTR_TILTANGLE = "tiltangle"
ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
PROVIDES_EXTRA_ATTRIBUTES = (
"alarm",
"carbon_monoxide",
"fire",
"flag",
"open",
"presence",
"vibration",
"water",
)
@dataclass(frozen=True, kw_only=True)
class DeconzBinarySensorDescription[
_T: (
Alarm,
CarbonMonoxide,
Fire,
GenericFlag,
OpenClose,
Presence,
Vibration,
Water,
PydeconzSensorBase,
)
](BinarySensorEntityDescription):
"""Class describing deCONZ binary sensor entities."""
instance_check: type[_T] | None = None
name_suffix: str = ""
old_unique_id_suffix: str = ""
update_key: str
value_fn: Callable[[_T], bool | None]
ENTITY_DESCRIPTIONS: tuple[DeconzBinarySensorDescription, ...] = (
DeconzBinarySensorDescription[Alarm](
key="alarm",
update_key="alarm",
value_fn=lambda device: device.alarm,
instance_check=Alarm,
device_class=BinarySensorDeviceClass.SAFETY,
),
DeconzBinarySensorDescription[CarbonMonoxide](
key="carbon_monoxide",
update_key="carbonmonoxide",
value_fn=lambda device: device.carbon_monoxide,
instance_check=CarbonMonoxide,
device_class=BinarySensorDeviceClass.CO,
),
DeconzBinarySensorDescription[Fire](
key="fire",
update_key="fire",
value_fn=lambda device: device.fire,
instance_check=Fire,
device_class=BinarySensorDeviceClass.SMOKE,
),
DeconzBinarySensorDescription[Fire](
key="in_test_mode",
update_key="test",
value_fn=lambda device: device.in_test_mode,
instance_check=Fire,
name_suffix="Test Mode",
old_unique_id_suffix="test mode",
device_class=BinarySensorDeviceClass.SMOKE,
entity_category=EntityCategory.DIAGNOSTIC,
),
DeconzBinarySensorDescription[GenericFlag](
key="flag",
update_key="flag",
value_fn=lambda device: device.flag,
instance_check=GenericFlag,
),
DeconzBinarySensorDescription[OpenClose](
key="open",
update_key="open",
value_fn=lambda device: device.open,
instance_check=OpenClose,
device_class=BinarySensorDeviceClass.OPENING,
),
DeconzBinarySensorDescription[Presence](
key="presence",
update_key="presence",
value_fn=lambda device: device.presence,
instance_check=Presence,
device_class=BinarySensorDeviceClass.MOTION,
),
DeconzBinarySensorDescription[Vibration](
key="vibration",
update_key="vibration",
value_fn=lambda device: device.vibration,
instance_check=Vibration,
device_class=BinarySensorDeviceClass.VIBRATION,
),
DeconzBinarySensorDescription[Water](
key="water",
update_key="water",
value_fn=lambda device: device.water,
instance_check=Water,
device_class=BinarySensorDeviceClass.MOISTURE,
),
DeconzBinarySensorDescription[SensorResources](
key="tampered",
update_key="tampered",
value_fn=lambda device: device.tampered,
name_suffix="Tampered",
old_unique_id_suffix="tampered",
device_class=BinarySensorDeviceClass.TAMPER,
entity_category=EntityCategory.DIAGNOSTIC,
),
DeconzBinarySensorDescription[SensorResources](
key="low_battery",
update_key="lowbattery",
value_fn=lambda device: device.low_battery,
name_suffix="Low Battery",
old_unique_id_suffix="low battery",
device_class=BinarySensorDeviceClass.BATTERY,
entity_category=EntityCategory.DIAGNOSTIC,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the deCONZ binary sensor."""
hub = DeconzHub.get_hub(hass, config_entry)
hub.entities[BINARY_SENSOR_DOMAIN] = set()
@callback
def async_add_sensor(_: EventType, sensor_id: str) -> None:
"""Add sensor from deCONZ."""
sensor = hub.api.sensors[sensor_id]
for description in ENTITY_DESCRIPTIONS:
if (
description.instance_check
and not isinstance(sensor, description.instance_check)
) or description.value_fn(sensor) is None:
continue
async_add_entities([DeconzBinarySensor(sensor, hub, description)])
hub.register_platform_add_device_callback(
async_add_sensor,
hub.api.sensors,
)
class DeconzBinarySensor(DeconzDevice[SensorResources], BinarySensorEntity):
"""Representation of a deCONZ binary sensor."""
TYPE = BINARY_SENSOR_DOMAIN
entity_description: DeconzBinarySensorDescription
def __init__(
self,
device: SensorResources,
hub: DeconzHub,
description: DeconzBinarySensorDescription,
) -> None:
"""Initialize deCONZ binary sensor."""
self.entity_description = description
self.unique_id_suffix = description.key
self._update_key = description.update_key
if description.name_suffix:
self._name_suffix = description.name_suffix
super().__init__(device, hub)
if (
self.entity_description.key in PROVIDES_EXTRA_ATTRIBUTES
and self._update_keys is not None
):
self._update_keys.update({"on", "state"})
@property
def is_on(self) -> bool | None:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self._device)
@property
def extra_state_attributes(self) -> dict[str, bool | float | int | list | None]:
"""Return the state attributes of the sensor."""
attr: dict[str, bool | float | int | list | None] = {}
if self.entity_description.key not in PROVIDES_EXTRA_ATTRIBUTES:
return attr
if self._device.on is not None:
attr[ATTR_ON] = self._device.on
if self._device.internal_temperature is not None:
attr[ATTR_TEMPERATURE] = self._device.internal_temperature
if isinstance(self._device, Presence):
if self._device.dark is not None:
attr[ATTR_DARK] = self._device.dark
elif isinstance(self._device, Vibration):
attr[ATTR_ORIENTATION] = self._device.orientation
attr[ATTR_TILTANGLE] = self._device.tilt_angle
attr[ATTR_VIBRATIONSTRENGTH] = self._device.vibration_strength
return attr