From 39cc70957bde33dd1aab560c5bbb525678c7df3c Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:10:36 +0000 Subject: [PATCH] Fix re-adding previously removed child --- .../components/tplink/coordinator.py | 2 ++ homeassistant/components/tplink/entity.py | 4 ++++ tests/components/tplink/test_init.py | 21 ++++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/tplink/coordinator.py b/homeassistant/components/tplink/coordinator.py index f845d006813de4..331380aa9fc0c9 100644 --- a/homeassistant/components/tplink/coordinator.py +++ b/homeassistant/components/tplink/coordinator.py @@ -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: @@ -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, diff --git a/homeassistant/components/tplink/entity.py b/homeassistant/components/tplink/entity.py index 25d306b942c15c..b47d01ba2ff1d5 100644 --- a/homeassistant/components/tplink/entity.py +++ b/homeassistant/components/tplink/entity.py @@ -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 diff --git a/tests/components/tplink/test_init.py b/tests/components/tplink/test_init.py index 380c73d726e5bc..c2e4dfd6fe4a5d 100644 --- a/tests/components/tplink/test_init.py +++ b/tests/components/tplink/test_init.py @@ -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) @@ -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)})