🐛 Fix KeyError in async_unload_entry #12

Merged
mat merged 1 commit from fix/c5-unload-keyerror into main 2026-04-24 14:20:24 +00:00
Owner

Summary

  • async_setup_entry only wrote a single shared hass.data[DOMAIN]["cookies"] key, but async_unload_entry popped hass.data[DOMAIN][entry.entry_id]. Unloading any config entry therefore raised KeyError; the integration could not be cleanly removed without restarting HA, and two config entries for different accounts would trample each other's cookies.
  • Store per-entry state under hass.data[DOMAIN][entry.entry_id] = {"cookies": ...} and use pop(entry.entry_id, None) in unload. Entities capture entry.entry_id in their constructors and read the cookie jar via the per-entry path.

Side-effects

  • The strict-xfail marker on test_unload_entry_cleans_up is dropped; the test now passes normally and asserts both the LOADED→NOT_LOADED transition and that the entry's data is removed from hass.data[DOMAIN].
  • test_setup_entry_authenticates_and_loads is updated to check the new, per-entry data shape.

Test plan

  • pytest tests/ — 20 passed, 1 xfailed (the remaining xfail is C1, tracked in PR #11).
  • ruff check . — green.

Note

This is a surgical fix; the coordinator refactor (C2+C4+C6+H7+M6) will later replace this hass.data[DOMAIN][entry_id] pattern with entry.runtime_data but that requires restructuring how entities access shared state.

## Summary - `async_setup_entry` only wrote a single shared `hass.data[DOMAIN]["cookies"]` key, but `async_unload_entry` popped `hass.data[DOMAIN][entry.entry_id]`. Unloading any config entry therefore raised `KeyError`; the integration could not be cleanly removed without restarting HA, and two config entries for different accounts would trample each other's cookies. - Store per-entry state under `hass.data[DOMAIN][entry.entry_id] = {"cookies": ...}` and use `pop(entry.entry_id, None)` in unload. Entities capture `entry.entry_id` in their constructors and read the cookie jar via the per-entry path. ## Side-effects - The strict-xfail marker on `test_unload_entry_cleans_up` is dropped; the test now passes normally and asserts both the LOADED→NOT_LOADED transition and that the entry's data is removed from `hass.data[DOMAIN]`. - `test_setup_entry_authenticates_and_loads` is updated to check the new, per-entry data shape. ## Test plan - [x] `pytest tests/` — 20 passed, 1 xfailed (the remaining xfail is C1, tracked in PR #11). - [x] `ruff check .` — green. ## Note This is a surgical fix; the coordinator refactor (C2+C4+C6+H7+M6) will later replace this `hass.data[DOMAIN][entry_id]` pattern with `entry.runtime_data` but that requires restructuring how entities access shared state.
🐛 Fix KeyError in async_unload_entry
All checks were successful
Validate / validate-hacs (push) Has been skipped
Validate / validate-hassfest (push) Has been skipped
Validate / lint-ruff (push) Successful in 7s
Validate / test-pytest (push) Successful in 1m45s
Validate / type-check-mypy (push) Successful in 1m48s
Validate / validate-hacs (pull_request) Has been skipped
Validate / validate-hassfest (pull_request) Has been skipped
Validate / lint-ruff (pull_request) Successful in 6s
Validate / test-pytest (pull_request) Successful in 1m45s
Validate / type-check-mypy (pull_request) Successful in 1m49s
a5c930cc02
`async_unload_entry` popped `entry.entry_id` from `hass.data[DOMAIN]`,
but `async_setup_entry` only ever stored a single flat `"cookies"` key.
Unloading or reloading any entry therefore raised `KeyError`; the
integration could not be cleanly removed without restarting Home
Assistant. Two config entries also trampled each other's cookies for
the same reason.

Store per-entry state under `hass.data[DOMAIN][entry.entry_id]` so
unload can `pop(entry.entry_id, None)` safely. Entities now capture
`entry.entry_id` in their constructors and read their cookie jar via
the per-entry path. Update the setup and unload tests to assert the
new data shape, and drop the strict-xfail that previously documented
this bug.
mat merged commit 6bf66cb601 into main 2026-04-24 14:20:24 +00:00
mat deleted branch fix/c5-unload-keyerror 2026-04-24 14:20:24 +00:00
mat referenced this pull request from a commit 2026-04-24 14:26:33 +00:00
mat referenced this pull request from a commit 2026-04-24 21:51:56 +00:00
mat referenced this pull request from a commit 2026-04-24 21:51:56 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mat/homeassistant-comwatt!12
No description provided.