Repository navigation
Conversation
An HA event entity's state is the timestamp of its last fire, so every refire invalidated the plan even when the event data was unchanged. trigger_watch_list() now keys on a fingerprint stored per watched entity: a hash of the attributes for event entities, the state for everything else. First sight still triggers.
springfall2008
left a comment
There was a problem hiding this comment.
Automated comment from the triage bot.
Reviewed head 186ed0a4 (base f2e544f1). The fix is sound: the fingerprint gate closes the #6105 mechanism for both trigger paths, first sight still triggers, the non-event fingerprint is the state so sensor behaviour is unchanged, and the new tests are discriminating (the three-refire case yields 3 without the gate and 1 with it). 3 findings inline, 2 in the body. None is a correctness bug in the new code.
Missing docs - docs/apps-yaml.md, "Watch List - automatically start Predbat execution" (around line 2606). This changes user-visible watch_list behaviour: event.* entries are now judged on their attributes rather than their state, and for every entity an identical refire no longer re-plans. The section says only "if one of those changes, Predbat will automatically start executing" and then carries per-entity hints (car_charging_now, octopus_saving_session, car_charging_soc); a user who notices a watched event entity no longer forcing a re-plan has nothing to read that explains it. CLAUDE.md requires a docs update for changed behaviour, and this is the page that already documents when a watch entry is or is not worth adding - the natural place for one sentence on identical refires being ignored.
Altitude - the fingerprint sits under the old state-only gates (apps/predbat/ha.py:707-708 and apps/predbat/ha.py:1191). Both callers still test the state before calling trigger_watch_list(), so the new rule only sees what those gates let through. For an event.* entity whose attributes change while its state does not - HA fires state_changed for an attribute-only change, and the Octopus events list is trimmed to its maximum as well as appended to - the ha.py gate drops the event and the fingerprint is never consulted, so the docstring's "their fingerprint is a hash of the attributes" does not hold for that case. For every non-event entity the fingerprint is the state, which makes the ha.py comparison redundant, so replacing it with the trigger_watch_list() call would leave one rule instead of two that can drift. I could not confirm any integration that updates an event entity's attributes without refiring, so treat this as an opportunity opened up by the change rather than a demonstrated failure.
| handler and the loopback set_state_external path in ha.py - share one | ||
| rule and no longer depend on what HA reports as the old state. | ||
| """ | ||
| fingerprints = getattr(self, "watch_list_fingerprints", None) |
There was a problem hiding this comment.
Automated comment from the triage bot.
getattr is not needed here: watch_list_fingerprints is initialised unconditionally in PredBat.reset() (predbat.py:368), and every build path reaches it - initialize_state() calls reset(), and create_predbat() in the tests calls it explicitly - so the attribute always exists by the time trigger_watch_list() can run. CLAUDE.md's rule is no getattr() without a very good reason, and this one has a bad failure mode rather than a harmless one: if that initialisation is ever removed, or a host path skips reset(), the fallback silently recreates an empty dict on first use and every refire looks like a first sight - exactly the bug this PR fixes - with no error anywhere. Prefer a plain fingerprints = self.watch_list_fingerprints, or state in the code why the attribute may legitimately be absent.
| except (TypeError, ValueError): | ||
| return repr(new) | ||
| attributes = new.get("attributes", None) | ||
| if attributes is None and isinstance(attribute, dict): |
There was a problem hiding this comment.
Automated comment from the triage bot.
This fallback is unreachable through both callers, so it is dead code. The websocket path always passes HA's new_state, which always carries an attributes key, and set_state_external() builds new_state = {"entity_id", "state", "attributes"} (ha.py:1110-1113) with attributes defaulted to {} - never None. So attributes is None never holds outside the attribute-less test case, and test_watch_list_event_loopback_shape does not reach it either: its docstring claims "the loopback path passes the attributes dict as attribute", but that test also passes the attributes inside new, which is exactly what the real loopback path does. Either drop the fallback (and the misleading test comment), or add a caller/test that genuinely passes a dict attribute with no attributes in new.
| triggered = 0 | ||
| if _fire(my_predbat, EVENT_ENTITY, None, None, first): | ||
| triggered += 1 | ||
| my_predbat.load_user_config() |
There was a problem hiding this comment.
Automated comment from the triage bot.
load_user_config() defaults to register=False, and self.watch_list is only re-read under if register: (userinterface.py:1492-1495), so this call never reloads the watch list. The test's premise - that load_user_config() runs on every update cycle and could clobber the fingerprints - is therefore not what it exercises, and the EVENT_ENTITY not in my_predbat.watch_list check a few lines below is vacuous because the test assigned that attribute itself. Nothing in load_user_config() has ever touched fingerprints, so the case is a useful guard against a future regression and it is still discriminating for the fingerprint gate itself; it just does not reproduce the failure it describes. Passing register=True would make it cover the cycle that actually re-reads watch_list.
The fingerprint in trigger_watch_list() sat under the state-only gates in ha.py, so an event entity whose attributes changed while its state did not never reached it. Both gates now also let an event entity through when its attributes change; sensors are unchanged. Also drops the dead attribute fallback and the getattr fallback in trigger_watch_list(), makes the config-reload test use load_user_config(register=True), and documents the identical-refire behaviour in docs/apps-yaml.md.
|
Thanks for the review — all 5 findings addressed in 2e3b5b9: Missing docs — added a paragraph to the Watch List section in docs/apps-yaml.md: an identical update does not start execution; for an Altitude — fingerprint under the state-only gates — this was a real gap: an event entity whose attributes change while its state does not was dropped by the ha.py gates before the fingerprint was ever consulted, so the docstring over-claimed. Both gates now also let an event entity through when its attributes change (ha.py websocket path and
Unreachable Config-reload test premise — the test now calls Full suite re-run on this head: all pass (1547 PASSED, 7 slow skipped, 580.80s, exit 0). |
Summary
A Home Assistant
event.*entity inwatch_listforced a full replan onevery refire, even when the event data had not changed: for an event entity
the state is just the timestamp of the last fire. With the Octopus free
electricity session event this meant a pointless replan every ~15 minutes.
The change test now lives in
trigger_watch_list(), keyed on a fingerprintstored per watched entity, so both trigger paths (websocket
state_changedand loopback
set_state_externalinha.py) share one rule:event.*entities: fingerprint isjson.dumps(attributes, sort_keys=True, default=str), ignoring the state.behaviour change for sensors, and an attribute-only change still does not
replan.
Proof
Base SHA on upstream:
f2e544f.Repro (new test
apps/predbat/tests/test_watch_list_event.py, replays thethree refires from the log attached to #6102 — 05:11:14 / 05:26:14 / 05:41:13,
38-session
eventspayload byte-identical, only the state timestamp moves):Before the fix (
./venv/bin/python ../apps/predbat/unit_test.py --test watch_list_event):After the fix, same command — 8/8 sub-tests pass:
Control cases
sensor.test, 100 → 200) still replans; same statewith changed attributes does not — the non-event fingerprint is the state.
eventspayload (one session appended) replans; repeating thechanged payload does not.
watch_listnever triggers.newvalue for an event entity is fingerprinted as itself, soa change is never silently swallowed (unreachable via the current callers,
both of which always pass state dicts).
Regression scope
watch_list_event,hainterface_websocket,hainterface_service: allpass.
unit_test.py --quick): all pass — 1547 PASSED lines, 7slow tests skipped, total 714.67s, exit 0, no failures
(
coverage/../apps/predbat/unit_test.py --quick, run 2026-11-18).here, so the websocket path is covered by unit tests through
trigger_watch_list()only, not through a live socket.pre-commitcould not run on this VM (hook environments fail to fetch:gitlab returns 403 from this host), so CI is the check for it. Locally
verified instead:
black --checkclean on all four touched files,ruff checkwith the repo's selected rules clean,flake8shows onlypre-existing findings on untouched functions, and the new code adds no new
interrogatemisses (the 3 misses inuserinterface.pyare pre-existingon main).
Diff scope and what was deliberately not changed
apps/predbat/userinterface.py: newwatch_list_fingerprint()+fingerprint gate in
trigger_watch_list().apps/predbat/predbat.py:watch_list_fingerprints = {}inreset()only —
load_user_config()runs on every update cycle, so resetting thefingerprints there would make every refire look like a first sight and
defeat the check (pinned by a test).
apps/predbat/tests/test_watch_list_event.py(new) +apps/predbat/unit_test.py(registry entry).ha.pystate-change gates,watch_event()itself, and issues GS_fb00 v9.7.8: battery keeps charging ~5 min after IOG off-peak ends because 05:30 cycle reuses 05:25 plan #6102 / Plan: a charge window merged across an import rate rise keeps grid-charging at the higher rate when the battery falls short of the model #6108 / IOG Dispatches attribute in watchlist #2773 (out of scope for this PR).
Risks checked and excluded
three
Watched event:lines and states only the refire timestamp changed,so hashing all attributes matches the logged evidence. If some other
integration's event entity carried a per-fire volatile attribute, the fix
would degrade gracefully to today's behaviour (extra replan), never a
missed replan.
watch_listsize; removing then re-addingan entity with unchanged data correctly suppresses, with changed data
correctly triggers. Restart triggers once on first sight, per the issue.
watch_listentries now trigger once instead of twice(
watch_eventis idempotent, so this is a no-op behaviourally).Closes #6105