Skip to content

Commit

Permalink
Fix re-adding previously removed child
Browse files Browse the repository at this point in the history
  • Loading branch information
sdb9696 committed Jan 15, 2025
1 parent 4ed2305 commit 39cc709
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions homeassistant/components/tplink/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
),
)
self._previous_child_device_ids = {child.device_id for child in device.children}
self.removed_child_device_ids: set[str] = set()
self._child_coordinators: dict[str, TPLinkDataUpdateCoordinator] = {}

async def _async_update_data(self) -> None:
Expand Down Expand Up @@ -107,6 +108,7 @@ async def _async_update_data(self) -> None:
await child_coordinator.async_shutdown()

self._previous_child_device_ids = current_child_device_ids
self.removed_child_device_ids = stale_device_ids

def get_child_coordinator(
self,
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/tplink/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ def entities_for_device_and_its_children[
)
)

# Remove any device ids removed via the coordinator so they can be re-added
for removed_child_id in coordinator.removed_child_device_ids:
known_child_device_ids.discard(removed_child_id)

current_child_devices = {child.device_id: child for child in device.children}
current_child_device_ids = set(current_child_devices.keys())
new_child_device_ids = current_child_device_ids - known_child_device_ids
Expand Down
21 changes: 20 additions & 1 deletion tests/components/tplink/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ async def test_automatic_device_addition_and_removal(

assert device_registry.async_get_device(identifiers={(DOMAIN, "child2")}) is None

# Add a child devices
# Add child devices
mock_device.children = [children["child1"], children["child3"], children["child4"]]
freezer.tick(5)
async_fire_time_changed(hass)
Expand All @@ -930,3 +930,22 @@ async def test_automatic_device_addition_and_removal(

for device_id in ("child1", "child3", "child4"):
assert device_registry.async_get_device(identifiers={(DOMAIN, device_id)})

# Add the previously removed child device
mock_device.children = [
children["child1"],
children["child2"],
children["child3"],
children["child4"],
]
freezer.tick(5)
async_fire_time_changed(hass)

for child_id in (1, 2, 3, 4):
entity_id = f"{platform}.child_{child_id}_{translated_name}"
state = hass.states.get(entity_id)
assert state
assert entity_registry.async_get(entity_id)

for device_id in ("child1", "child2", "child3", "child4"):
assert device_registry.async_get_device(identifiers={(DOMAIN, device_id)})

0 comments on commit 39cc709

Please sign in to comment.