Skip to content

fix(ctw3): correct CMD 221 byte order - LED fields were off-by-one#70

Merged
aavdberg merged 1 commit into
devfrom
fix/ctw3-cmd221-led-byte-order
May 3, 2026
Merged

fix(ctw3): correct CMD 221 byte order - LED fields were off-by-one#70
aavdberg merged 1 commit into
devfrom
fix/ctw3-cmd221-led-byte-order

Conversation

@aavdberg

@aavdberg aavdberg commented May 3, 2026

Copy link
Copy Markdown
Owner

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.

# Payload (bytes 6..9)
1 0 1 1 0
2 0 0 5 0
3 0 1 8 0
4 0 1 9 0
5 0 0 8 0

Under 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_ctw3 and 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_enabled by 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

  • New builder regression test covering all 5 captured payloads.
  • New parser round-trip test for the same sequence + a full-field test with DND/lock set.
  • Updated 2 existing tests to the corrected layout.
  • All 97 tests pass; ruff clean.

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.

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>
Copilot AI review requested due to automatic review settings May 3, 2026 17:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_enabled is at index 6, led_switch at 7, and led_brightness at 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.

@aavdberg aavdberg merged commit 775c808 into dev May 3, 2026
7 checks passed
@aavdberg aavdberg deleted the fix/ctw3-cmd221-led-byte-order branch May 3, 2026 17:22
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.

2 participants