From ca20fc857fbe98b2cedb5b38100052b8a64f58a2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 23 Nov 2021 09:36:03 +0100 Subject: [PATCH] Remove via_device links when a device is removed (#60153) * Remove via_device links when a device is removed * Update test --- homeassistant/helpers/device_registry.py | 3 +++ tests/helpers/test_device_registry.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 5395218b48e..65df557b582 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -501,6 +501,9 @@ class DeviceRegistry: orphaned_timestamp=None, ) ) + for other_device in list(self.devices.values()): + if other_device.via_device_id == device_id: + self._async_update_device(other_device.id, via_device_id=None) self.hass.bus.async_fire( EVENT_DEVICE_REGISTRY_UPDATED, {"action": "remove", "device_id": device_id} ) diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index 8c311d44588..939387f411a 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -420,7 +420,7 @@ async def test_deleted_device_removing_area_id(registry): async def test_specifying_via_device_create(registry): - """Test specifying a via_device and updating.""" + """Test specifying a via_device and removal of the hub device.""" via = registry.async_get_or_create( config_entry_id="123", connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, @@ -440,6 +440,10 @@ async def test_specifying_via_device_create(registry): assert light.via_device_id == via.id + registry.async_remove_device(via.id) + light = registry.async_get_device({("hue", "456")}) + assert light.via_device_id is None + async def test_specifying_via_device_update(registry): """Test specifying a via_device and updating."""