Unsubscribe person from device trackers when it is removed (#183319)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Franck Nijhof
2026-09-27 16:10:00 +02:00
committed by GitHub
co-authored by Claude
parent 314cb48f97
commit 37aff07db2
2 changed files with 14 additions and 0 deletions
@@ -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."""
+7
View File
@@ -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,