Skip to content

fix(hanchu): send a schedule change after the debounce, not the next tick (#6190) - #6216

Open
upton68 wants to merge 2 commits into
springfall2008:mainfrom
upton68:fix/hanchu-write-on-change
Open

upton68 wants to merge 2 commits into
springfall2008:mainfrom
upton68:fix/hanchu-write-on-change

Conversation

@upton68

@upton68 upton68 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

As suggested by @springfall2008 on #6214 (closed): the component-side half, shortening the write lag on its own.

Part of #6190 (component side only).

Problem

Predbat arms a charge window at full power and works out the low-power rate only once the window is running, then writes the rate with no press. Since #6077 a change that arrives with no write pending is left for the next reconcile tick, so in the #6190 log the 14:00 window ran at ~9.8 kW until 14:01:00: the rate was computed at 14:00:15, and ~44 s of the burst was the wait for the tick.

Fix

Once Predbat controls the device (after its first press), a schedule-entity change with no write pending now starts the debounced write itself (_restart_pending_write()), so it goes out ~hanchu_write_debounce (10 s) later. It is gated exactly as the reconcile tick: not before the first press, not in read-only, not with hanchu_control_enable off. An unchanged payload is still not sent, min_write_interval still paces writes, and a change while a write is pending still restarts it (#6074 sequence unchanged).

This roughly halves the burst (~60 s → ~25–35 s). It doesn't replace the executor fix of working out the rate before arming the window.

Tests

  • write_on_change (new): a 2,600 W rate written with no press on a controlled device goes out once after the debounce; two quick changes send one write with the last value. Fails on main.
  • write_debounce_change_only (extended): no write before Predbat's first press, in read-only, or with control disabled.
  • ./run_all --quick passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01K5medi1kA9icksXAqfi44v

…tick (springfall2008#6190)

Predbat arms a charge window at full power and works out the low-power rate
only once the window is running, then writes it with no press. A change with
no write pending was left for the next reconcile tick, so on a HESS-HY-S-10K
the window charged at ~10 kW for about a minute before CHG_PWR_LMT 2600
landed (~44 s of that was the wait for the tick).

Once Predbat controls the device, a schedule change with nothing pending now
starts the debounced write itself, gated as the reconcile tick is (not before
the first press, not in read-only, not with control disabled). An unchanged
payload is still not sent and min_write_interval still paces writes. This
roughly halves the burst; the executor fix (rate before arming) is separate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K5medi1kA9icksXAqfi44v
@github-actions github-actions Bot added the BOT_REVIEW Trigger an autotriage label Nov 15, 2026

@springfall2008 springfall2008 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Automated comment from the triage bot.

Reviewed 5bd04e2c (component-side half of #6190). 1 finding inline, 1 in this body.

The change itself reads sound: a schedule change on a device Predbat already controls now arms the debounce instead of waiting for the reconcile tick, the new branch is gated the same way _reconcile_control is, _write_payload_locked still suppresses an unchanged payload and hanchu_min_write_interval still paces the write. The new write_on_change test fails on main (no timer is armed, so nothing is sent), the extended write_debounce_change_only pins each of the three gate conditions, and both are registered under hanchu_control in TEST_REGISTRY. The #6074 coalescing test still holds because the pre-press stages restart the timer the press armed. I did not run any tests (the PR's code was reviewed by reading only).

docs/inverter-setup.md:762 - the write-pacing paragraph now describes the wrong behaviour

The Write pacing bullet says "All of Predbat's settings changes go to the cloud in one call per cycle, at most once per hanchu_min_write_interval seconds" and "Set it to 0 to write on every change". Both are contradicted by the change this PR makes, in exactly the case the PR is about:

  • A rate written with no press now goes out ~hanchu_write_debounce after the change, not on the cycle, so writes no longer happen only once per cycle.
  • With hanchu_write_debounce: 0 the documented "write on every change" does not happen for those no-press changes: _write_debounce_available() is false, so _restart_pending_write is never reached and the change still waits for the next reconcile tick - the very delay this PR removes.

A user reading that bullet to decide whether to lower the debounce would draw the opposite conclusion from the one the code now implements. It should say that a change to a device Predbat controls is written hanchu_write_debounce seconds after the change even with no button press, and that 0 leaves those changes to the reconcile tick.

What I checked and found nothing on: the 48-hour/midnight slot maths (untouched), apply_schedule's control_enable re-check at send time, mutable-default and late-binding traps (none), and that no task can be created on a foreign loop - _write_debounce_available() returns false unless the component's loop is running, so _schedule_write's get_running_loop() is always the component loop.

Comment thread apps/predbat/hanchu.py
await self._schedule_write(sn)
return True
return False
if sn not in self.control_active or self._is_read_only() or not self.control_enable:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Automated comment from the triage bot.

The read-only gate here is only evaluated when the change arrives. _debounced_write() sleeps write_debounce and then calls apply_schedule() unconditionally, and apply_schedule() re-checks control_enable but has no read-only check - so a write queued here goes out even if Predbat is put into read-only mode during the following write_debounce seconds (default 10).

Failure scenario: a rate change arrives while read-only is off, so a timer is armed; the user then turns on switch.predbat_set_read_only (or a plan does it) before the timer fires; apply_schedule() posts the iotSet anyway. That is the promise the switch makes - "Predbat will not write to the inverter" - broken by a write the component started itself, which is the case _reconcile_control()'s own docstring singles out as not covered by Predbat's plan-level read-only handling.

The docstring two lines above says "Gated exactly as the reconcile tick is (_reconcile_control)", but it is not quite: _reconcile_control() checks read-only immediately before it calls apply_schedule(), whereas this check is up to write_debounce seconds stale. Press-armed writes had the same shape before this PR, so the exposure is not new in kind - but this path now arms a timer on every schedule change, which widens it.

Suggested fix: re-check self._is_read_only() at the top of _debounced_write() (next to where it pops itself from write_debounce_tasks) and return without sending, so the change is picked up by the next reconcile tick once read-only is off - apply_schedule() already re-checks control_enable the same way. No test covers this: test_hanchu_read_only_blocks_the_reconcile_write only exercises the tick, and the read-only case in write_debounce_change_only sets read-only before the change, so the stale-check window is never entered.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Fixed in 118f1d7: _debounced_write() re-checks _is_read_only() when the timer fires and returns without sending, leaving the change in the local schedule for the reconcile tick once read-only is off (apply_schedule() already re-checks control_enable). This also closes the same window for press-armed writes. New test write_debounce_read_only turns read-only on during the debounce, checks nothing is sent, then that the tick sends it once read-only is off. It fails without the check. The docstring now says read-only is checked again when the timer fires.

@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Nov 15, 2026
… the write-pacing docs (springfall2008#6190)

- _debounced_write() checks read-only again when the timer fires and drops
  the write if it was turned on during the debounce: the gate that armed the
  timer is up to hanchu_write_debounce seconds stale. The change stays in the
  local schedule and the reconcile tick sends it once read-only is off. New
  test write_debounce_read_only.
- docs/inverter-setup.md: the write-pacing bullet now says a change to a
  controlled inverter, including the rates Predbat writes with no press, is
  sent hanchu_write_debounce seconds after it, and that 0 writes straight away
  on a press but leaves such changes to the next tick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K5medi1kA9icksXAqfi44v
@upton68

upton68 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. The write-pacing bullet in inverter-setup.md is corrected in 118f1d7: a change to a controlled inverter, including the rates Predbat writes with no press, is sent hanchu_write_debounce seconds after it, and 0 writes straight away on a press but leaves no-press changes to the reconcile tick (about once a minute).

This branch has not been deployed

No deployments
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.

3 participants