fix(ctw3): correct CMD 221 byte order - LED fields were off-by-one#70
Merged
Conversation
The CTW3 CMD 221 (write settings) payload had led_switch / led_brightness / dnd_enabled in the wrong positions. Real-device captures (debug log 2026-05-03 18:52:13-48, user actions: LED on -> brightness changes -> LED off) show the correct layout is: [smart_work, smart_sleep, batt_w_hi, batt_w_lo, batt_s_hi, batt_s_lo, dnd_enabled, led_switch, led_brightness, child_lock] Previously led_switch was always sent at index 6 (wrong), so user clicks on the LED switch had inconsistent effects. Brightness slider values 1/5/8/9 showed up at index 8 where the integration thought it was writing dnd_enabled. _parse_config_ctw3 was kept symmetric (currently dead code on CTW3 fw 111 which never replies to CMD 211, but ready for future firmware). Adds: - Builder regression test covering the 5 captured payloads. - Parser round-trip test using the same sequence. - Updated existing parser tests to the corrected layout. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes CTW3 settings writes by correcting the byte offsets used in the CMD 221 settings payload (and keeping the CMD 211 parser symmetric), addressing observed LED switch/brightness inconsistencies on real hardware.
Changes:
- Rotates CTW3 settings payload fields so
dnd_enabledis at index 6,led_switchat 7, andled_brightnessat 8. - Updates the CTW3 CMD 211 config parser to match the corrected layout.
- Adds/updates regression and round-trip tests based on captured real-device payloads; bumps integration patch version.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
custom_components/petkit_ble/protocol.py |
Fixes CTW3 CMD 221 settings payload field ordering; documents reverse-engineered layout. |
custom_components/petkit_ble/ble_client.py |
Updates _parse_config_ctw3 to match the corrected CTW3 layout and clarifies docstring. |
tests/test_protocol.py |
Adds regression coverage for captured CTW3 payload sequences and builder/parser symmetry. |
tests/test_data_model.py |
Updates CTW3 config parsing test to the corrected byte positions. |
tests/test_settings_optimistic.py |
Updates CTW3 layout comment + payload used in config_loaded flag test. |
custom_components/petkit_ble/manifest.json |
Bumps version from 1.1.14 to 1.1.15. |
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.
Problem
When toggling the LED on a CTW3 fountain in Home Assistant, behaviour was inconsistent: the switch appeared to toggle but settings drifted, and the brightness slider didn't behave predictably. Captured CMD 221 frames in debug log + "home-assistant_petkit_ble_2026-05-03T16-52-48.529Z.log" + show the integration was writing fields at the wrong byte offsets.
Evidence
User actions during 18:52:13 – 18:52:48:
LED on -> brightness 1/5/8/9 -> LED off.0 1 1 00 0 5 00 1 8 00 1 9 00 0 8 0Under the previous layout (idx[6]=led_switch, idx[7]=led_brightness, idx[8]=dnd_enabled), led_switch was always 0 and dnd_enabled took values 1/5/8/9/8 — both impossible for the user actions performed. The pattern only makes sense if led_switch is at idx 7 and led_brightness at idx 8.
Fix
Rotate three fields in
build_settings_payload_ctw3and mirror the rotation in_parse_config_ctw3. New CTW3 layout:[smart_work, smart_sleep, batt_w_hi, batt_w_lo, batt_s_hi, batt_s_lo, dnd_enabled, led_switch, led_brightness, child_lock]idx[6] is assumed to be
dnd_enabledby analogy with the W5/CTW2 layout — the captures always show 0 there so this is documented as an assumption to revisit once a CTW3 firmware actually responds to CMD 211.Tests
Out of scope
Verification
After this is pre-released, please toggle the LED switch and adjust the brightness slider on the real CTW3 and confirm the LED state stays consistent.