Skip to content

fix(watch_list): ignore unchanged event entity refires - #6257

Open
Huyvux12 wants to merge 2 commits into
springfall2008:mainfrom
Huyvux12:fix/6105-watch-list-event-refire
Open

Huyvux12 wants to merge 2 commits into
springfall2008:mainfrom
Huyvux12:fix/6105-watch-list-event-refire

Conversation

@Huyvux12

Copy link
Copy Markdown
Contributor

Summary

A Home Assistant event.* entity in watch_list forced a full replan on
every 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 fingerprint
stored per watched entity, so both trigger paths (websocket state_changed
and loopback set_state_external in ha.py) share one rule:

  • event.* entities: fingerprint is json.dumps(attributes, sort_keys=True, default=str), ignoring the state.
  • Every other entity: fingerprint is the state, exactly as before — no
    behaviour change for sensors, and an attribute-only change still does not
    replan.
  • First sight of an entity still triggers, as now.

Proof

Base SHA on upstream: f2e544f.

Repro (new test apps/predbat/tests/test_watch_list_event.py, replays the
three refires from the log attached to #6102 — 05:11:14 / 05:26:14 / 05:41:13,
38-session events payload byte-identical, only the state timestamp moves):

Before the fix (./venv/bin/python ../apps/predbat/unit_test.py --test watch_list_event):

ERROR: Expected 1 replan from 3 identical refires, got 3
ERROR: Expected 2 replans (first sight + changed payload), got 4
ERROR: Expected 1 replan from 2 identical loopback refires, got 2
ERROR: Expected 1 replan from 2 attribute-less refires, got 2
ERROR: Expected 2 replans (first sight + state change), got 3
**** ERROR: Test watch_list_event FAILED

After the fix, same command — 8/8 sub-tests pass:

✓ 3 identical event refires caused only 1 replan (the first sight)
✓ Changed event payload replanned, identical refires did not
✓ Identical event refire via loopback shape caused only 1 replan
✓ Attribute-less event refire caused only 1 replan
✓ Raw event value change replanned, repeat did not
✓ Identical refire after config reload did not replan
✓ Sensor state change replanned, attribute-only change did not
✓ Unwatched entity did not invalidate the plan
**** Test watch_list_event PASSED ****

Control cases

  • Sensor state change (sensor.test, 100 → 200) still replans; same state
    with changed attributes does not — the non-event fingerprint is the state.
  • Changed events payload (one session appended) replans; repeating the
    changed payload does not.
  • An entity not in watch_list never triggers.
  • A non-dict new value for an event entity is fingerprinted as itself, so
    a 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: all
    pass.
  • Full suite (unit_test.py --quick): all pass — 1547 PASSED lines, 7
    slow tests skipped, total 714.67s, exit 0, no failures
    (coverage/../apps/predbat/unit_test.py --quick, run 2026-11-18).
  • Not run on this VM: full AppDaemon/HA end-to-end — no live HA instance
    here, so the websocket path is covered by unit tests through
    trigger_watch_list() only, not through a live socket.
  • pre-commit could 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 --check clean on all four touched files, ruff check with the repo's selected rules clean, flake8 shows only
    pre-existing findings on untouched functions, and the new code adds no new
    interrogate misses (the 3 misses in userinterface.py are pre-existing
    on main).

Diff scope and what was deliberately not changed

Risks checked and excluded

  • Fingerprint definition: the triage comment on watch_list: HA event entities force a replan on every refire, even with unchanged data #6105 parsed the actual
    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.
  • Stale fingerprints: bounded by watch_list size; removing then re-adding
    an entity with unchanged data correctly suppresses, with changed data
    correctly triggers. Restart triggers once on first sight, per the issue.
  • Duplicate watch_list entries now trigger once instead of twice
    (watch_event is idempotent, so this is a no-op behaviourally).

Closes #6105

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.
@github-actions github-actions Bot added the BOT_REVIEW Trigger an autotriage label Nov 18, 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 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.

Comment thread apps/predbat/userinterface.py Outdated
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)

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.

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.

Comment thread apps/predbat/userinterface.py Outdated
except (TypeError, ValueError):
return repr(new)
attributes = new.get("attributes", None)
if attributes is None and isinstance(attribute, dict):

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.

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()

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.

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.

@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Nov 18, 2026
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.
@Huyvux12

Copy link
Copy Markdown
Contributor Author

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 event.* entity the data is its attributes, for every other entity it is the state.

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 set_state_external); sensors are unchanged. I kept the gates rather than removing them for a single rule: they still filter the bulk of no-change traffic before the fingerprint is computed, and the event-attribute condition keeps the two rules consistent (dict equality at the gate, same attributes hashed by the fingerprint). Two new gate tests pin this — both failed on the old ha.py (0 calls) and pass now: an event attribute-only change reaches trigger_watch_list() on both paths, an identical one does not, and a sensor attribute-only change is still dropped.

getattr fallback — removed; trigger_watch_list() now reads self.watch_list_fingerprints directly. Every build path reaches PredBat.reset() (initialize_state(), create_headless_predbat(), create_predbat() in the tests).

Unreachable attributes is None fallback — dropped the fallback and the now-unused attribute parameter of watch_list_fingerprint(); the loopback test docstring now says what the test actually covers (the loopback call shape), not the fallback.

Config-reload test premise — the test now calls load_user_config(register=True) with args["watch_list"] set first, so watch_list is genuinely re-read and the check is not vacuous. This surfaced a fixture gap: TestHAInterface defines no websocket_active, which the register path reads — pinned to False in the test.

Full suite re-run on this head: all pass (1547 PASSED, 7 slow skipped, 580.80s, exit 0). black --check and ruff (repo's selected rules) clean on the touched files.

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.

watch_list: HA event entities force a replan on every refire, even with unchanged data

2 participants