Repository navigation
Skip push and poll updates for entities without hass - #146
Conversation
Entities configured with "enabled: False", such as the "rbt" reboot timer sensor, are aborted by Home Assistant with add_to_platform_abort(). That leaves hass as None and never assigns registry_entry. The platform setup code still registers them in push_entities, so every pushed value reached async_write_ha_state() and raised: RuntimeError: Attribute hass is None for <entity unknown.unknown=unknown> On a charger pushing once per second this produced roughly one error per second for each disabled push entity. The existing "if not self.enabled" guard cannot catch this, because Entity.enabled is derived from registry_entry: return self.registry_entry is None or not self.registry_entry.disabled For an entity that was never added, registry_entry is None, so enabled returns True and the guard falls through in exactly the case it was meant to protect against. Check self.hass as well in async_local_push(), and add the same guard to async_local_poll(), which had none and is reachable from async_local_push() when the validated state is None. The guard is evaluated per update, so an entity enabled later through the UI starts updating without a reload, and normally added entities are unaffected. Fixing this in the shared base class covers all seven platforms that populate push_entities. Skipping registration in those platforms instead was rejected: it would permanently disable push updates for an entity the user enables later, until the integration is reloaded. Adds regression tests covering the disabled-entity case for both methods and asserting that added entities still write state. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Anil Belur <askb23@gmail.com>
|
CI here is held at In the meantime I ran the identical workflow on my fork against this exact commit — all 7 jobs green, including HA Flow Tests against a real Home Assistant core and unit tests on 3.13 / 3.14 / 3.15: https://github.com/askb/wattpilot-HA/actions/runs/40418531362 One note in case you look at the first attempt on that run: HACS Validation initially failed with |
Fixes #144
Cause
Entities configured with
enabled: False, such as therbtreboot timer, are aborted by Home Assistant withadd_to_platform_abort(). That leaveshassasNoneand never assignsregistry_entry. The platform setup code still registers them inpush_entities, so every pushed value reachedasync_write_ha_state()and raised roughly once per second per disabled push entity.The existing guard cannot catch this, because
Entity.enabledis derived fromregistry_entry:For an entity that was never added,
registry_entry is None, soenabledreturnsTrueand the guard falls through in exactly the case it was meant to protect against.Change
custom_components/wattpilot/entities.pyasync_local_push(): also checkself.hassasync_local_poll(): add the same guard; it had none and is reachable fromasync_local_push()when the validated state isNoneFixing this in the shared base class covers all seven platforms that populate
push_entities.The guard is evaluated per update, so an entity that the user enables later through the UI starts updating without a reload, and normally added entities are unaffected.
Alternative that was rejected
The issue also suggested skipping registration in the platforms:
I did not take this.
enabledin the YAML is only the initial registry state, so an entity the user enables later through the UI would never have a push subscription and would silently stop receiving updates until the integration is reloaded. The runtime guard has no such failure mode.Verification
Both CI suites, run locally with the same flags as
ci.yml, on Python 3.14:The new tests fail without the source change and pass with it:
test_push_does_not_write_state_when_hass_is_nonetest_poll_does_not_write_state_when_hass_is_nonetest_push_does_not_log_an_error_when_hass_is_nonetest_push_still_writes_state_for_an_added_entityThe last one passes either way by design — it guards against the fix suppressing legitimate updates.
Both claims about
Entity.enabledwere checked against a real Home Assistant core (2026.10.4) rather than the test stub:That
RuntimeErrortext matches the reported log line exactly, including<entity unknown.unknown=unknown>.Not changed
manifest.json— per AGENTS.md 4.6 the branch is already0.4.1and the latest release isv0.4.0, which is lower, so it must not be updated.Not verified
I could not confirm the fix on a physical charger, since that needs a Core restart on my production instance. The reproduction was confirmed there before the change: with the
loggersuppression lifted for 45 s, 23 errors appeared, all forrbt, 1.005 s apart.Minor observation
tests/conftest.pystubs_Entity.enabled = Trueas a plain class attribute rather than modelling theregistry_entryproperty. That happens to match real behaviour for a never-added entity, which is why these tests are meaningful, but it would not catch a regression involving an entity disabled after being added. Happy to modelregistry_entryin the stub if you would like that, though it touches shared test scaffolding so I left it out of this change.