Skip to content

Skip push and poll updates for entities without hass - #146

Merged
mk-maddin merged 1 commit into
mk-maddin:masterfrom
askb:issue_144
Oct 27, 2026
Merged

mk-maddin merged 1 commit into
mk-maddin:masterfrom
askb:issue_144

Conversation

@askb

@askb askb commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #144

Cause

Entities configured with enabled: False, such as the rbt reboot timer, 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 roughly once per second per disabled push entity.

The existing 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.

Change

custom_components/wattpilot/entities.py

  • async_local_push(): also check self.hass
  • async_local_poll(): add the same guard; it had none and is reachable from async_local_push() when the validated state is None

Fixing 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:

if entity._source == 'property' and entity_cfg.get('enabled', True):
    push_entities[entity._identifier] = entity

I did not take this. enabled in 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:

$ pytest tests/ -v --tb=short
72 passed

$ pytest tests_ha/ -v --tb=short
4 passed

The new tests fail without the source change and pass with it:

Test without fix with fix
test_push_does_not_write_state_when_hass_is_none FAILED PASSED
test_poll_does_not_write_state_when_hass_is_none FAILED PASSED
test_push_does_not_log_an_error_when_hass_is_none FAILED PASSED
test_push_still_writes_state_for_an_added_entity PASSED PASSED

The last one passes either way by design — it guards against the fix suppressing legitimate updates.

Both claims about Entity.enabled were checked against a real Home Assistant core (2026.10.4) rather than the test stub:

never-added entity -> hass=None registry_entry=None enabled=True
async_write_ha_state() -> RuntimeError: Attribute hass is None for <entity unknown.unknown=unknown>

That RuntimeError text matches the reported log line exactly, including <entity unknown.unknown=unknown>.

Not changed

  • manifest.json — per AGENTS.md 4.6 the branch is already 0.4.1 and the latest release is v0.4.0, which is lower, so it must not be updated.
  • Documentation — no installation, configuration or user-visible behaviour changes.
  • No comments added to source, per AGENTS.md 4.5.

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 logger suppression lifted for 45 s, 23 errors appeared, all for rbt, 1.005 s apart.

Minor observation

tests/conftest.py stubs _Entity.enabled = True as a plain class attribute rather than modelling the registry_entry property. 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 model registry_entry in the stub if you would like that, though it touches shared test scaffolding so I left it out of this change.

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>
@askb

askb commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

CI here is held at action_required, since this is my first contribution to the repo and the workflow needs your approval to run.

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 does not have issues enabled and no valid topics. Both are repository-settings checks against the fork, not code checks — every content validation (license, brands, integration_manifest, hacsjson, description, information, archived) passed on that attempt too. I enabled issues and set topics on the fork and re-ran, and it went green.

@mk-maddin
mk-maddin merged commit 00f593e into mk-maddin:master Oct 27, 2026
7 checks passed
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.

Disabled entities (e.g. rbt / Reboot Timer) log "Attribute hass is None" ~1x/second

2 participants