Remove old update of group unique id in deCONZ (#112533)

This commit is contained in:
Robert Svensson 2024-03-15 20:09:44 +01:00 committed by GitHub
parent 9c2c7f1a45
commit 3ba29c361a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 129 deletions

View File

@ -2,21 +2,13 @@
from __future__ import annotations
from typing import cast
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
import homeassistant.helpers.entity_registry as er
from .config_flow import get_master_gateway
from .const import CONF_GROUP_ID_BASE, CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
from .deconz_event import async_setup_events, async_unload_events
from .errors import AuthenticationRequired, CannotConnect
from .hub import DeconzHub, get_deconz_api
@ -31,8 +23,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
"""
hass.data.setdefault(DOMAIN, {})
await async_update_group_unique_id(hass, config_entry)
if not config_entry.options:
await async_update_master_gateway(hass, config_entry)
@ -98,33 +88,3 @@ async def async_update_master_gateway(
options = {**config_entry.options, CONF_MASTER_GATEWAY: master}
hass.config_entries.async_update_entry(config_entry, options=options)
async def async_update_group_unique_id(
hass: HomeAssistant, config_entry: ConfigEntry
) -> None:
"""Update unique ID entities based on deCONZ groups."""
if not (group_id_base := config_entry.data.get(CONF_GROUP_ID_BASE)):
return
old_unique_id = cast(str, group_id_base)
new_unique_id = cast(str, config_entry.unique_id)
@callback
def update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] | None:
"""Update unique ID of entity entry."""
if f"{old_unique_id}-" not in entity_entry.unique_id:
return None
return {
"new_unique_id": entity_entry.unique_id.replace(
old_unique_id, new_unique_id
)
}
await er.async_migrate_entries(hass, config_entry.entry_id, update_unique_id)
data = {
CONF_API_KEY: config_entry.data[CONF_API_KEY],
CONF_HOST: config_entry.data[CONF_HOST],
CONF_PORT: config_entry.data[CONF_PORT],
}
hass.config_entries.async_update_entry(config_entry, data=data)

View File

@ -7,21 +7,13 @@ from homeassistant.components.deconz import (
DeconzHub,
async_setup_entry,
async_unload_entry,
async_update_group_unique_id,
)
from homeassistant.components.deconz.const import (
CONF_GROUP_ID_BASE,
DOMAIN as DECONZ_DOMAIN,
)
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker
ENTRY1_HOST = "1.2.3.4"
@ -158,80 +150,3 @@ async def test_unload_entry_multiple_gateways_parallel(
)
assert len(hass.data[DECONZ_DOMAIN]) == 0
async def test_update_group_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test successful migration of entry data."""
old_unique_id = "123"
new_unique_id = "1234"
entry = MockConfigEntry(
domain=DECONZ_DOMAIN,
unique_id=new_unique_id,
data={
CONF_API_KEY: "1",
CONF_HOST: "2",
CONF_GROUP_ID_BASE: old_unique_id,
CONF_PORT: "3",
},
)
entry.add_to_hass(hass)
# Create entity entry to migrate to new unique ID
entity_registry.async_get_or_create(
LIGHT_DOMAIN,
DECONZ_DOMAIN,
f"{old_unique_id}-OLD",
suggested_object_id="old",
config_entry=entry,
)
# Create entity entry with new unique ID
entity_registry.async_get_or_create(
LIGHT_DOMAIN,
DECONZ_DOMAIN,
f"{new_unique_id}-NEW",
suggested_object_id="new",
config_entry=entry,
)
await async_update_group_unique_id(hass, entry)
assert entry.data == {CONF_API_KEY: "1", CONF_HOST: "2", CONF_PORT: "3"}
assert (
entity_registry.async_get(f"{LIGHT_DOMAIN}.old").unique_id
== f"{new_unique_id}-OLD"
)
assert (
entity_registry.async_get(f"{LIGHT_DOMAIN}.new").unique_id
== f"{new_unique_id}-NEW"
)
async def test_update_group_unique_id_no_legacy_group_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test migration doesn't trigger without old legacy group id in entry data."""
old_unique_id = "123"
new_unique_id = "1234"
entry = MockConfigEntry(
domain=DECONZ_DOMAIN,
unique_id=new_unique_id,
data={},
)
# Create entity entry to migrate to new unique ID
entity_registry.async_get_or_create(
LIGHT_DOMAIN,
DECONZ_DOMAIN,
f"{old_unique_id}-OLD",
suggested_object_id="old",
config_entry=entry,
)
await async_update_group_unique_id(hass, entry)
assert (
entity_registry.async_get(f"{LIGHT_DOMAIN}.old").unique_id
== f"{old_unique_id}-OLD"
)