Skip to content

Commit 9ed6784

Browse files
luc-assbdracopre-commit-ci[bot]
authored
Fix Leak Detector model 3&, add tests from HA (#281)
Co-authored-by: J. Nick Koston <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d6b0c36 commit 9ed6784

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

switchbot/adv_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class SwitchbotSupportedType(TypedDict):
179179
"func": process_woblindtilt,
180180
"manufacturer_id": 2409,
181181
},
182-
"3": {
182+
"&": {
183183
"modelName": SwitchbotModel.LEAK,
184184
"modelFriendlyName": "Leak Detector",
185185
"func": process_leak,

switchbot/discovery.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ async def get_contactsensors(self) -> dict[str, SwitchBotAdvertisement]:
118118
"""Return all WoContact/Contact sensor devices with services data."""
119119
return await self._get_devices_by_model("d")
120120

121+
async def get_leakdetectors(self) -> dict[str, SwitchBotAdvertisement]:
122+
"""Return all Leak Detectors with services data."""
123+
return await self._get_devices_by_model("&")
124+
121125
async def get_locks(self) -> dict[str, SwitchBotAdvertisement]:
122126
"""Return all WoLock/Locks devices with services data."""
123127
locks = await self._get_devices_by_model("o")

tests/test_adv_parser.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ def test_leak_active():
17581758
"low_battery": False,
17591759
},
17601760
"isEncrypted": False,
1761-
"model": "3",
1761+
"model": "&",
17621762
"modelFriendlyName": "Leak Detector",
17631763
"modelName": SwitchbotModel.LEAK,
17641764
"rawAdvData": b"&\x00N",
@@ -1782,7 +1782,7 @@ def test_leak_passive():
17821782
data={
17831783
"data": {},
17841784
"isEncrypted": False,
1785-
"model": "3",
1785+
"model": "&",
17861786
"rawAdvData": None,
17871787
},
17881788
device=ble_device,
@@ -1812,7 +1812,7 @@ def test_leak_no_leak_detected():
18121812
"low_battery": False,
18131813
},
18141814
"isEncrypted": False,
1815-
"model": "3",
1815+
"model": "&",
18161816
"modelFriendlyName": "Leak Detector",
18171817
"modelName": SwitchbotModel.LEAK,
18181818
"rawAdvData": b"&\x00d",
@@ -1844,7 +1844,7 @@ def test_leak_leak_detected():
18441844
"low_battery": False,
18451845
},
18461846
"isEncrypted": False,
1847-
"model": "3",
1847+
"model": "&",
18481848
"modelFriendlyName": "Leak Detector",
18491849
"modelName": SwitchbotModel.LEAK,
18501850
"rawAdvData": b"&\x00d",
@@ -1876,7 +1876,7 @@ def test_leak_low_battery():
18761876
"low_battery": False,
18771877
},
18781878
"isEncrypted": False,
1879-
"model": "3",
1879+
"model": "&",
18801880
"modelFriendlyName": "Leak Detector",
18811881
"modelName": SwitchbotModel.LEAK,
18821882
"rawAdvData": b"&\x00d",
@@ -1885,3 +1885,35 @@ def test_leak_low_battery():
18851885
rssi=-73,
18861886
active=True,
18871887
)
1888+
1889+
1890+
def test_leak_real_data_from_ha():
1891+
"""Test parse_advertisement_data for the leak detector."""
1892+
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "Any")
1893+
adv_data = generate_advertisement_data(
1894+
manufacturer_data={
1895+
2409: b"\\xd6407D1\\x02V\\x90\\x00\\x00\\x00\\x00\\x1e\\x05\\x00\\x00\\x00\\x00"
1896+
}, # no leak, low battery
1897+
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\\x00V"},
1898+
rssi=-73,
1899+
)
1900+
result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
1901+
assert result == SwitchBotAdvertisement(
1902+
address="aa:bb:cc:dd:ee:ff",
1903+
data={
1904+
"data": {
1905+
"leak": True,
1906+
"tampered": False,
1907+
"battery": 68,
1908+
"low_battery": False,
1909+
},
1910+
"isEncrypted": False,
1911+
"model": "&",
1912+
"modelFriendlyName": "Leak Detector",
1913+
"modelName": SwitchbotModel.LEAK,
1914+
"rawAdvData": b"&\\x00V",
1915+
},
1916+
device=ble_device,
1917+
rssi=-73,
1918+
active=True,
1919+
)

0 commit comments

Comments
 (0)