diff --git a/switchbot/const.py b/switchbot/const.py index 4372caf5..fec505c6 100644 --- a/switchbot/const.py +++ b/switchbot/const.py @@ -20,9 +20,9 @@ class SwitchbotAuthenticationError(RuntimeError): class SwitchbotAccountConnectionError(RuntimeError): """Raised when connection to Switchbot account fails. - + This exception inherits from RuntimeError to avoid breaking existing code - but will be changed to Exception in a future release. + but will be changed to Exception in a future release. """ diff --git a/switchbot/devices/lock.py b/switchbot/devices/lock.py index 676a2614..9f7058a9 100644 --- a/switchbot/devices/lock.py +++ b/switchbot/devices/lock.py @@ -1,12 +1,12 @@ """Library to handle connection with Switchbot Lock.""" from __future__ import annotations -import asyncio import base64 import hashlib import hmac import json import logging +import time from typing import Any import boto3 @@ -162,6 +162,13 @@ async def unlock(self) -> bool: COMMAND_UNLOCK, {LockStatus.UNLOCKED, LockStatus.UNLOCKING} ) + def _parse_basic_data(self, basic_data: bytes) -> dict[str, Any]: + """Parse basic data from lock.""" + return { + "battery": basic_data[1], + "firmware": basic_data[2] / 10.0, + } + async def _lock_unlock( self, command: str, ignore_statuses: set[LockStatus] ) -> bool: @@ -174,9 +181,15 @@ async def _lock_unlock( await self._enable_notifications() result = await self._send_command(command) - if not self._check_command_result(result, 0, {1}): - return False - return True + status = self._check_command_result(result, 0, {1}) + + # Also update the battery and firmware version + if basic_data := await self._get_basic_info(): + self._last_full_update = time.monotonic() + self._update_parsed_data(self._parse_basic_data(basic_data)) + self._fire_callbacks() + + return status async def get_basic_info(self) -> dict[str, Any] | None: """Get device basic status.""" @@ -188,10 +201,9 @@ async def get_basic_info(self) -> dict[str, Any] | None: if not basic_data: return None - lock_data = self._parse_lock_data(lock_raw_data[1:]) - lock_data.update(battery=basic_data[1], firmware=basic_data[2] / 10.0) - - return lock_data + return self._parse_lock_data(lock_raw_data[1:]) | self._parse_basic_data( + basic_data + ) def is_calibrated(self) -> Any: """Return True if lock is calibrated."""