diff --git a/switchbot/devices/lock.py b/switchbot/devices/lock.py index 275e6280..5564c742 100644 --- a/switchbot/devices/lock.py +++ b/switchbot/devices/lock.py @@ -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: diff --git a/tests/test_lock.py b/tests/test_lock.py index d47a1e99..93441ef7 100644 --- a/tests/test_lock.py +++ b/tests/test_lock.py @@ -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