From 37aff07db259f79209c0e5ff9835f81d2c9a0a09 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 27 Sep 2026 16:10:00 +0200 Subject: [PATCH] Unsubscribe person from device trackers when it is removed (#183319) Co-authored-by: Claude --- homeassistant/components/person/__init__.py | 7 +++++++ tests/components/person/test_init.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 85908832d68e..35313d2df5f8 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -502,6 +502,13 @@ class Person( # as there are attributes that can already be set self._update_extra_state_attributes() + @override + async def async_will_remove_from_hass(self) -> None: + """Unsubscribe from device trackers when the person is removed.""" + if self._unsub_track_device is not None: + self._unsub_track_device() + self._unsub_track_device = None + @override async def async_update_config(self, config: ConfigType) -> None: """Handle when the config is updated.""" diff --git a/tests/components/person/test_init.py b/tests/components/person/test_init.py index e6ce4c221507..12328305fa46 100644 --- a/tests/components/person/test_init.py +++ b/tests/components/person/test_init.py @@ -1258,6 +1258,7 @@ async def test_ws_delete( ) -> None: """Test deleting via WS.""" manager = hass.data[DOMAIN][1] + entity = hass.data[DOMAIN][2].get_entity("person.tracked_person") client = await hass_ws_client(hass) persons = manager.async_items() @@ -1274,6 +1275,12 @@ async def test_ws_delete( assert len(hass.states.async_entity_ids(DOMAIN)) == 0 assert not entity_registry.async_is_registered("person.tracked_person") + # The removed person no longer follows its device tracker + with patch.object(entity, "_update_state") as mock_update_state: + hass.states.async_set(DEVICE_TRACKER, "home") + await hass.async_block_till_done() + mock_update_state.assert_not_called() + async def test_ws_delete_require_admin( hass: HomeAssistant,