Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion switchbot/devices/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ async def _get_lock_info(self) -> bytes | None:

async def _enable_notifications(self) -> bool:
result = await self._send_command(COMMAND_ENABLE_NOTIFICATIONS[self._model])
return self._check_command_result(result, 0, COMMAND_RESULT_EXPECTED_VALUES)
if self._check_command_result(result, 0, COMMAND_RESULT_EXPECTED_VALUES):
self._notifications_enabled = True
return True
return False

async def _disable_notifications(self) -> bool:
if not self._notifications_enabled:
Expand Down
27 changes: 26 additions & 1 deletion tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,34 @@ async def test_get_lock_info_failure(model: str):
async def test_enable_notifications(model: str):
"""Test _enable_notifications method."""
device = create_device_for_command_testing(model)
with patch.object(device, "_send_command", return_value=b"\x01\x00"):
assert device._notifications_enabled is False
with patch.object(device, "_send_command", return_value=b"\x01\x00") as mock_send:
result = await device._enable_notifications()
assert result is True
assert device._notifications_enabled is True
assert mock_send.call_count == 1


@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.LOCK,
SwitchbotModel.LOCK_LITE,
SwitchbotModel.LOCK_PRO,
SwitchbotModel.LOCK_ULTRA,
SwitchbotModel.LOCK_VISION,
SwitchbotModel.LOCK_VISION_PRO,
SwitchbotModel.LOCK_PRO_WIFI,
],
)
async def test_enable_notifications_failure_keeps_state_false(model: str):
"""Failed enable command must not flip the tracked state to True."""
device = create_device_for_command_testing(model)
with patch.object(device, "_send_command", return_value=b"\x00\x00"):
result = await device._enable_notifications()
assert result is False
assert device._notifications_enabled is False


@pytest.mark.asyncio
Expand Down
Loading