fix: remove per-slot devices when the slot is removed - #1402
Merged
Conversation
Each slot gets its own device, but dropping a slot from config only tore down its entities. Home Assistant's registry cleanup reaps a device only when neither an entity nor a live config entry references it, and a slot device stays attached to the LCM entry -- so it survived forever, and with no async_remove_config_entry_device hook there was no Delete button either. The only escape was deleting the whole config entry. - Slot removal now removes the slot's device, after its entities. - Setup sweeps devices whose slot is no longer configured, so registries already polluted by this bug clean up on the next reload instead of needing manual deletion. - async_remove_config_entry_device exposes the Delete button, and allows it only for slot devices that are not currently configured. Deleting a live slot's device would strand its entities and be undone on reload; the entry's own device has to outlive the slots that hang off it. The device identifier format moves into build/parse helpers next to build_slot_unique_id -- the sweep has to parse identifiers back into slot numbers, so the format can no longer live inline at three call sites. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 3431bbad1215
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1402 +/- ##
==========================================
+ Coverage 96.98% 96.99% +0.01%
==========================================
Files 53 53
Lines 6495 6533 +38
Branches 470 470
==========================================
+ Hits 6299 6337 +38
Misses 196 196
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
`parse_slot_device_identifier` gated on `str.isdigit()`, which rejects the negative slot number `build_slot_device_identifier` will happily encode -- the slots YAML schema puts no lower bound on the key, so a user can configure slot -1 today. Its device then parsed to None, which made the orphan sweep skip it AND made the removal hook classify it as the entry's own device and refuse deletion: precisely the stuck, undeletable device issue #1399 is about, reached by a different route. Parse with `int()` and verify the round-trip instead. A bare `int()` would have over-corrected -- it accepts `+1` and `1_0` as aliases the builder never emits, which would map two identifiers onto one slot. Tests cover the builder/parser round-trip over negative, zero and large slots, rejection of non-slot and alias identifiers, and both the sweep and the removal hook against a negative-slot device. All five fail against the previous parser. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 35550f166bfd
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
Each slot gets its own device (
entity.py,identifiers={(DOMAIN, f"{entry_id}|{slot_num}")}), but removing a slot from config only tore down its entities —async_update_listener'sslots_to_removebranch never touched the device registry.Home Assistant does not clean these up either.
device_registry.async_cleanupreaps a device only when it is referenced by neither an entity nor a live config entry; a slot device stays attached to the Lock Code Manager entry, so it is permanently exempt. And with noasync_remove_config_entry_devicehook, Home Assistant rendered no Delete button — matching the report's "no way to delete it besides completely removing the config entry."Three changes:
async_remove_config_entry_deviceexposes the Delete button, and allows it only for slot devices that are not currently configured. Deleting a live slot's device would strand its entities and be undone on the next reload; the entry's own device (bareentry_id, no slot suffix) has to outlive the slots that hang off it, so it is refused too.The device identifier format moves into
build_slot_device_identifier/parse_slot_device_identifiernext to the existingbuild_slot_unique_id. The sweep has to parse identifiers back into slot numbers, so the format can no longer live inline at the three call sites that were each rebuilding it by hand.Type of change
Additional information
Three new tests, one per change: removing a slot retires its device while the surviving slot's and the entry's own devices are untouched; a stale slot device is pruned on reload; and the removal hook refuses a configured slot's device and the entry device but permits an unconfigured one. All three were confirmed to fail with the production changes reverted.
Full suite passes (1248 passed). The 43 errors in
tests/providers/zha/test_provider.pyare pre-existing onmainin my local environment and unrelated to this change.🤖 Generated with Claude Code