Skip to content

fix(#32): keep helper alive when a source sensor disappears#38

Open
AlexMKX wants to merge 5 commits into
andrew-codechimp:mainfrom
AlexMKX:fix/preserve-helper-on-source-removal
Open

fix(#32): keep helper alive when a source sensor disappears#38
AlexMKX wants to merge 5 commits into
andrew-codechimp:mainfrom
AlexMKX:fix/preserve-helper-on-source-removal

Conversation

@AlexMKX

@AlexMKX AlexMKX commented May 29, 2026

Copy link
Copy Markdown

Fixes #32.

Problem

async_registry_updated removes the helper's own ConfigEntry whenever a source sensor (motion or door) is removed from the entity registry. This is what users see as 'the helper disappears after restart' — typically a Zigbee/MQTT device is rebuilt or re-paired, the source entity is briefly absent, and the helper is then permanently gone with all dependent automations broken.

async_setup_entry also returns False if a source entity_id is not in the registry at startup, marking the entry as setup_error.

Fix

  • async_registry_updated now reloads the entry on source removal instead of removing it. The helper stays; its binary_sensor becomes unavailable until the source returns.
  • async_setup_entry no longer hard-fails on a missing source. It logs a warning (now labeled as 'Motion' / 'Door' so it's obvious which one is missing) and proceeds — the existing async_track_state_change_event listener picks up the entity when it appears.
  • New available property on WaspInABoxSensor: False whenever a source state is missing/unknown/unavailable. Users see unavailable (semantically correct) instead of a stale value.
  • async_added_to_hass replays state unconditionally so the unavailable state is reflected at startup even when a source is absent.

Tests

Three new regression tests in tests/test_init.py, all passing:

  1. test_source_removal_preserves_entry — source removal preserves the ConfigEntry.
  2. test_setup_with_missing_source_loads_unavailable — setup with a missing source loads the entry and exposes the helper as unavailable.
  3. test_source_returns_makes_helper_available — end-to-end recovery: remove → unavailable → recreate → available.

All existing tests still pass (6/6). ruff check and ruff format clean.

Commits

AlexMKX added 5 commits May 29, 2026 22:05
…removed

Reload the entry instead of removing it. The helper now stays in the
registry; its binary sensor becomes unavailable until the source
sensor returns.
…urce is missing

- async_setup_entry no longer returns False when a source entity_id
  is not in the registry; logs a warning and proceeds, letting the
  state-change listener pick up the entity when it appears.
- WaspInABoxSensor.available is False whenever a source state is
  missing/unknown/unavailable, so HA shows the helper as
  'unavailable' rather than a stale value.
- async_added_to_hass replays state unconditionally so the
  unavailable state is reflected even when one of the sources is
  absent from the registry at startup.
- _resolve() now takes a label ('Motion'/'Door') so the warning makes
  clear which source is missing; the duplicated 'wasp_in_a_box:'
  prefix is dropped (logger namespace already provides it).
- test_source_returns_makes_helper_available asserts the helper
  reports 'off' (both sources off) initially, not just !=
  'unavailable'.
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.

Wasp Helper disappears after HA restart

1 participant