Skip to content

Commit 96ddb65

Browse files
authored
Add passive parsers for light strip and bulb (#154)
1 parent 7594fd9 commit 96ddb65

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

switchbot/adv_parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class SwitchbotSupportedType(TypedDict):
6464
"modelName": SwitchbotModel.LIGHT_STRIP,
6565
"modelFriendlyName": "Light Strip",
6666
"func": process_wostrip,
67+
"manufacturer_id": 2409,
68+
"manufacturer_data_length": 16,
6769
},
6870
"c": {
6971
"modelName": SwitchbotModel.CURTAIN,
@@ -100,6 +102,8 @@ class SwitchbotSupportedType(TypedDict):
100102
"modelName": SwitchbotModel.COLOR_BULB,
101103
"modelFriendlyName": "Color Bulb",
102104
"func": process_color_bulb,
105+
"manufacturer_id": 2409,
106+
"manufacturer_data_length": 11,
103107
},
104108
"q": {
105109
"modelName": SwitchbotModel.CEILING_LIGHT,
@@ -194,7 +198,8 @@ def _parse_data(
194198
if model_data.get("manufacturer_data_length") == len(_mfr_data):
195199
_model = model_chr
196200
break
197-
if model_data.get("service_uuids", set()).intersection(_service_uuids):
201+
service_uuids = model_data.get("service_uuids", set())
202+
if service_uuids and service_uuids.intersection(_service_uuids):
198203
_model = model_chr
199204
break
200205

tests/test_adv_parser.py

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def test_parse_advertisement_data_empty():
292292
"""Test parse_advertisement_data with empty data does not blow up."""
293293
ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
294294
adv_data = generate_advertisement_data(
295-
manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
295+
manufacturer_data={2403: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
296296
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b""},
297297
)
298298
result = parse_advertisement_data(ble_device, adv_data)
@@ -609,3 +609,107 @@ def test_switchbot_passive():
609609
device=ble_device,
610610
rssi=-50,
611611
)
612+
613+
614+
def test_bulb_active():
615+
"""Test parsing bulb as active."""
616+
ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
617+
adv_data = generate_advertisement_data(
618+
manufacturer_data={2409: b"\x84\xf7\x03\xb4\xcbz\x03\xe4!\x00\x00"},
619+
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"u\x00d"},
620+
tx_power=-127,
621+
rssi=-50,
622+
)
623+
result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.BOT)
624+
assert result == SwitchBotAdvertisement(
625+
address="aa:bb:cc:dd:ee:ff",
626+
data={
627+
"data": {
628+
"brightness": 100,
629+
"color_mode": 1,
630+
"delay": False,
631+
"isOn": True,
632+
"loop_index": 0,
633+
"preset": False,
634+
"sequence_number": 3,
635+
"speed": 0,
636+
},
637+
"isEncrypted": False,
638+
"model": "u",
639+
"modelFriendlyName": "Color Bulb",
640+
"modelName": SwitchbotModel.COLOR_BULB,
641+
"rawAdvData": b"u\x00d",
642+
},
643+
device=ble_device,
644+
rssi=-50,
645+
)
646+
647+
648+
def test_bulb_passive():
649+
"""Test parsing bulb as passive."""
650+
ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
651+
adv_data = generate_advertisement_data(
652+
manufacturer_data={2409: b"\x84\xf7\x03\xb4\xcbz\x03\xe4!\x00\x00"},
653+
service_data={},
654+
tx_power=-127,
655+
rssi=-50,
656+
)
657+
result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.BOT)
658+
assert result == SwitchBotAdvertisement(
659+
address="aa:bb:cc:dd:ee:ff",
660+
data={
661+
"data": {
662+
"brightness": 100,
663+
"color_mode": 1,
664+
"delay": False,
665+
"isOn": True,
666+
"loop_index": 0,
667+
"preset": False,
668+
"sequence_number": 3,
669+
"speed": 0,
670+
},
671+
"isEncrypted": False,
672+
"model": "u",
673+
"modelFriendlyName": "Color Bulb",
674+
"modelName": SwitchbotModel.COLOR_BULB,
675+
"rawAdvData": None,
676+
},
677+
device=ble_device,
678+
rssi=-50,
679+
)
680+
681+
682+
def test_lightstrip_passive():
683+
"""Test parsing lightstrip as passive."""
684+
ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
685+
adv_data = generate_advertisement_data(
686+
manufacturer_data={
687+
2409: b"`U\xf9(\xe5\x96\x00d\x02\xb0\x00\x00\x00\x00\x00\x00"
688+
},
689+
service_data={},
690+
tx_power=-127,
691+
rssi=-50,
692+
)
693+
result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.BOT)
694+
assert result == SwitchBotAdvertisement(
695+
address="aa:bb:cc:dd:ee:ff",
696+
data={
697+
"data": {
698+
"brightness": 100,
699+
"color_mode": 2,
700+
"delay": False,
701+
"isOn": False,
702+
"loop_index": 0,
703+
"preset": False,
704+
"sequence_number": 0,
705+
"speed": 48,
706+
},
707+
"isEncrypted": False,
708+
"model": "r",
709+
"modelFriendlyName": "Light Strip",
710+
"modelName": SwitchbotModel.LIGHT_STRIP,
711+
"rawAdvData": None,
712+
},
713+
device=ble_device,
714+
rssi=-50,
715+
)

0 commit comments

Comments
 (0)