Skip to content

fix: skip cached state override when bot ignores command (#213)#502

Draft
bluetoothbot wants to merge 2 commits into
sblibs:masterfrom
bluetoothbot:koan/bot-skip-override-on-failed-press
Draft

fix: skip cached state override when bot ignores command (#213)#502
bluetoothbot wants to merge 2 commits into
sblibs:masterfrom
bluetoothbot:koan/bot-skip-override-on-failed-press

Conversation

@bluetoothbot
Copy link
Copy Markdown
Collaborator

@bluetoothbot bluetoothbot commented May 19, 2026

What

When Switchbot.turn_on/turn_off get a "command ignored" reply (0x03 0xff 0x00, typically on back-to-back presses), don't flip the cached isOn state. Only override the state when _check_command_result returns True.

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 returns False for the rejection case — but the next line ran self._override_state({"isOn": True}) unconditionally, so HA's cached state flipped to on even 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 as on despite 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:

ret = self._check_command_result(result, 0, {1, 5})
if ret:
    self._override_state({"isOn": True})  # or False, for turn_off

Behaviour for the success path (notification byte 0x01 or 0x05) is unchanged.

Testing

  • New tests/test_bot.py covers turn_on/turn_off for both accepted and rejected results, plus an inverse-mode regression. (No existing test file for the Switchbot bot device class.)
  • Full suite: 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

bluetoothbot and others added 2 commits May 19, 2026 18:08
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
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
switchbot/devices/bot.py 74.62% <100.00%> (+30.01%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant