-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
458 lines (423 loc) · 15.4 KB
/
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""The Tuya BLE integration."""
from __future__ import annotations
from dataclasses import dataclass, field
import logging
from typing import Callable
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
#TEMP_CELSIUS,
#UnitOfTemperature.CELSIUS,
#VOLUME_MILLILITERS,
#UnitOfVolume.MILLILITERS,
UnitOfVolume,
UnitOfTemperature,
UnitOfTime
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import (
BATTERY_STATE_HIGH,
BATTERY_STATE_LOW,
BATTERY_STATE_NORMAL,
BATTERY_CHARGED,
BATTERY_CHARGING,
BATTERY_NOT_CHARGING,
CO2_LEVEL_ALARM,
CO2_LEVEL_NORMAL,
DOMAIN,
)
from .devices import TuyaBLEData, TuyaBLEEntity, TuyaBLEProductInfo
from .tuya_ble import TuyaBLEDataPointType, TuyaBLEDevice
_LOGGER = logging.getLogger(__name__)
SIGNAL_STRENGTH_DP_ID = -1
TuyaBLESensorIsAvailable = Callable[["TuyaBLESensor", TuyaBLEProductInfo], bool] | None
@dataclass
class TuyaBLESensorMapping:
dp_id: int
description: SensorEntityDescription
force_add: bool = True
dp_type: TuyaBLEDataPointType | None = None
getter: Callable[[TuyaBLESensor], None] | None = None
coefficient: float = 1.0
icons: list[str] | None = None
is_available: TuyaBLESensorIsAvailable = None
@dataclass
class TuyaBLEBatteryMapping(TuyaBLESensorMapping):
description: SensorEntityDescription = field(
default_factory=lambda: SensorEntityDescription(
key="battery",
device_class=SensorDeviceClass.BATTERY,
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
)
)
@dataclass
class TuyaBLETemperatureMapping(TuyaBLESensorMapping):
description: SensorEntityDescription = field(
default_factory=lambda: SensorEntityDescription(
key="temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
)
)
def is_co2_alarm_enabled(self: TuyaBLESensor, product: TuyaBLEProductInfo) -> bool:
result: bool = True
datapoint = self._device.datapoints[13]
if datapoint:
result = bool(datapoint.value)
return result
def battery_enum_getter(self: TuyaBLESensor) -> None:
datapoint = self._device.datapoints[104]
if datapoint:
self._attr_native_value = datapoint.value * 20.0
@dataclass
class TuyaBLECategorySensorMapping:
products: dict[str, list[TuyaBLESensorMapping]] | None = None
mapping: list[TuyaBLESensorMapping] | None = None
mapping: dict[str, TuyaBLECategorySensorMapping] = {
"co2bj": TuyaBLECategorySensorMapping(
products={
"59s19z5m": [ # CO2 Detector
TuyaBLESensorMapping(
dp_id=1,
description=SensorEntityDescription(
key="carbon_dioxide_alarm",
icon="mdi:molecule-co2",
device_class=SensorDeviceClass.ENUM,
options=[
CO2_LEVEL_ALARM,
CO2_LEVEL_NORMAL,
],
),
is_available=is_co2_alarm_enabled,
),
TuyaBLESensorMapping(
dp_id=2,
description=SensorEntityDescription(
key="carbon_dioxide",
device_class=SensorDeviceClass.CO2,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
state_class=SensorStateClass.MEASUREMENT,
),
),
TuyaBLEBatteryMapping(dp_id=15),
TuyaBLETemperatureMapping(dp_id=18),
TuyaBLESensorMapping(
dp_id=19,
description=SensorEntityDescription(
key="humidity",
device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
),
]
}
),
"ms": TuyaBLECategorySensorMapping(
products={
**dict.fromkeys(
["ludzroix", "isk2p555"], # Smart Lock
[
TuyaBLESensorMapping(
dp_id=21,
description=SensorEntityDescription(
key="alarm_lock",
device_class=SensorDeviceClass.ENUM,
options=[
"wrong_finger",
"wrong_password",
"low_battery",
],
),
),
TuyaBLEBatteryMapping(dp_id=8),
],
),
}
),
"jtmspro": TuyaBLECategorySensorMapping(
products={
"6fibxtph": # Smart Lock
[
TuyaBLESensorMapping(
dp_id=9,
description=SensorEntityDescription(
key="battery_state",
icon="mdi:battery",
device_class=SensorDeviceClass.ENUM,
options=[
BATTERY_STATE_HIGH,
BATTERY_STATE_NORMAL,
BATTERY_STATE_LOW,
BATTERY_STATE_LOW,
],
),
icons=[
"mdi:battery-check",
"mdi:battery-50",
"mdi:battery-alert",
"mdi:battery-alert",
],
),
]
}
),
"szjqr": TuyaBLECategorySensorMapping(
products={
**dict.fromkeys(
["3yqdo5yt", "xhf790if"], # CubeTouch 1s and II
[
TuyaBLESensorMapping(
dp_id=7,
description=SensorEntityDescription(
key="battery_charging",
device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC,
options=[
BATTERY_NOT_CHARGING,
BATTERY_CHARGING,
BATTERY_CHARGED,
],
),
icons=[
"mdi:battery",
"mdi:power-plug-battery",
"mdi:battery-check",
],
),
TuyaBLEBatteryMapping(dp_id=8),
],
),
**dict.fromkeys(
[
"blliqpsj",
"ndvkgsrm",
"yiihr7zh",
"neq16kgd"
], # Fingerbot Plus
[
TuyaBLEBatteryMapping(dp_id=12),
],
),
**dict.fromkeys(
[
"ltak7e1p",
"y6kttvd6",
"yrnk7mnn",
"nvr2rocq",
"bnt7wajf",
"rvdceqjh",
"5xhbk964",
], # Fingerbot
[
TuyaBLEBatteryMapping(dp_id=12),
],
),
},
),
"wsdcg": TuyaBLECategorySensorMapping(
products={
"ojzlzzsw": [ # Soil moisture sensor
TuyaBLETemperatureMapping(
dp_id=1,
coefficient=10.0,
),
TuyaBLESensorMapping(
dp_id=2,
description=SensorEntityDescription(
key="moisture",
device_class=SensorDeviceClass.MOISTURE,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
),
TuyaBLESensorMapping(
dp_id=3,
description=SensorEntityDescription(
key="battery_state",
icon="mdi:battery",
device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC,
options=[
BATTERY_STATE_LOW,
BATTERY_STATE_NORMAL,
BATTERY_STATE_HIGH,
],
),
icons=[
"mdi:battery-alert",
"mdi:battery-50",
"mdi:battery-check",
],
),
TuyaBLEBatteryMapping(dp_id=4),
],
},
),
"znhsb": TuyaBLECategorySensorMapping(
products={
"cdlandip": # Smart water bottle
[
TuyaBLETemperatureMapping(
dp_id=101,
),
TuyaBLESensorMapping(
dp_id=102,
description=SensorEntityDescription(
key="water_intake",
device_class=SensorDeviceClass.WATER,
native_unit_of_measurement=UnitOfVolume.MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
),
),
TuyaBLESensorMapping(
dp_id=104,
description=SensorEntityDescription(
key="battery",
device_class=SensorDeviceClass.BATTERY,
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
),
getter=battery_enum_getter,
),
],
},
),
"ggq": TuyaBLECategorySensorMapping(
products={
"6pahkcau": [ # Irrigation computer
TuyaBLEBatteryMapping(dp_id=11),
TuyaBLESensorMapping(
dp_id=6,
description=SensorEntityDescription(
key="time_left",
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.MINUTES,
state_class=SensorStateClass.MEASUREMENT,
),
),
],
},
),
}
def rssi_getter(sensor: TuyaBLESensor) -> None:
sensor._attr_native_value = sensor._device.rssi
rssi_mapping = TuyaBLESensorMapping(
dp_id=SIGNAL_STRENGTH_DP_ID,
description=SensorEntityDescription(
key="signal_strength",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
getter=rssi_getter,
)
def get_mapping_by_device(device: TuyaBLEDevice) -> list[TuyaBLESensorMapping]:
category = mapping.get(device.category)
if category is not None and category.products is not None:
product_mapping = category.products.get(device.product_id)
if product_mapping is not None:
return product_mapping
if category.mapping is not None:
return category.mapping
else:
return []
else:
return []
class TuyaBLESensor(TuyaBLEEntity, SensorEntity):
"""Representation of a Tuya BLE sensor."""
def __init__(
self,
hass: HomeAssistant,
coordinator: DataUpdateCoordinator,
device: TuyaBLEDevice,
product: TuyaBLEProductInfo,
mapping: TuyaBLESensorMapping,
) -> None:
super().__init__(hass, coordinator, device, product, mapping.description)
self._mapping = mapping
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if self._mapping.getter is not None:
self._mapping.getter(self)
else:
datapoint = self._device.datapoints[self._mapping.dp_id]
if datapoint:
if datapoint.type == TuyaBLEDataPointType.DT_ENUM:
if self.entity_description.options is not None:
if datapoint.value >= 0 and datapoint.value < len(
self.entity_description.options
):
self._attr_native_value = self.entity_description.options[
datapoint.value
]
else:
self._attr_native_value = datapoint.value
if self._mapping.icons is not None:
if datapoint.value >= 0 and datapoint.value < len(
self._mapping.icons
):
self._attr_icon = self._mapping.icons[datapoint.value]
elif datapoint.type == TuyaBLEDataPointType.DT_VALUE:
self._attr_native_value = (
datapoint.value / self._mapping.coefficient
)
else:
self._attr_native_value = datapoint.value
self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return if entity is available."""
result = super().available
if result and self._mapping.is_available:
result = self._mapping.is_available(self, self._product)
return result
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Tuya BLE sensors."""
data: TuyaBLEData = hass.data[DOMAIN][entry.entry_id]
mappings = get_mapping_by_device(data.device)
entities: list[TuyaBLESensor] = [
TuyaBLESensor(
hass,
data.coordinator,
data.device,
data.product,
rssi_mapping,
)
]
for mapping in mappings:
if mapping.force_add or data.device.datapoints.has_id(
mapping.dp_id, mapping.dp_type
):
entities.append(
TuyaBLESensor(
hass,
data.coordinator,
data.device,
data.product,
mapping,
)
)
async_add_entities(entities)