From 3ba29c361a26d4f67502f31ae24a3a88f412f7f2 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 15 Mar 2024 20:09:44 +0100 Subject: [PATCH] Remove old update of group unique id in deCONZ (#112533) --- homeassistant/components/deconz/__init__.py | 46 +---------- tests/components/deconz/test_init.py | 87 +-------------------- 2 files changed, 4 insertions(+), 129 deletions(-) diff --git a/homeassistant/components/deconz/__init__.py b/homeassistant/components/deconz/__init__.py index 776ab8e830f..ae9bd0db0fd 100644 --- a/homeassistant/components/deconz/__init__.py +++ b/homeassistant/components/deconz/__init__.py @@ -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) diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index 64b1594faea..3405a1973c0 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -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" - )