fix: track lock _notifications_enabled state on enable#497
fix: track lock _notifications_enabled state on enable#497bluetoothbot wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
|
Looks like a real bug. I'm traveling so can't test on a real device. |
|
I am currently away on a business trip; once I return tomorrow, I will perform the testing on the device. |
zerzhang
left a comment
There was a problem hiding this comment.
The purpose of the _enable_notifications function is to enable the device to periodically report messages. For instance, when the device is being unlocked, its status transitions sequentially through "unlocking" and "unlocked"; conversely, when the BLE connection is lost, this status is reset.
There was a problem hiding this comment.
Pull request overview
This PR fixes notification state tracking for SwitchbotLock by updating _notifications_enabled after a successful enable, and avoiding redundant enable writes once notifications are already enabled. It also strengthens and expands tests to validate both success and failure paths for notification enabling.
Changes:
- Short-circuit
_enable_notifications()when notifications are already enabled. - Set
self._notifications_enabled = Trueonly when the enable command succeeds, and returnFalseon an unsuccessful result. - Update the existing enable-notifications test to assert state changes and no-op behavior, and add a failure-path test to ensure state remains
False.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| switchbot/devices/lock.py | Tracks _notifications_enabled on successful enable and skips re-sending the enable command when already enabled. |
| tests/test_lock.py | Tightens _enable_notifications test expectations and adds a failure-path test to confirm state is not flipped on unsuccessful enable. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Test re-enable after disconnect specifically. Flag never reset on connection loss. Verified: |
Real bug confirmed: flag never set True. But two parts. Setter fix is correct. Short-circuit is risky. Flag not reset on disconnect, so re-enable is skipped after the disconnect timer. Recommend dropping the short-circuit, keeping only the setter. Also |
PR Review — fix: track lock _notifications_enabled state on enableThe Strengths:
Needs attention:
🟡 Important
1. Short-circuit skips re-enable after a disconnect — flag is never reset on connection loss
|
_enable_notifications never set self._notifications_enabled = True, so the flag stayed False forever. Two consequences: - _disable_notifications short-circuits on `if not self._notifications_enabled`, meaning the disable command would never actually fire on the lock. - Every lock/unlock invocation re-sent the enable command instead of recognising notifications were already on. Set the flag on a successful response, return False on failure, and no-op when already enabled. Existing test only checked the return value; expand it to assert state plus a no-op second call, and add a failure-path test that the flag stays False if the lock rejects the command. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
6b91932 to
4bfd306
Compare
What
Set
_notifications_enabled = Trueon a successful_enable_notificationscall (and skip the BLE write when already enabled). Tighten the existing test and add a failure-path test.Why
SwitchbotLock._enable_notificationsonly returned the command result — it never updatedself._notifications_enabled. The flag therefore stayedFalsefor the lifetime of the device. That has two consequences:_disable_notificationsearly-returns onif not self._notifications_enabled: return True, so the disable command would never actually be sent to the lock.lock()/unlock()/unlock_without_unlatch()/half_lock()re-issues the enable command rather than recognising notifications are already on.Surfaced while looking at #489 (lock event subscription). The notification-tracking gap is unrelated to that question's root cause but is a real bug in its own right.
How
_enable_notifications: short-circuit when the flag is alreadyTrue; on a successful command, set the flag; on failure, returnFalseand leave the flagFalse.test_enable_notificationsnow asserts the flag flips toTrueand that a second call is a no-op (call count stays at 1).test_enable_notifications_failure_keeps_state_falsecovers the failure path.Testing
.venv/bin/python -m pytest -q→ 1085 passed.🤖 Generated with Claude Code
Quality Report
Changes: 2 files changed, 38 insertions(+), 2 deletions(-)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline