mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Fix device cleanup in plugwise (#126212)
This commit is contained in:
parent
ec2db38516
commit
a10d68e63e
@ -107,16 +107,22 @@ class PlugwiseDataUpdateCoordinator(DataUpdateCoordinator[PlugwiseData]):
|
|||||||
# via_device cannot be None, this will result in the deletion
|
# via_device cannot be None, this will result in the deletion
|
||||||
# of other Plugwise Gateways when present!
|
# of other Plugwise Gateways when present!
|
||||||
via_device: str = ""
|
via_device: str = ""
|
||||||
|
|
||||||
|
# First find the Plugwise via_device
|
||||||
for device_entry in device_list:
|
for device_entry in device_list:
|
||||||
if device_entry.identifiers:
|
for identifier in device_entry.identifiers:
|
||||||
item = list(list(device_entry.identifiers)[0])
|
if identifier[0] != DOMAIN or identifier[1] != data.gateway[GATEWAY_ID]:
|
||||||
if item[0] == DOMAIN:
|
continue
|
||||||
# First find the Plugwise via_device, this is always the first device
|
via_device = device_entry.id
|
||||||
if item[1] == data.gateway[GATEWAY_ID]:
|
break
|
||||||
via_device = device_entry.id
|
|
||||||
elif ( # then remove the connected orphaned device(s)
|
# 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
|
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_reg.async_update_device(
|
||||||
device_entry.id, remove_config_entry_id=entry.entry_id
|
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",
|
"Removed %s device %s %s from device_registry",
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
device_entry.model,
|
device_entry.model,
|
||||||
item[1],
|
identifier[1],
|
||||||
)
|
)
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
from plugwise.exceptions import (
|
from plugwise.exceptions import (
|
||||||
ConnectionFailedError,
|
ConnectionFailedError,
|
||||||
InvalidAuthentication,
|
InvalidAuthentication,
|
||||||
@ -19,7 +20,6 @@ from homeassistant.const import Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.util import dt as dt_util
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||||
|
|
||||||
@ -231,9 +231,9 @@ async def test_update_device(
|
|||||||
mock_smile_adam_2: MagicMock,
|
mock_smile_adam_2: MagicMock,
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test a clean-up of the device_registry."""
|
"""Test a clean-up of the device_registry."""
|
||||||
utcnow = dt_util.utcnow()
|
|
||||||
data = mock_smile_adam_2.async_update.return_value
|
data = mock_smile_adam_2.async_update.return_value
|
||||||
|
|
||||||
mock_config_entry.add_to_hass(hass)
|
mock_config_entry.add_to_hass(hass)
|
||||||
@ -260,7 +260,8 @@ async def test_update_device(
|
|||||||
# Add a 2nd Tom/Floor
|
# Add a 2nd Tom/Floor
|
||||||
data.devices.update(TOM)
|
data.devices.update(TOM)
|
||||||
with patch(HA_PLUGWISE_SMILE_ASYNC_UPDATE, return_value=data):
|
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()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
@ -287,7 +288,8 @@ async def test_update_device(
|
|||||||
# Remove the existing Tom/Floor
|
# Remove the existing Tom/Floor
|
||||||
data.devices.pop("1772a4ea304041adb83f357b751341ff")
|
data.devices.pop("1772a4ea304041adb83f357b751341ff")
|
||||||
with patch(HA_PLUGWISE_SMILE_ASYNC_UPDATE, return_value=data):
|
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()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user