fix: skip cached state override when bot ignores command (#213)#502
Draft
bluetoothbot wants to merge 2 commits into
Draft
fix: skip cached state override when bot ignores command (#213)#502bluetoothbot wants to merge 2 commits into
bluetoothbot wants to merge 2 commits into
Conversation
When the bot replies with 0x03 0xff 0x00 ("command ignored", typically on
back-to-back presses), `Switchbot.turn_on`/`turn_off` still called
`_override_state` unconditionally, so Home Assistant's cached state
flipped even though the device never actuated. Gate the override on the
result of `_check_command_result` so a rejected command leaves the
cached state alone.
Adds tests covering accepted (`0x01`/`0x05`) and rejected return values
for both turn_on and turn_off, plus an inverse-mode regression check.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
When
Switchbot.turn_on/turn_offget a "command ignored" reply (0x03 0xff 0x00, typically on back-to-back presses), don't flip the cachedisOnstate. Only override the state when_check_command_resultreturnsTrue.Why
Closes the latent half of #213. The bot's own protocol distinguishes acceptance (
0x01 0x48 0x90) from rejection (0x03 0xff 0x00) and_check_command_result(result, 0, {1, 5})already returnsFalsefor the rejection case — but the next line ranself._override_state({"isOn": True})unconditionally, so HA's cached state flipped tooneven when the device never actuated.That makes the back-to-back press behaviour observable in the issue: log shows
Turn on result: 03ff00 -> {'isOn': True}for the second press, and HA happily reports the bot asondespite no physical actuation. The 8.5s disconnect timer + a brief delay between commands is still the user-side workaround; this PR just stops the library from lying about state when the command is rejected.How
Two-line gate in
bot.py:Behaviour for the success path (notification byte 0x01 or 0x05) is unchanged.
Testing
tests/test_bot.pycovers turn_on/turn_off for both accepted and rejected results, plus an inverse-mode regression. (No existing test file for theSwitchbotbot device class.)1218 passed.🤖 Generated with Claude Code
Quality Report
Changes: 2 files changed, 111 insertions(+), 2 deletions(-)
Code scan: clean
Tests: passed (1218 passed)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline