Skip to content

Commit 001841b

Browse files
authored
Add read-only support for lock (#145)
1 parent e9482cc commit 001841b

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

switchbot/adv_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .adv_parsers.meter import process_wosensorth
2020
from .adv_parsers.motion import process_wopresence
2121
from .adv_parsers.plug import process_woplugmini
22+
from .adv_parsers.lock import process_wolock
2223
from .const import SwitchbotModel
2324
from .models import SwitchBotAdvertisement
2425

@@ -94,6 +95,11 @@ class SwitchbotSupportedType(TypedDict):
9495
"modelFriendlyName": "Humidifier",
9596
"func": process_wohumidifier,
9697
},
98+
"o": {
99+
"modelName": SwitchbotModel.LOCK,
100+
"modelFriendlyName": "Lock",
101+
"func": process_wolock,
102+
},
97103
}
98104

99105

switchbot/adv_parsers/lock.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Lock parser."""
2+
from __future__ import annotations
3+
from ..const import LockStatus
4+
5+
import logging
6+
7+
_LOGGER = logging.getLogger(__name__)
8+
9+
10+
def process_wolock(data: bytes, mfr_data: bytes | None) -> dict[str, bool | int]:
11+
"""Process woLock services data."""
12+
if mfr_data is None:
13+
return {}
14+
15+
_LOGGER.debug("mfr_data: %s", mfr_data.hex())
16+
_LOGGER.debug("data: %s", data.hex())
17+
18+
return {
19+
"battery": data[2] & 0b01111111,
20+
"calibration": bool(mfr_data[7] & 0b10000000),
21+
"status": LockStatus(mfr_data[7] & 0b01110000),
22+
"update_from_secondary_lock": bool(mfr_data[7] & 0b00001000),
23+
"door_open": bool(mfr_data[7] & 0b00000100),
24+
"double_lock_mode": bool(mfr_data[8] & 0b10000000),
25+
"unclosed_alarm": bool(mfr_data[8] & 0b00100000),
26+
"unlocked_alarm": bool(mfr_data[8] & 0b00010000),
27+
"auto_lock_paused": bool(mfr_data[8] & 0b00000010),
28+
}

switchbot/const.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Library to handle connection with Switchbot."""
22
from __future__ import annotations
33

4+
from enum import Enum
5+
46
DEFAULT_RETRY_COUNT = 3
57
DEFAULT_RETRY_TIMEOUT = 1
68
DEFAULT_SCAN_TIMEOUT = 5
@@ -20,3 +22,14 @@ class SwitchbotModel(StrEnum):
2022
MOTION_SENSOR = "WoPresence"
2123
COLOR_BULB = "WoBulb"
2224
CEILING_LIGHT = "WoCeiling"
25+
LOCK = "WoLock"
26+
27+
28+
class LockStatus(Enum):
29+
LOCKED = 0b0000000
30+
UNLOCKED = 0b0010000
31+
LOCKING = 0b0100000
32+
UNLOCKING = 0b0110000
33+
LOCKING_STOP = 0b1000000
34+
UNLOCKING_STOP = 0b1010000
35+
NOT_FULLY_LOCKED = 0b1100000 # Only EU lock type

switchbot/devices/lock.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from __future__ import annotations

switchbot/discovery.py

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

104+
async def get_locks(self) -> dict[str, SwitchBotAdvertisement]:
105+
"""Return all WoLock/Locks devices with services data."""
106+
return await self._get_devices_by_model("o")
107+
104108
async def get_device_data(
105109
self, address: str
106110
) -> dict[str, SwitchBotAdvertisement] | None:

0 commit comments

Comments
 (0)