-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathbinary_sensor.py
299 lines (270 loc) · 9.77 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""Support for Tuya Binary Sensor."""
from __future__ import annotations
import json
import logging
from threading import Timer
from typing import Callable
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_DOOR,
DEVICE_CLASS_GARAGE_DOOR,
DEVICE_CLASS_GAS,
DEVICE_CLASS_MOISTURE,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_PROBLEM,
DEVICE_CLASS_SMOKE,
)
from homeassistant.components.binary_sensor import DOMAIN as DEVICE_DOMAIN
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import Entity
from tuya_iot import TuyaDevice, TuyaDeviceManager
from .base import TuyaHaEntity
from .const import (
DOMAIN,
TUYA_DEVICE_MANAGER,
TUYA_DISCOVERY_NEW,
TUYA_HA_DEVICES,
TUYA_HA_TUYA_MAP,
)
_LOGGER = logging.getLogger(__name__)
TUYA_SUPPORT_TYPE = [
"mcs", # Door Window Sensor
"ywbj", # Smoke Detector
"rqbj", # Gas Detector
"pir", # PIR Detector
"sj", # Water Detector
"sos", # Emergency Button
"hps", # Human Presence Sensor
"ms", # Residential Lock
"ckmkzq", # Garage Door Opener
]
# Door Window Sensor
# https://developer.tuya.com/en/docs/iot/s?id=K9gf48hm02l8m
DPCODE_SWITCH = "switch"
DPCODE_BATTERY_STATE = "battery_state"
DPCODE_DOORCONTACT_STATE = "doorcontact_state"
DPCODE_SMOKE_SENSOR_STATE = "smoke_sensor_state"
DPCODE_SMOKE_SENSOR_STATUS = "smoke_sensor_status"
DPCODE_GAS_SENSOR_STATE = "gas_sensor_state"
DPCODE_PIR = "pir"
DPCODE_WATER_SENSOR_STATE = "watersensor_state"
DPCODE_SOS_STATE = "sos_state"
DPCODE_PRESENCE_STATE = "presence_state"
DPCODE_TEMPER_ALRAM = "temper_alarm"
DPCODE_DOORLOCK_STATE = "closed_opened"
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up tuya binary sensors dynamically through tuya discovery."""
_LOGGER.debug("binary sensor init")
hass.data[DOMAIN][entry.entry_id][TUYA_HA_TUYA_MAP][
DEVICE_DOMAIN
] = TUYA_SUPPORT_TYPE
@callback
def async_discover_device(dev_ids):
"""Discover and add a discovered tuya sensor."""
_LOGGER.debug(f"binary sensor add->{dev_ids}")
if not dev_ids:
return
entities = _setup_entities(hass, entry, dev_ids)
for entity in entities:
hass.data[DOMAIN][entry.entry_id][TUYA_HA_DEVICES].add(entity._attr_unique_id)
async_add_entities(entities)
entry.async_on_unload(
async_dispatcher_connect(
hass, TUYA_DISCOVERY_NEW.format(DEVICE_DOMAIN), async_discover_device
)
)
device_manager = hass.data[DOMAIN][entry.entry_id][TUYA_DEVICE_MANAGER]
device_ids = []
for (device_id, device) in device_manager.device_map.items():
if device.category in TUYA_SUPPORT_TYPE:
device_ids.append(device_id)
async_discover_device(device_ids)
def _setup_entities(
hass: HomeAssistant, entry: ConfigEntry, device_ids: list[str]
) -> list[Entity]:
"""Set up Tuya Switch device."""
device_manager = hass.data[DOMAIN][entry.entry_id][TUYA_DEVICE_MANAGER]
entities: list[Entity] = []
for device_id in device_ids:
device = device_manager.device_map[device_id]
if device is None:
continue
if DPCODE_DOORLOCK_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_DOOR,
DPCODE_DOORLOCK_STATE,
(lambda d: d.status.get(DPCODE_DOORLOCK_STATE, "none") != "closed"),
)
)
if DPCODE_DOORCONTACT_STATE in device.status:
if device.category == "ckmkzq":
device_class_d = DEVICE_CLASS_GARAGE_DOOR
else:
device_class_d = DEVICE_CLASS_DOOR
entities.append(
TuyaHaBSensor(
device,
device_manager,
device_class_d,
DPCODE_DOORCONTACT_STATE,
(lambda d: d.status.get(DPCODE_DOORCONTACT_STATE, False)),
)
)
if DPCODE_SWITCH in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_DOOR,
DPCODE_SWITCH,
(lambda d: d.status.get(DPCODE_SWITCH, False)),
)
)
if DPCODE_SMOKE_SENSOR_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_SMOKE,
DPCODE_SMOKE_SENSOR_STATE,
(lambda d: d.status.get(DPCODE_SMOKE_SENSOR_STATE, 1) == "1"),
)
)
if DPCODE_SMOKE_SENSOR_STATUS in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_SMOKE,
DPCODE_SMOKE_SENSOR_STATUS,
(
lambda d: d.status.get(DPCODE_SMOKE_SENSOR_STATUS, "normal")
== "alarm"
),
)
)
if DPCODE_BATTERY_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_BATTERY,
DPCODE_BATTERY_STATE,
(lambda d: d.status.get(DPCODE_BATTERY_STATE, "normal") == "low"),
)
)
if DPCODE_TEMPER_ALRAM in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_MOTION,
DPCODE_TEMPER_ALRAM,
(lambda d: d.status.get(DPCODE_TEMPER_ALRAM, False)),
)
)
if DPCODE_GAS_SENSOR_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_GAS,
DPCODE_GAS_SENSOR_STATE,
(lambda d: d.status.get(DPCODE_GAS_SENSOR_STATE, 1) == "1"),
)
)
if DPCODE_PIR in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_MOTION,
DPCODE_PIR,
(lambda d: d.status.get(DPCODE_PIR, "none") == "pir"),
)
)
if DPCODE_WATER_SENSOR_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_MOISTURE,
DPCODE_WATER_SENSOR_STATE,
(
lambda d: d.status.get(DPCODE_WATER_SENSOR_STATE, "normal")
== "alarm"
),
)
)
if DPCODE_SOS_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_PROBLEM,
DPCODE_SOS_STATE,
(lambda d: d.status.get(DPCODE_SOS_STATE, False)),
)
)
if DPCODE_PRESENCE_STATE in device.status:
entities.append(
TuyaHaBSensor(
device,
device_manager,
DEVICE_CLASS_MOTION,
DPCODE_PRESENCE_STATE,
(
lambda d: d.status.get(DPCODE_PRESENCE_STATE, "none")
== "presence"
),
)
)
return entities
class TuyaHaBSensor(TuyaHaEntity, BinarySensorEntity):
"""Tuya Binary Sensor Device."""
def __init__(
self,
device: TuyaDevice,
device_manager: TuyaDeviceManager,
sensor_type: str,
sensor_code: str,
sensor_is_on: Callable[..., bool],
) -> None:
"""Init TuyaHaBSensor."""
super().__init__(device, device_manager)
self._type = sensor_type
self._code = sensor_code
self._is_on = sensor_is_on
self._attr_unique_id = f"{super().unique_id}{self._code}"
self._attr_name = f"{self.tuya_device.name}_{self._code}"
self._attr_device_class = self._type
self._attr_available = True
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._is_on(self.tuya_device)
@property
def unique_id(self) -> str | None:
"""Return a unique ID."""
return self._attr_unique_id
def reset_pir(self):
self.tuya_device.status[DPCODE_PIR] = "none"
self.schedule_update_ha_state()
def schedule_update_ha_state(self, force_refresh: bool = False) -> None:
if self._code == DPCODE_PIR:
pir_range = json.loads(
self.tuya_device.status_range.get(DPCODE_PIR, {}).values
).get("range")
if len(pir_range) == 1 and self.tuya_device.status[DPCODE_PIR] == "pir":
timer = Timer(10, lambda: self.reset_pir())
timer.start()
super().schedule_update_ha_state(force_refresh)