Debian Codegraph @

src/bthome_ble/const.py

raw symbols SCIP
1 """Constants for BTHome measurements."""
2
3 import dataclasses
4
5 from sensor_state_data import (
6 BaseDeviceClass,
7 BinarySensorDeviceClass,
8 SensorLibrary,
9 Units,
10 description,
11 )
12
13 from .event import EventDeviceKeys
14
15
16 @dataclasses.dataclass
17 class MeasTypes:
18 meas_format: (
19 EventDeviceKeys
20 | description.BaseBinarySensorDescription
21 | description.BaseSensorDescription
22 | None
23 )
24 data_length: int = 1
25 data_format: str = "unsigned_integer"
26 factor: float = 1
27
28
29 class ExtendedSensorDeviceClass(BaseDeviceClass):
30 """Device class for additional sensors (compared to sensor-state-data)."""
31
32 # Data channel
33 CHANNEL = "channel"
34
35 # Raw hex data
36 RAW = "raw"
37
38 # Text
39 TEXT = "text"
40
41 # Volume storage
42 VOLUME_STORAGE = "volume_storage"
43
44 # Direction
45 DIRECTION = "direction"
46
47 # Light level
48 LIGHT_LEVEL = "light_level"
49
50 # Precipitation
51 PRECIPITATION = "precipitation"
52
53 # Rotational speed
54 ROTATIONAL_SPEED = "rotational_speed"
55
56 # Settings revision
57 SETTINGS_REVISION = "settings_revision"
58
59
60 class ExtendedSensorLibrary(SensorLibrary):
61 """Sensor Library for additional sensors (compared to sensor-state-data)."""
62
63 RAW__NONE = description.BaseSensorDescription(
64 device_class=ExtendedSensorDeviceClass.RAW,
65 native_unit_of_measurement=None,
66 )
67
68 TEXT__NONE = description.BaseSensorDescription(
69 device_class=ExtendedSensorDeviceClass.TEXT,
70 native_unit_of_measurement=None,
71 )
72
73 VOLUME_STORAGE__VOLUME_LITERS = description.BaseSensorDescription(
74 device_class=ExtendedSensorDeviceClass.VOLUME_STORAGE,
75 native_unit_of_measurement=Units.VOLUME_LITERS,
76 )
77
78 DIRECTION__DEGREE = description.BaseSensorDescription(
79 device_class=ExtendedSensorDeviceClass.DIRECTION,
80 native_unit_of_measurement=Units.DEGREE,
81 )
82
83 PRECIPITATION__LENGTH_MILLIMETERS = description.BaseSensorDescription(
84 device_class=ExtendedSensorDeviceClass.PRECIPITATION,
85 native_unit_of_measurement=Units.LENGTH_MILLIMETERS,
86 )
87
88 CHANNEL__NONE = description.BaseSensorDescription(
89 device_class=ExtendedSensorDeviceClass.CHANNEL,
90 native_unit_of_measurement=None,
91 )
92
93 LIGHT_LEVEL__NONE = description.BaseSensorDescription(
94 device_class=ExtendedSensorDeviceClass.LIGHT_LEVEL,
95 native_unit_of_measurement=None,
96 )
97
98 ROTATIONAL_SPEED__REVOLUTIONS_PER_MINUTE = description.BaseSensorDescription(
99 device_class=ExtendedSensorDeviceClass.ROTATIONAL_SPEED,
100 native_unit_of_measurement=Units.REVOLUTIONS_PER_MINUTE,
101 )
102
103 SETTINGS_REVISION__NONE = description.BaseSensorDescription(
104 device_class=ExtendedSensorDeviceClass.SETTINGS_REVISION,
105 native_unit_of_measurement=None,
106 )
107
108
109 MEAS_TYPES: dict[int, MeasTypes] = {
110 0x00: MeasTypes(meas_format=SensorLibrary.PACKET_ID__NONE),
111 0x01: MeasTypes(meas_format=SensorLibrary.BATTERY__PERCENTAGE),
112 0x02: MeasTypes(
113 meas_format=SensorLibrary.TEMPERATURE__CELSIUS,
114 data_length=2,
115 data_format="signed_integer",
116 factor=0.01,
117 ),
118 0x03: MeasTypes(
119 meas_format=SensorLibrary.HUMIDITY__PERCENTAGE,
120 data_length=2,
121 factor=0.01,
122 ),
123 0x04: MeasTypes(
124 meas_format=SensorLibrary.PRESSURE__MBAR,
125 data_length=3,
126 factor=0.01,
127 ),
128 0x05: MeasTypes(
129 meas_format=SensorLibrary.LIGHT__LIGHT_LUX,
130 data_length=3,
131 factor=0.01,
132 ),
133 0x06: MeasTypes(
134 meas_format=SensorLibrary.MASS__MASS_KILOGRAMS,
135 data_length=2,
136 factor=0.01,
137 ),
138 0x07: MeasTypes(
139 meas_format=SensorLibrary.MASS__MASS_POUNDS,
140 data_length=2,
141 factor=0.01,
142 ),
143 0x08: MeasTypes(
144 meas_format=SensorLibrary.DEW_POINT__TEMP_CELSIUS,
145 data_length=2,
146 data_format="signed_integer",
147 factor=0.01,
148 ),
149 0x09: MeasTypes(
150 meas_format=SensorLibrary.COUNT__NONE,
151 data_length=1,
152 ),
153 0x0A: MeasTypes(
154 meas_format=SensorLibrary.ENERGY__ENERGY_KILO_WATT_HOUR,
155 data_length=3,
156 factor=0.001,
157 ),
158 0x0B: MeasTypes(
159 meas_format=SensorLibrary.POWER__POWER_WATT,
160 data_length=3,
161 factor=0.01,
162 ),
163 0x0C: MeasTypes(
164 meas_format=SensorLibrary.VOLTAGE__ELECTRIC_POTENTIAL_VOLT,
165 data_length=2,
166 factor=0.001,
167 ),
168 0x0D: MeasTypes(
169 meas_format=SensorLibrary.PM25__CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
170 data_length=2,
171 ),
172 0x0E: MeasTypes(
173 meas_format=SensorLibrary.PM10__CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
174 data_length=2,
175 ),
176 0x0F: MeasTypes(
177 meas_format=description.BaseBinarySensorDescription(
178 device_class=BinarySensorDeviceClass.GENERIC,
179 ),
180 ),
181 0x10: MeasTypes(
182 meas_format=description.BaseBinarySensorDescription(
183 device_class=BinarySensorDeviceClass.POWER,
184 ),
185 ),
186 0x11: MeasTypes(
187 meas_format=description.BaseBinarySensorDescription(
188 device_class=BinarySensorDeviceClass.OPENING,
189 ),
190 ),
191 0x12: MeasTypes(
192 meas_format=SensorLibrary.CO2__CONCENTRATION_PARTS_PER_MILLION,
193 data_length=2,
194 ),
195 0x13: MeasTypes(
196 meas_format=(
197 SensorLibrary.VOLATILE_ORGANIC_COMPOUNDS__CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
198 ),
199 data_length=2,
200 ),
201 0x14: MeasTypes(
202 meas_format=SensorLibrary.MOISTURE__PERCENTAGE,
203 data_length=2,
204 factor=0.01,
205 ),
206 0x15: MeasTypes(
207 meas_format=description.BaseBinarySensorDescription(
208 device_class=BinarySensorDeviceClass.BATTERY,
209 )
210 ),
211 0x16: MeasTypes(
212 meas_format=description.BaseBinarySensorDescription(
213 device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
214 )
215 ),
216 0x17: MeasTypes(
217 meas_format=description.BaseBinarySensorDescription(
218 device_class=BinarySensorDeviceClass.CO,
219 )
220 ),
221 0x18: MeasTypes(
222 meas_format=description.BaseBinarySensorDescription(
223 device_class=BinarySensorDeviceClass.COLD,
224 )
225 ),
226 0x19: MeasTypes(
227 meas_format=description.BaseBinarySensorDescription(
228 device_class=BinarySensorDeviceClass.CONNECTIVITY,
229 )
230 ),
231 0x1A: MeasTypes(
232 meas_format=description.BaseBinarySensorDescription(
233 device_class=BinarySensorDeviceClass.DOOR,
234 )
235 ),
236 0x1B: MeasTypes(
237 meas_format=description.BaseBinarySensorDescription(
238 device_class=BinarySensorDeviceClass.GARAGE_DOOR,
239 )
240 ),
241 0x1C: MeasTypes(
242 meas_format=description.BaseBinarySensorDescription(
243 device_class=BinarySensorDeviceClass.GAS,
244 )
245 ),
246 0x1D: MeasTypes(
247 meas_format=description.BaseBinarySensorDescription(
248 device_class=BinarySensorDeviceClass.HEAT,
249 )
250 ),
251 0x1E: MeasTypes(
252 meas_format=description.BaseBinarySensorDescription(
253 device_class=BinarySensorDeviceClass.LIGHT,
254 )
255 ),
256 0x1F: MeasTypes(
257 meas_format=description.BaseBinarySensorDescription(
258 device_class=BinarySensorDeviceClass.LOCK,
259 )
260 ),
261 0x20: MeasTypes(
262 meas_format=description.BaseBinarySensorDescription(
263 device_class=BinarySensorDeviceClass.MOISTURE,
264 )
265 ),
266 0x21: MeasTypes(
267 meas_format=description.BaseBinarySensorDescription(
268 device_class=BinarySensorDeviceClass.MOTION,
269 )
270 ),
271 0x22: MeasTypes(
272 meas_format=description.BaseBinarySensorDescription(
273 device_class=BinarySensorDeviceClass.MOVING,
274 )
275 ),
276 0x23: MeasTypes(
277 meas_format=description.BaseBinarySensorDescription(
278 device_class=BinarySensorDeviceClass.OCCUPANCY,
279 )
280 ),
281 0x24: MeasTypes(
282 meas_format=description.BaseBinarySensorDescription(
283 device_class=BinarySensorDeviceClass.PLUG,
284 )
285 ),
286 0x25: MeasTypes(
287 meas_format=description.BaseBinarySensorDescription(
288 device_class=BinarySensorDeviceClass.PRESENCE,
289 )
290 ),
291 0x26: MeasTypes(
292 meas_format=description.BaseBinarySensorDescription(
293 device_class=BinarySensorDeviceClass.PROBLEM,
294 )
295 ),
296 0x27: MeasTypes(
297 meas_format=description.BaseBinarySensorDescription(
298 device_class=BinarySensorDeviceClass.RUNNING,
299 )
300 ),
301 0x28: MeasTypes(
302 meas_format=description.BaseBinarySensorDescription(
303 device_class=BinarySensorDeviceClass.SAFETY,
304 )
305 ),
306 0x29: MeasTypes(
307 meas_format=description.BaseBinarySensorDescription(
308 device_class=BinarySensorDeviceClass.SMOKE,
309 )
310 ),
311 0x2A: MeasTypes(
312 meas_format=description.BaseBinarySensorDescription(
313 device_class=BinarySensorDeviceClass.SOUND,
314 )
315 ),
316 0x2B: MeasTypes(
317 meas_format=description.BaseBinarySensorDescription(
318 device_class=BinarySensorDeviceClass.TAMPER,
319 )
320 ),
321 0x2C: MeasTypes(
322 meas_format=description.BaseBinarySensorDescription(
323 device_class=BinarySensorDeviceClass.VIBRATION,
324 )
325 ),
326 0x2D: MeasTypes(
327 meas_format=description.BaseBinarySensorDescription(
328 device_class=BinarySensorDeviceClass.WINDOW,
329 )
330 ),
331 0x2E: MeasTypes(meas_format=SensorLibrary.HUMIDITY__PERCENTAGE),
332 0x2F: MeasTypes(meas_format=SensorLibrary.MOISTURE__PERCENTAGE),
333 0x3A: MeasTypes(meas_format=EventDeviceKeys.BUTTON),
334 0x3B: MeasTypes(
335 meas_format=EventDeviceKeys.COMMAND,
336 data_format="command",
337 ),
338 0x3C: MeasTypes(
339 meas_format=EventDeviceKeys.DIMMER,
340 data_length=2,
341 ),
342 0x3D: MeasTypes(
343 meas_format=SensorLibrary.COUNT__NONE,
344 data_length=2,
345 ),
346 0x3E: MeasTypes(
347 meas_format=SensorLibrary.COUNT__NONE,
348 data_length=4,
349 ),
350 0x3F: MeasTypes(
351 meas_format=SensorLibrary.ROTATION__DEGREE,
352 data_length=2,
353 data_format="signed_integer",
354 factor=0.1,
355 ),
356 0x40: MeasTypes(
357 meas_format=SensorLibrary.DISTANCE__LENGTH_MILLIMETERS,
358 data_length=2,
359 factor=1,
360 ),
361 0x41: MeasTypes(
362 meas_format=SensorLibrary.DISTANCE__LENGTH_METERS,
363 data_length=2,
364 factor=0.1,
365 ),
366 0x42: MeasTypes(
367 meas_format=SensorLibrary.DURATION__TIME_SECONDS,
368 data_length=3,
369 factor=0.001,
370 ),
371 0x43: MeasTypes(
372 meas_format=SensorLibrary.CURRENT__ELECTRIC_CURRENT_AMPERE,
373 data_length=2,
374 factor=0.001,
375 ),
376 0x44: MeasTypes(
377 meas_format=SensorLibrary.SPEED__SPEED_METERS_PER_SECOND,
378 data_length=2,
379 factor=0.01,
380 ),
381 0x45: MeasTypes(
382 meas_format=SensorLibrary.TEMPERATURE__CELSIUS,
383 data_length=2,
384 data_format="signed_integer",
385 factor=0.1,
386 ),
387 0x46: MeasTypes(
388 meas_format=SensorLibrary.UV_INDEX__NONE,
389 data_length=1,
390 factor=0.1,
391 ),
392 0x47: MeasTypes(
393 meas_format=SensorLibrary.VOLUME__VOLUME_LITERS,
394 data_length=2,
395 factor=0.1,
396 ),
397 0x48: MeasTypes(
398 meas_format=SensorLibrary.VOLUME__VOLUME_MILLILITERS,
399 data_length=2,
400 ),
401 0x49: MeasTypes(
402 meas_format=SensorLibrary.VOLUME_FLOW_RATE__VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR,
403 data_length=2,
404 factor=0.001,
405 ),
406 0x4A: MeasTypes(
407 meas_format=SensorLibrary.VOLTAGE__ELECTRIC_POTENTIAL_VOLT,
408 data_length=2,
409 factor=0.1,
410 ),
411 0x4B: MeasTypes(
412 meas_format=SensorLibrary.GAS__VOLUME_CUBIC_METERS,
413 data_length=3,
414 factor=0.001,
415 ),
416 0x4C: MeasTypes(
417 meas_format=SensorLibrary.GAS__VOLUME_CUBIC_METERS,
418 data_length=4,
419 factor=0.001,
420 ),
421 0x4D: MeasTypes(
422 meas_format=SensorLibrary.ENERGY__ENERGY_KILO_WATT_HOUR,
423 data_length=4,
424 factor=0.001,
425 ),
426 0x4E: MeasTypes(
427 meas_format=SensorLibrary.VOLUME__VOLUME_LITERS,
428 data_length=4,
429 factor=0.001,
430 ),
431 0x4F: MeasTypes(
432 meas_format=SensorLibrary.WATER__VOLUME_LITERS,
433 data_length=4,
434 factor=0.001,
435 ),
436 0x50: MeasTypes(
437 meas_format=SensorLibrary.TIMESTAMP__NONE,
438 data_length=4,
439 data_format="timestamp",
440 ),
441 0x51: MeasTypes(
442 meas_format=SensorLibrary.ACCELERATION__ACCELERATION_METERS_PER_SQUARE_SECOND,
443 data_length=2,
444 factor=0.001,
445 ),
446 0x52: MeasTypes(
447 meas_format=SensorLibrary.GYROSCOPE__GYROSCOPE_DEGREES_PER_SECOND,
448 data_length=2,
449 factor=0.001,
450 ),
451 0x53: MeasTypes(
452 meas_format=ExtendedSensorLibrary.TEXT__NONE,
453 data_format="string",
454 ),
455 0x54: MeasTypes(
456 meas_format=ExtendedSensorLibrary.RAW__NONE,
457 data_format="raw",
458 ),
459 0x55: MeasTypes(
460 meas_format=ExtendedSensorLibrary.VOLUME_STORAGE__VOLUME_LITERS,
461 data_length=4,
462 factor=0.001,
463 ),
464 0x56: MeasTypes(
465 meas_format=SensorLibrary.CONDUCTIVITY__CONDUCTIVITY,
466 data_length=2,
467 ),
468 0x57: MeasTypes(
469 meas_format=SensorLibrary.TEMPERATURE__CELSIUS,
470 data_length=1,
471 data_format="signed_integer",
472 ),
473 0x58: MeasTypes(
474 meas_format=SensorLibrary.TEMPERATURE__CELSIUS,
475 data_length=1,
476 data_format="signed_integer",
477 factor=0.35,
478 ),
479 0x59: MeasTypes(
480 meas_format=SensorLibrary.COUNT__NONE,
481 data_length=1,
482 data_format="signed_integer",
483 ),
484 0x5A: MeasTypes(
485 meas_format=SensorLibrary.COUNT__NONE,
486 data_length=2,
487 data_format="signed_integer",
488 ),
489 0x5B: MeasTypes(
490 meas_format=SensorLibrary.COUNT__NONE,
491 data_length=4,
492 data_format="signed_integer",
493 ),
494 0x5C: MeasTypes(
495 meas_format=SensorLibrary.POWER__POWER_WATT,
496 data_length=4,
497 factor=0.01,
498 data_format="signed_integer",
499 ),
500 0x5D: MeasTypes(
501 meas_format=SensorLibrary.CURRENT__ELECTRIC_CURRENT_AMPERE,
502 data_length=2,
503 factor=0.001,
504 data_format="signed_integer",
505 ),
506 0x5E: MeasTypes(
507 meas_format=ExtendedSensorLibrary.DIRECTION__DEGREE,
508 data_length=2,
509 factor=0.01,
510 ),
511 0x5F: MeasTypes(
512 meas_format=ExtendedSensorLibrary.PRECIPITATION__LENGTH_MILLIMETERS,
513 data_length=2,
514 factor=0.1,
515 ),
516 0x60: MeasTypes(meas_format=ExtendedSensorLibrary.CHANNEL__NONE),
517 0x61: MeasTypes(
518 meas_format=ExtendedSensorLibrary.ROTATIONAL_SPEED__REVOLUTIONS_PER_MINUTE,
519 data_length=2,
520 ),
521 0x62: MeasTypes(
522 meas_format=SensorLibrary.SPEED__SPEED_METERS_PER_SECOND,
523 data_length=4,
524 factor=0.000001,
525 data_format="signed_integer",
526 ),
527 0x63: MeasTypes(
528 meas_format=SensorLibrary.ACCELERATION__ACCELERATION_METERS_PER_SQUARE_SECOND,
529 data_length=4,
530 factor=0.000001,
531 data_format="signed_integer",
532 ),
533 0x64: MeasTypes(meas_format=ExtendedSensorLibrary.LIGHT_LEVEL__NONE),
534 0x65: MeasTypes(meas_format=ExtendedSensorLibrary.SETTINGS_REVISION__NONE),
535 # Device information objects (BTHome misc range). These do not produce
536 # sensor entities; the parser consumes them to set device-level metadata.
537 0xF0: MeasTypes(
538 meas_format=None,
539 data_length=2,
540 data_format="device_type_id",
541 ),
542 0xF1: MeasTypes(
543 meas_format=None,
544 data_length=4,
545 data_format="firmware_version_4",
546 ),
547 0xF2: MeasTypes(
548 meas_format=None,
549 data_length=3,
550 data_format="firmware_version_3",
551 ),
552 }