Skip to content

Commit c7f1228

Browse files
authored
Update the battery and firmware version after lock operation (#178)
1 parent ebc5b7e commit c7f1228

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

switchbot/const.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class SwitchbotAuthenticationError(RuntimeError):
2020

2121
class SwitchbotAccountConnectionError(RuntimeError):
2222
"""Raised when connection to Switchbot account fails.
23-
23+
2424
This exception inherits from RuntimeError to avoid breaking existing code
25-
but will be changed to Exception in a future release.
25+
but will be changed to Exception in a future release.
2626
"""
2727

2828

switchbot/devices/lock.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Library to handle connection with Switchbot Lock."""
22
from __future__ import annotations
33

4-
import asyncio
54
import base64
65
import hashlib
76
import hmac
87
import json
98
import logging
9+
import time
1010
from typing import Any
1111

1212
import 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

Comments
 (0)