Remove stale tradfri devices (#65218)

This commit is contained in:
Martin Hjelmare 2022-02-01 01:43:16 +01:00 committed by GitHub
parent 0be8060b69
commit d0412d65ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 1 deletions

View File

@ -15,9 +15,10 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.device_registry as dr
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
@ -164,6 +165,8 @@ async def async_setup_entry(
sw_version=gateway_info.firmware_version,
)
remove_stale_devices(hass, entry, devices)
# Setup the device coordinators
coordinator_data = {
CONF_GATEWAY_ID: gateway,
@ -231,3 +234,45 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
listener()
return unload_ok
@callback
def remove_stale_devices(
hass: HomeAssistant, config_entry: ConfigEntry, devices: list[Device]
) -> None:
"""Remove stale devices from device registry."""
device_registry = dr.async_get(hass)
device_entries = dr.async_entries_for_config_entry(
device_registry, config_entry.entry_id
)
all_device_ids = {device.id for device in devices}
for device_entry in device_entries:
device_id: str | None = None
gateway_id: str | None = None
for identifier in device_entry.identifiers:
if identifier[0] != DOMAIN:
continue
_id = identifier[1]
# Identify gateway device.
if _id == config_entry.data[CONF_GATEWAY_ID]:
gateway_id = _id
break
device_id = _id
break
if gateway_id is not None:
# Do not remove gateway device entry.
continue
if device_id is None or device_id not in all_device_ids:
# If device_id is None an invalid device entry was found for this config entry.
# If the device_id is not in existing device ids it's a stale device entry.
# Remove config entry from this device entry in either case.
device_registry.async_update_device(
device_entry.id, remove_config_entry_id=config_entry.entry_id
)

View File

@ -49,3 +49,44 @@ async def test_entry_setup_unload(hass, mock_api_factory):
await hass.async_block_till_done()
assert unload.call_count == len(tradfri.PLATFORMS)
assert mock_api_factory.shutdown.call_count == 1
async def test_remove_stale_devices(hass, mock_api_factory):
"""Test remove stale device registry entries."""
entry = MockConfigEntry(
domain=tradfri.DOMAIN,
data={
tradfri.CONF_HOST: "mock-host",
tradfri.CONF_IDENTITY: "mock-identity",
tradfri.CONF_KEY: "mock-key",
tradfri.CONF_IMPORT_GROUPS: True,
tradfri.CONF_GATEWAY_ID: GATEWAY_ID,
},
)
entry.add_to_hass(hass)
dev_reg = dr.async_get(hass)
dev_reg.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(tradfri.DOMAIN, "stale_device_id")},
)
dev_entries = dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
assert len(dev_entries) == 1
dev_entry = dev_entries[0]
assert dev_entry.identifiers == {(tradfri.DOMAIN, "stale_device_id")}
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
dev_entries = dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
# Check that only the gateway device entry remains.
assert len(dev_entries) == 1
dev_entry = dev_entries[0]
assert dev_entry.identifiers == {
(tradfri.DOMAIN, entry.data[tradfri.CONF_GATEWAY_ID])
}
assert dev_entry.manufacturer == tradfri.ATTR_TRADFRI_MANUFACTURER
assert dev_entry.name == tradfri.ATTR_TRADFRI_GATEWAY
assert dev_entry.model == tradfri.ATTR_TRADFRI_GATEWAY_MODEL