Skip to content

Commit 509b393

Browse files
committed
Move toothbrush to binary sensor
1 parent 6b61505 commit 509b393

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

custom_components/ble_monitor/binary_sensor.py

+9
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ async def async_added_to_hass(self):
286286
self._extra_state_attributes["timestamp"] = old_state.attributes["timestamp"]
287287
if "result" in old_state.attributes:
288288
self._extra_state_attributes["result"] = old_state.attributes["result"]
289+
if "score" in old_state.attributes:
290+
self._extra_state_attributes["score"] = old_state.attributes["score"]
291+
if "counter" in old_state.attributes:
292+
self._extra_state_attributes["counter"] = old_state.attributes["counter"]
289293

290294
self.ready_for_update = True
291295

@@ -382,6 +386,11 @@ def collect(self, data, batt_attr=None):
382386
if self.entity_description.key == "fingerprint":
383387
self._extra_state_attributes["result"] = data["result"]
384388
self._extra_state_attributes["key id"] = data["key id"]
389+
if self.entity_description.key == "toothbrush":
390+
if "counter" in data:
391+
self._extra_state_attributes["counter"] = data["counter"]
392+
else:
393+
self._extra_state_attributes["score"] = data["score"]
385394

386395
async def async_update(self):
387396
"""Update sensor state and attribute."""

custom_components/ble_monitor/ble_parser/xiaomi.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,12 @@
125125
# Advertisement conversion of measurement data
126126
# https://iot.mi.com/new/doc/embedded-development/ble/object-definition
127127
def obj0003(xobj):
128+
# Motion
128129
return {"motion": xobj[0], "motion timer": xobj[0]}
129130

130131

131132
def obj0006(xobj):
133+
# Fingerprint
132134
if len(xobj) == 5:
133135
key_id = xobj[0:4]
134136
match_byte = xobj[4]
@@ -167,10 +169,15 @@ def obj0006(xobj):
167169

168170

169171
def obj0010(xobj):
170-
return {"toothbrush mode": xobj[1]}
172+
# Toothbrush
173+
if xobj[0] == 0:
174+
return {'toothbrush': 1, 'counter': xobj[1]}
175+
else:
176+
return {'toothbrush': 0, 'score': xobj[1]}
171177

172178

173179
def obj000b(xobj):
180+
# Lock
174181
if len(xobj) == 9:
175182
action = xobj[0] & 0x0F
176183
method = xobj[0] >> 4
@@ -206,6 +213,7 @@ def obj000b(xobj):
206213

207214

208215
def obj000f(xobj):
216+
# Moving with light
209217
if len(xobj) == 3:
210218
(value,) = LIGHT_STRUCT.unpack(xobj + b'\x00')
211219
# MJYD02YL: 1 - moving no light, 100 - moving with light
@@ -217,6 +225,7 @@ def obj000f(xobj):
217225

218226

219227
def obj1001(xobj):
228+
# Button
220229
if len(xobj) == 3:
221230
(button_type, value, press) = BUTTON_STRUCT.unpack(xobj)
222231
# RTCGQ02LM: button
@@ -373,6 +382,7 @@ def obj1001(xobj):
373382

374383

375384
def obj1004(xobj):
385+
# Temperature
376386
if len(xobj) == 2:
377387
(temp,) = T_STRUCT.unpack(xobj)
378388
return {"temperature": temp / 10}
@@ -385,6 +395,7 @@ def obj1005(xobj):
385395

386396

387397
def obj1006(xobj):
398+
# Humidity
388399
if len(xobj) == 2:
389400
(humi,) = H_STRUCT.unpack(xobj)
390401
return {"humidity": humi / 10}
@@ -393,6 +404,7 @@ def obj1006(xobj):
393404

394405

395406
def obj1007(xobj):
407+
# Illuminance
396408
if len(xobj) == 3:
397409
(illum,) = ILL_STRUCT.unpack(xobj + b'\x00')
398410
return {"illuminance": illum, "light": 1 if illum == 100 else 0}
@@ -401,10 +413,12 @@ def obj1007(xobj):
401413

402414

403415
def obj1008(xobj):
416+
# Moisture
404417
return {"moisture": xobj[0]}
405418

406419

407420
def obj1009(xobj):
421+
# Conductivity
408422
if len(xobj) == 2:
409423
(cond,) = CND_STRUCT.unpack(xobj)
410424
return {"conductivity": cond}
@@ -413,6 +427,7 @@ def obj1009(xobj):
413427

414428

415429
def obj1010(xobj):
430+
# Formaldehyde
416431
if len(xobj) == 2:
417432
(fmdh,) = FMDH_STRUCT.unpack(xobj)
418433
return {"formaldehyde": fmdh / 100}
@@ -421,22 +436,27 @@ def obj1010(xobj):
421436

422437

423438
def obj1012(xobj):
439+
# Switch
424440
return {"switch": xobj[0]}
425441

426442

427443
def obj1013(xobj):
444+
# Consumable (in percent)
428445
return {"consumable": xobj[0]}
429446

430447

431448
def obj1014(xobj):
449+
# Moisture
432450
return {"moisture": xobj[0]}
433451

434452

435453
def obj1015(xobj):
454+
# Smoke
436455
return {"smoke detector": xobj[0]}
437456

438457

439458
def obj1017(xobj):
459+
# Motion
440460
if len(xobj) == 4:
441461
(motion,) = M_STRUCT.unpack(xobj)
442462
# seconds since last motion detected message (not used, we use motion timer in obj000f)
@@ -447,10 +467,12 @@ def obj1017(xobj):
447467

448468

449469
def obj1018(xobj):
470+
# Light intensity
450471
return {"light": xobj[0]}
451472

452473

453474
def obj1019(xobj):
475+
# Door
454476
open = xobj[0]
455477
if open == 0:
456478
opening = 1
@@ -471,12 +493,14 @@ def obj1019(xobj):
471493

472494

473495
def obj100a(xobj):
496+
# Battery
474497
batt = xobj[0]
475498
volt = 2.2 + (3.1 - 2.2) * (batt / 100)
476499
return {"battery": batt, "voltage": volt}
477500

478501

479502
def obj100d(xobj):
503+
# Temperature and humidity
480504
if len(xobj) == 4:
481505
(temp, humi) = TH_STRUCT.unpack(xobj)
482506
return {"temperature": temp / 10, "humidity": humi / 10}
@@ -485,6 +509,7 @@ def obj100d(xobj):
485509

486510

487511
def obj2000(xobj):
512+
# Body temperature
488513
if len(xobj) == 5:
489514
(temp1, temp2, bat) = TTB_STRUCT.unpack(xobj)
490515
# Body temperature is calculated from the two measured temperatures.

custom_components/ble_monitor/const.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ class BLEMonitorBinarySensorEntityDescription(
201201
unique_id="lock_",
202202
device_class=DEVICE_CLASS_LOCK,
203203
),
204+
BLEMonitorBinarySensorEntityDescription(
205+
key="toothbrush",
206+
sensor_class="BaseBinarySensor",
207+
name="ble toothbrush",
208+
unique_id="tb_",
209+
icon="mdi:toothbrush-electric",
210+
device_class=DEVICE_CLASS_POWER,
211+
),
204212
)
205213

206214

@@ -326,16 +334,6 @@ class BLEMonitorBinarySensorEntityDescription(
326334
device_class=None,
327335
state_class=STATE_CLASS_MEASUREMENT,
328336
),
329-
BLEMonitorSensorEntityDescription(
330-
key="toothbrush mode",
331-
sensor_class="InstantUpdateSensor",
332-
name="ble toothbrush mode",
333-
unique_id="to_",
334-
icon="mdi:toothbrush-electric",
335-
native_unit_of_measurement=None,
336-
device_class=None,
337-
state_class=None,
338-
),
339337
BLEMonitorSensorEntityDescription(
340338
key="weight",
341339
sensor_class="WeightSensor",
@@ -549,7 +547,7 @@ class BLEMonitorBinarySensorEntityDescription(
549547
'MUE4094RT' : [[], [], ["motion"]],
550548
'RTCGQ02LM' : [["battery"], ["button"], ["light", "motion"]],
551549
'MMC-T201-1' : [["temperature", "battery"], [], []],
552-
'M1S-T500' : [["battery"], ["toothbrush mode"], []],
550+
'M1S-T500' : [["battery"], [], ["toothbrush"]],
553551
'ZNMS16LM' : [["battery"], [], ["lock", "fingerprint"]],
554552
'ZNMS17LM' : [["battery"], [], ["lock", "fingerprint"]],
555553
'CGC1' : [["temperature", "humidity", "battery"], [], []],

custom_components/ble_monitor/sensor.py

-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ class BaseSensor(RestoreEntity, SensorEntity):
248248
# | |**consumable
249249
# | |--AccelerationSensor (Class)
250250
# | | |**acceleration
251-
# | |**toothbrush
252251
# | |--WeightSensor (Class)
253252
# | | |**weight
254253
# | | |**non-stabilized weight

docs/_devices/M1S-T500.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ model: M1S-T500
55
image: M1S-T500.jpg
66
physical_description:
77
broadcasted_properties:
8-
- toothbrush mode
8+
- toothbrush
99
- battery
10+
- score
11+
- counter
1012
broadcasted_property_notes:
11-
- property: toothbrush mode
12-
note: At the moment, we are looking into the meaning of the different states. If you have more info which state corresponds to what, please post a message in [this topic](https://github.com/custom-components/ble_monitor/issues/319)
13+
- property: toothbrush
14+
note: The toothbrush sensor has two attributes, `score` and `counter`
15+
- property: counter
16+
note: After starting the toothbrush, the counter will count the time you used your toothbrush
17+
- property: score
18+
note: After finishing toothbrushing, the toothbrush will report a score
1319
broadcast_rate:
1420
active_scan:
1521
encryption_key:

0 commit comments

Comments
 (0)