Skip to content

Commit a34072f

Browse files
authored
fix: ensure lock state is reflected when operated manually while connected (#173)
1 parent c7eb5a6 commit a34072f

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

switchbot/devices/device.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,20 @@ def _check_command_result(
552552
)
553553
return result[index] in values
554554

555-
def _update_parsed_data(self, new_data: dict[str, Any]) -> None:
556-
"""Update data."""
555+
def _update_parsed_data(self, new_data: dict[str, Any]) -> bool:
556+
"""Update data.
557+
558+
Returns true if data has changed and False if not.
559+
"""
557560
if not self._sb_adv_data:
558561
_LOGGER.exception("No advertisement data to update")
559562
return
560-
self._set_parsed_data(
561-
self._sb_adv_data,
562-
_merge_data(self._sb_adv_data.data.get("data") or {}, new_data),
563-
)
563+
old_data = self._sb_adv_data.data.get("data") or {}
564+
merged_data = _merge_data(old_data, new_data)
565+
if merged_data == old_data:
566+
return False
567+
self._set_parsed_data(self._sb_adv_data, merged_data)
568+
return True
564569

565570
def _set_parsed_data(
566571
self, advertisement: SwitchBotAdvertisement, data: dict[str, Any]

switchbot/devices/lock.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,13 @@ def _notification_handler(self, _sender: int, data: bytearray) -> None:
235235
super()._notification_handler(_sender, data)
236236

237237
def _update_lock_status(self, data: bytearray) -> None:
238-
data = self._decrypt(data[4:])
239-
lock_data = self._parse_lock_data(data)
240-
current_status = self.get_lock_status()
241-
if (
242-
lock_data["status"] != current_status or current_status not in REST_STATUSES
243-
) and (
244-
lock_data["status"] in REST_STATUSES
245-
or lock_data["status"] in BLOCKED_STATUSES
246-
):
247-
asyncio.create_task(self._disable_notifications())
248-
249-
self._update_parsed_data(lock_data)
250-
self._fire_callbacks()
238+
lock_data = self._parse_lock_data(self._decrypt(data[4:]))
239+
if self._update_parsed_data(lock_data):
240+
# We leave notifications enabled in case
241+
# the lock is operated manually before we
242+
# disconnect.
243+
self._reset_disconnect_timer()
244+
self._fire_callbacks()
251245

252246
@staticmethod
253247
def _parse_lock_data(data: bytes) -> dict[str, Any]:

0 commit comments

Comments
 (0)