Fix device cleanup in plugwise (#126212)

This commit is contained in:
Erik Montnemery 2024-09-18 13:50:36 +02:00 committed by GitHub
parent ec2db38516
commit a10d68e63e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 13 deletions

View File

@ -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],
)

View File

@ -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 (