mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 18:27:51 +00:00
Remove stale tradfri devices (#65218)
This commit is contained in:
parent
0be8060b69
commit
d0412d65ac
@ -15,9 +15,10 @@ import voluptuous as vol
|
|||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STOP
|
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
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
import homeassistant.helpers.device_registry as dr
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
@ -164,6 +165,8 @@ async def async_setup_entry(
|
|||||||
sw_version=gateway_info.firmware_version,
|
sw_version=gateway_info.firmware_version,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
remove_stale_devices(hass, entry, devices)
|
||||||
|
|
||||||
# Setup the device coordinators
|
# Setup the device coordinators
|
||||||
coordinator_data = {
|
coordinator_data = {
|
||||||
CONF_GATEWAY_ID: gateway,
|
CONF_GATEWAY_ID: gateway,
|
||||||
@ -231,3 +234,45 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
listener()
|
listener()
|
||||||
|
|
||||||
return unload_ok
|
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
|
||||||
|
)
|
||||||
|
@ -49,3 +49,44 @@ async def test_entry_setup_unload(hass, mock_api_factory):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert unload.call_count == len(tradfri.PLATFORMS)
|
assert unload.call_count == len(tradfri.PLATFORMS)
|
||||||
assert mock_api_factory.shutdown.call_count == 1
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user