11"""Library to handle connection with Switchbot Lock."""
22from __future__ import annotations
33
4- import asyncio
54import base64
65import hashlib
76import hmac
87import json
98import logging
9+ import time
1010from typing import Any
1111
1212import boto3
@@ -162,6 +162,13 @@ async def unlock(self) -> bool:
162162 COMMAND_UNLOCK , {LockStatus .UNLOCKED , LockStatus .UNLOCKING }
163163 )
164164
165+ def _parse_basic_data (self , basic_data : bytes ) -> dict [str , Any ]:
166+ """Parse basic data from lock."""
167+ return {
168+ "battery" : basic_data [1 ],
169+ "firmware" : basic_data [2 ] / 10.0 ,
170+ }
171+
165172 async def _lock_unlock (
166173 self , command : str , ignore_statuses : set [LockStatus ]
167174 ) -> bool :
@@ -174,9 +181,15 @@ async def _lock_unlock(
174181
175182 await self ._enable_notifications ()
176183 result = await self ._send_command (command )
177- if not self ._check_command_result (result , 0 , {1 }):
178- return False
179- return True
184+ status = self ._check_command_result (result , 0 , {1 })
185+
186+ # Also update the battery and firmware version
187+ if basic_data := await self ._get_basic_info ():
188+ self ._last_full_update = time .monotonic ()
189+ self ._update_parsed_data (self ._parse_basic_data (basic_data ))
190+ self ._fire_callbacks ()
191+
192+ return status
180193
181194 async def get_basic_info (self ) -> dict [str , Any ] | None :
182195 """Get device basic status."""
@@ -188,10 +201,9 @@ async def get_basic_info(self) -> dict[str, Any] | None:
188201 if not basic_data :
189202 return None
190203
191- lock_data = self ._parse_lock_data (lock_raw_data [1 :])
192- lock_data .update (battery = basic_data [1 ], firmware = basic_data [2 ] / 10.0 )
193-
194- return lock_data
204+ return self ._parse_lock_data (lock_raw_data [1 :]) | self ._parse_basic_data (
205+ basic_data
206+ )
195207
196208 def is_calibrated (self ) -> Any :
197209 """Return True if lock is calibrated."""
0 commit comments