Skip to content

Commit 67f275e

Browse files
authored
Merge pull request #357 from Olen/fix/remove-orphaned-plant-entity
fix: tie plant entity to config entry for proper cleanup
2 parents da7d0bd + 97db4e1 commit 67f275e

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

custom_components/plant/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
207207
component = EntityComponent(_LOGGER, DOMAIN, hass)
208208
await component.async_add_entities(plant_entities)
209209

210-
# Add the entities to device registry together with plant
210+
# Add the entities to device registry and tie to config entry
211211
device_id = plant.device_id
212-
await _plant_add_to_device_registry(hass, plant_entities, device_id)
212+
await _plant_add_to_device_registry(hass, plant_entities, device_id, entry)
213213

214214
# Set up utility sensor
215215
hass.data.setdefault(DATA_UTILITY, {})
@@ -317,9 +317,12 @@ async def replace_sensor(call: ServiceCall) -> None:
317317

318318

319319
async def _plant_add_to_device_registry(
320-
hass: HomeAssistant, plant_entities: list[Entity], device_id: str
320+
hass: HomeAssistant,
321+
plant_entities: list[Entity],
322+
device_id: str,
323+
entry: ConfigEntry,
321324
) -> None:
322-
"""Add all related entities to the correct device_id"""
325+
"""Add all related entities to the correct device and config entry."""
323326

324327
# There must be a better way to do this, but I just can't find a way to set the
325328
# device_id when adding the entities.
@@ -329,7 +332,11 @@ async def _plant_add_to_device_registry(
329332
raise ConfigEntryNotReady(
330333
f"Entity {entity.entity_id} not yet registered, retrying setup"
331334
)
332-
erreg.async_update_entity(entity.registry_entry.entity_id, device_id=device_id)
335+
erreg.async_update_entity(
336+
entity.registry_entry.entity_id,
337+
device_id=device_id,
338+
config_entry_id=entry.entry_id,
339+
)
333340

334341

335342
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

custom_components/plant/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"requirements": [
1818
"async-timeout>=4.0.2"
1919
],
20-
"version": "2026.2.1-beta2"
20+
"version": "2026.2.1-beta3"
2121
}

tests/test_init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ async def test_remove_entry(
104104
assert len(entities_before) > 0
105105
assert device_before is not None
106106

107+
# Verify the main plant entity is tied to the config entry
108+
plant_entity_id = entity_registry.async_get_entity_id(DOMAIN, DOMAIN, entry_id)
109+
assert plant_entity_id is not None
110+
plant_entry = entity_registry.async_get(plant_entity_id)
111+
assert plant_entry.config_entry_id == entry_id
112+
107113
# Remove the config entry (this calls async_remove_entry)
108114
await hass.config_entries.async_remove(entry_id)
109115
await hass.async_block_till_done()
@@ -112,6 +118,9 @@ async def test_remove_entry(
112118
entities_after = er.async_entries_for_config_entry(entity_registry, entry_id)
113119
assert len(entities_after) == 0
114120

121+
# Verify the main plant entity is also removed
122+
assert entity_registry.async_get(plant_entity_id) is None
123+
115124
# Verify device is removed from registry
116125
device_after = device_registry.async_get_device(
117126
identifiers={(DOMAIN, entry_id)}

0 commit comments

Comments
 (0)