diff --git a/homeassistant/components/plugwise/coordinator.py b/homeassistant/components/plugwise/coordinator.py index 8958ecae930..9a47bef8d9a 100644 --- a/homeassistant/components/plugwise/coordinator.py +++ b/homeassistant/components/plugwise/coordinator.py @@ -107,16 +107,22 @@ class PlugwiseDataUpdateCoordinator(DataUpdateCoordinator[PlugwiseData]): # via_device cannot be None, this will result in the deletion # of other Plugwise Gateways when present! via_device: str = "" + + # First find the Plugwise via_device for device_entry in device_list: - if device_entry.identifiers: - item = list(list(device_entry.identifiers)[0]) - if item[0] == DOMAIN: - # First find the Plugwise via_device, this is always the first device - if item[1] == data.gateway[GATEWAY_ID]: - via_device = device_entry.id - elif ( # then remove the connected orphaned device(s) + for identifier in device_entry.identifiers: + if identifier[0] != DOMAIN or identifier[1] != data.gateway[GATEWAY_ID]: + continue + via_device = device_entry.id + break + + # Then remove the connected orphaned device(s) + for device_entry in device_list: + for identifier in device_entry.identifiers: + if identifier[0] == DOMAIN: + if ( device_entry.via_device_id == via_device - and item[1] not in data.devices + and identifier[1] not in data.devices ): device_reg.async_update_device( device_entry.id, remove_config_entry_id=entry.entry_id @@ -125,5 +131,5 @@ class PlugwiseDataUpdateCoordinator(DataUpdateCoordinator[PlugwiseData]): "Removed %s device %s %s from device_registry", DOMAIN, device_entry.model, - item[1], + identifier[1], ) diff --git a/tests/components/plugwise/test_init.py b/tests/components/plugwise/test_init.py index 65c9fb6c5a5..5b276d5018d 100644 --- a/tests/components/plugwise/test_init.py +++ b/tests/components/plugwise/test_init.py @@ -3,6 +3,7 @@ from datetime import timedelta from unittest.mock import MagicMock, patch +from freezegun.api import FrozenDateTimeFactory from plugwise.exceptions import ( ConnectionFailedError, InvalidAuthentication, @@ -19,7 +20,6 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component -from homeassistant.util import dt as dt_util from tests.common import MockConfigEntry, async_fire_time_changed @@ -231,9 +231,9 @@ async def test_update_device( mock_smile_adam_2: MagicMock, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, ) -> None: """Test a clean-up of the device_registry.""" - utcnow = dt_util.utcnow() data = mock_smile_adam_2.async_update.return_value mock_config_entry.add_to_hass(hass) @@ -260,7 +260,8 @@ async def test_update_device( # Add a 2nd Tom/Floor data.devices.update(TOM) with patch(HA_PLUGWISE_SMILE_ASYNC_UPDATE, return_value=data): - async_fire_time_changed(hass, utcnow + timedelta(minutes=1)) + freezer.tick(timedelta(minutes=1)) + async_fire_time_changed(hass) await hass.async_block_till_done() assert ( @@ -287,7 +288,8 @@ async def test_update_device( # Remove the existing Tom/Floor data.devices.pop("1772a4ea304041adb83f357b751341ff") with patch(HA_PLUGWISE_SMILE_ASYNC_UPDATE, return_value=data): - async_fire_time_changed(hass, utcnow + timedelta(minutes=1)) + freezer.tick(timedelta(minutes=1)) + async_fire_time_changed(hass) await hass.async_block_till_done() assert (