Skip to content

Commit f7a2454

Browse files
dsypniewskibdraco
andauthored
Add verify_encryption_key for lock (#169)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 4783ee5 commit f7a2454

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

switchbot/devices/lock.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
1010

1111
from ..const import LockStatus
12-
from .device import SwitchbotDevice
12+
from .device import SwitchbotDevice, SwitchbotOperationError
1313

1414
COMMAND_HEADER = "57"
1515
COMMAND_GET_CK_IV = f"{COMMAND_HEADER}0f2103"
@@ -52,6 +52,23 @@ def __init__(
5252
self._notifications_enabled: bool = False
5353
super().__init__(device, None, interface, **kwargs)
5454

55+
@staticmethod
56+
async def verify_encryption_key(
57+
device: BLEDevice, key_id: str, encryption_key: str
58+
) -> bool:
59+
try:
60+
lock = SwitchbotLock(
61+
device=device, key_id=key_id, encryption_key=encryption_key
62+
)
63+
except ValueError:
64+
return False
65+
try:
66+
lock_info = await lock.get_basic_info()
67+
except SwitchbotOperationError:
68+
return False
69+
70+
return lock_info is not None
71+
5572
async def lock(self) -> bool:
5673
"""Send lock command."""
5774
return await self._lock_unlock(

0 commit comments

Comments
 (0)