🗑️ Remove stale devices when they disappear from Comwatt #17
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/22-remove-stale"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Closes MateoGreil/homeassistant-comwatt#22.
When a device is deleted on the Comwatt side, its entities and the device itself currently stay in HA's registries forever. This PR adds a prune pass that runs after
async_config_entry_first_refreshsucceeds: walk the entity registry and device registry for this config entry and drop anything whose unique_id / identifier is no longer present in the fresh topology snapshot.The prune only runs at setup (HA startup and integration reload), so a transient API hiccup during normal polling can never accidentally remove a real device — the first-refresh has to succeed, which means the topology we compare against is reliable.
Device removal uses
async_update_device(remove_config_entry_id=...)so HA removes the device only if no other integration still references it.Test plan
test_setup_prunes_stale_entities_and_devicesintest_init.py: seeds a stale device + entity, runs setup with an API response that doesn't mention them, asserts they've been removed.pytest tests/— 25 passed.2015af572d1fd4f52e68