Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adv_parser for Switchbot Lock Pro #243

Merged
merged 9 commits into from
Jul 11, 2024
4 changes: 2 additions & 2 deletions switchbot/adv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .adv_parsers.hub2 import process_wohub2
from .adv_parsers.humidifier import process_wohumidifier
from .adv_parsers.light_strip import process_wostrip
from .adv_parsers.lock import process_wolock
from .adv_parsers.lock import process_wolock, process_wolock_pro
from .adv_parsers.meter import process_wosensorth
from .adv_parsers.motion import process_wopresence
from .adv_parsers.plug import process_woplugmini
Expand Down Expand Up @@ -153,7 +153,7 @@ class SwitchbotSupportedType(TypedDict):
"$": {
"modelName": SwitchbotModel.LOCK_PRO,
"modelFriendlyName": "Lock Pro",
"func": process_wolock,
"func": process_wolock_pro,
"manufacturer_id": 2409,
},
"x": {
Expand Down
25 changes: 25 additions & 0 deletions switchbot/adv_parsers/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ def process_wolock(data: bytes | None, mfr_data: bytes | None) -> dict[str, bool
"auto_lock_paused": bool(mfr_data[8] & 0b00000010),
"night_latch": bool(mfr_data[9] & 0b00000001) if len(mfr_data) > 9 else False,
}


def process_wolock_pro(
data: bytes | None, mfr_data: bytes | None
) -> dict[str, bool | int]:
_LOGGER.debug("mfr_data: %s", mfr_data.hex())
if data:
_LOGGER.debug("data: %s", data.hex())

res = {
"battery": data[2] & 0b01111111 if data else None,
"calibration": bool(mfr_data[7] & 0b10000000),
"status": LockStatus(int(mfr_data[7] & 0b00111000) / 8),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't >> 3 instead of / 8 more clearly convey what is happening here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it was some leftover of debugging to see if they changed how the lock status is encoded (thankfully they didn't). I'll change it to >>3.

"door_open": bool(mfr_data[8] & 0b01100000),
# Double lock mode is not supported on Lock Pro
"update_from_secondary_lock": False,
"double_lock_mode": False,
"unclosed_alarm": bool(mfr_data[11] & 0b10000000),
"unlocked_alarm": bool(mfr_data[11] & 0b01000000),
"auto_lock_paused": bool(mfr_data[8] & 0b100000),
"night_latch": bool(mfr_data[9] & 0b00000001),
"manual": not bool(mfr_data[7] & 0b00000010),
}
_LOGGER.debug(res)
return res
2 changes: 2 additions & 0 deletions tests/test_adv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,7 @@ def test_parsing_lock_pro_active():
"unlocked_alarm": False,
"auto_lock_paused": False,
"night_latch": False,
"manual": False,
},
"model": "$",
"isEncrypted": False,
Expand Down Expand Up @@ -1446,6 +1447,7 @@ def test_parsing_lock_pro_passive():
"unlocked_alarm": False,
"auto_lock_paused": False,
"night_latch": False,
"manual": False,
},
"model": "$",
"isEncrypted": False,
Expand Down
Loading