Skip to content

Commit

Permalink
Fix Leak Detector model 3&, add tests from HA (#281)
Browse files Browse the repository at this point in the history
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 22, 2024
1 parent d6b0c36 commit 9ed6784
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion switchbot/adv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class SwitchbotSupportedType(TypedDict):
"func": process_woblindtilt,
"manufacturer_id": 2409,
},
"3": {
"&": {
"modelName": SwitchbotModel.LEAK,
"modelFriendlyName": "Leak Detector",
"func": process_leak,
Expand Down
4 changes: 4 additions & 0 deletions switchbot/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ async def get_contactsensors(self) -> dict[str, SwitchBotAdvertisement]:
"""Return all WoContact/Contact sensor devices with services data."""
return await self._get_devices_by_model("d")

async def get_leakdetectors(self) -> dict[str, SwitchBotAdvertisement]:
"""Return all Leak Detectors with services data."""
return await self._get_devices_by_model("&")

async def get_locks(self) -> dict[str, SwitchBotAdvertisement]:
"""Return all WoLock/Locks devices with services data."""
locks = await self._get_devices_by_model("o")
Expand Down
42 changes: 37 additions & 5 deletions tests/test_adv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,7 @@ def test_leak_active():
"low_battery": False,
},
"isEncrypted": False,
"model": "3",
"model": "&",
"modelFriendlyName": "Leak Detector",
"modelName": SwitchbotModel.LEAK,
"rawAdvData": b"&\x00N",
Expand All @@ -1782,7 +1782,7 @@ def test_leak_passive():
data={
"data": {},
"isEncrypted": False,
"model": "3",
"model": "&",
"rawAdvData": None,
},
device=ble_device,
Expand Down Expand Up @@ -1812,7 +1812,7 @@ def test_leak_no_leak_detected():
"low_battery": False,
},
"isEncrypted": False,
"model": "3",
"model": "&",
"modelFriendlyName": "Leak Detector",
"modelName": SwitchbotModel.LEAK,
"rawAdvData": b"&\x00d",
Expand Down Expand Up @@ -1844,7 +1844,7 @@ def test_leak_leak_detected():
"low_battery": False,
},
"isEncrypted": False,
"model": "3",
"model": "&",
"modelFriendlyName": "Leak Detector",
"modelName": SwitchbotModel.LEAK,
"rawAdvData": b"&\x00d",
Expand Down Expand Up @@ -1876,7 +1876,7 @@ def test_leak_low_battery():
"low_battery": False,
},
"isEncrypted": False,
"model": "3",
"model": "&",
"modelFriendlyName": "Leak Detector",
"modelName": SwitchbotModel.LEAK,
"rawAdvData": b"&\x00d",
Expand All @@ -1885,3 +1885,35 @@ def test_leak_low_battery():
rssi=-73,
active=True,
)


def test_leak_real_data_from_ha():
"""Test parse_advertisement_data for the leak detector."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "Any")
adv_data = generate_advertisement_data(
manufacturer_data={
2409: b"\\xd6407D1\\x02V\\x90\\x00\\x00\\x00\\x00\\x1e\\x05\\x00\\x00\\x00\\x00"
}, # no leak, low battery
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\\x00V"},
rssi=-73,
)
result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
assert result == SwitchBotAdvertisement(
address="aa:bb:cc:dd:ee:ff",
data={
"data": {
"leak": True,
"tampered": False,
"battery": 68,
"low_battery": False,
},
"isEncrypted": False,
"model": "&",
"modelFriendlyName": "Leak Detector",
"modelName": SwitchbotModel.LEAK,
"rawAdvData": b"&\\x00V",
},
device=ble_device,
rssi=-73,
active=True,
)

0 comments on commit 9ed6784

Please sign in to comment.