Skip to content

Commit 5acd308

Browse files
authored
Fix periodic reporting status for lock series (#372)
1 parent 44ae30b commit 5acd308

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

switchbot/devices/lock.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
SwitchbotModel.LOCK_PRO: f"{COMMAND_HEADER}0f4e0101000000",
3838
SwitchbotModel.LOCK_ULTRA: f"{COMMAND_HEADER}0f4e0101000000",
3939
}
40-
COMMAND_ENABLE_NOTIFICATIONS = f"{COMMAND_HEADER}0e01001e00008101"
40+
COMMAND_ENABLE_NOTIFICATIONS = {
41+
SwitchbotModel.LOCK: f"{COMMAND_HEADER}0e01001e00008101",
42+
SwitchbotModel.LOCK_LITE: f"{COMMAND_HEADER}0e01001e00008101",
43+
SwitchbotModel.LOCK_PRO: f"{COMMAND_HEADER}0e01001e00008104",
44+
SwitchbotModel.LOCK_ULTRA: f"{COMMAND_HEADER}0e01001e00008107",
45+
}
4146
COMMAND_DISABLE_NOTIFICATIONS = f"{COMMAND_HEADER}0e00"
4247

4348
MOVING_STATUSES = {LockStatus.LOCKING, LockStatus.UNLOCKING}
@@ -197,12 +202,8 @@ async def _get_lock_info(self) -> bytes | None:
197202
return _data
198203

199204
async def _enable_notifications(self) -> bool:
200-
if self._notifications_enabled:
201-
return True
202-
result = await self._send_command(COMMAND_ENABLE_NOTIFICATIONS)
203-
if self._check_command_result(result, 0, COMMAND_RESULT_EXPECTED_VALUES):
204-
self._notifications_enabled = True
205-
return self._notifications_enabled
205+
result = await self._send_command(COMMAND_ENABLE_NOTIFICATIONS[self._model])
206+
return self._check_command_result(result, 0, COMMAND_RESULT_EXPECTED_VALUES)
206207

207208
async def _disable_notifications(self) -> bool:
208209
if not self._notifications_enabled:
@@ -213,7 +214,7 @@ async def _disable_notifications(self) -> bool:
213214
return not self._notifications_enabled
214215

215216
def _notification_handler(self, _sender: int, data: bytearray) -> None:
216-
if self._notifications_enabled and self._check_command_result(data, 0, {0xF}):
217+
if self._check_command_result(data, 0, {0xF}):
217218
if self._expected_disconnect:
218219
_LOGGER.debug(
219220
"%s: Ignoring lock notification during expected disconnect",

tests/test_lock.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,6 @@ async def test_enable_notifications(model: str):
372372
with patch.object(device, "_send_command", return_value=b"\x01\x00"):
373373
result = await device._enable_notifications()
374374
assert result is True
375-
assert device._notifications_enabled is True
376-
377-
378-
@pytest.mark.asyncio
379-
@pytest.mark.parametrize(
380-
"model",
381-
[
382-
SwitchbotModel.LOCK,
383-
SwitchbotModel.LOCK_LITE,
384-
SwitchbotModel.LOCK_PRO,
385-
SwitchbotModel.LOCK_ULTRA,
386-
],
387-
)
388-
async def test_enable_notifications_already_enabled(model: str):
389-
"""Test _enable_notifications when already enabled."""
390-
device = create_device_for_command_testing(model)
391-
device._notifications_enabled = True
392-
with patch.object(device, "_send_command") as mock_send:
393-
result = await device._enable_notifications()
394-
assert result is True
395-
mock_send.assert_not_called()
396375

397376

398377
@pytest.mark.asyncio
@@ -467,7 +446,7 @@ def test_notification_handler_not_enabled(model: str):
467446
"""Test _notification_handler when notifications not enabled."""
468447
device = create_device_for_command_testing(model)
469448
device._notifications_enabled = False
470-
data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
449+
data = bytearray(b"\x01\x00\x00\x00\x80\x00\x00\x00\x00\x00")
471450
with (
472451
patch.object(device, "_update_lock_status") as mock_update,
473452
patch.object(

0 commit comments

Comments
 (0)