mirror of
https://github.com/home-assistant/core.git
synced 2026-08-03 20:24:55 +02:00
Fix Cookidoo migration duplicate unique IDs (#174168)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
@@ -65,9 +65,23 @@ def _migrate_identifiers(
|
||||
device_registry.async_update_device(dev.id, new_identifiers=new_identifiers)
|
||||
for ent in entity_entries:
|
||||
if ent.unique_id and ent.unique_id.startswith(f"{old_prefix}_"):
|
||||
new_entity_unique_id = f"{new_unique_id}{ent.unique_id[len(old_prefix) :]}"
|
||||
if (
|
||||
existing_entity_id := entity_registry.async_get_entity_id(
|
||||
ent.domain, ent.platform, new_entity_unique_id
|
||||
)
|
||||
) and existing_entity_id != ent.entity_id:
|
||||
existing_ent = entity_registry.async_get(existing_entity_id)
|
||||
if (
|
||||
existing_ent
|
||||
and existing_ent.config_entry_id == config_entry.entry_id
|
||||
):
|
||||
entity_registry.async_remove(ent.entity_id)
|
||||
continue
|
||||
|
||||
entity_registry.async_update_entity(
|
||||
ent.entity_id,
|
||||
new_unique_id=f"{new_unique_id}{ent.unique_id[len(old_prefix) :]}",
|
||||
new_unique_id=new_entity_unique_id,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -240,6 +240,73 @@ async def test_migration_from(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_cookidoo_client")
|
||||
async def test_migration_from_partial_duplicate_unique_ids(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test migration handles stale entities when the target unique_id exists."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=MOCK_CONFIG_ENTRY_MIGRATION,
|
||||
title="MIGRATION_TEST with duplicate target unique_id",
|
||||
version=1,
|
||||
minor_version=2,
|
||||
unique_id="old_ciam_sub_uuid",
|
||||
entry_id=OLD_ENTRY_ID,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
device = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
identifiers={(DOMAIN, "old_ciam_sub_uuid")},
|
||||
entry_type=dr.DeviceEntryType.SERVICE,
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
config_entry=config_entry,
|
||||
platform=DOMAIN,
|
||||
domain=Platform.TODO,
|
||||
unique_id="old_ciam_sub_uuid_ingredients",
|
||||
device_id=device.id,
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
config_entry=config_entry,
|
||||
platform=DOMAIN,
|
||||
domain=Platform.BUTTON,
|
||||
unique_id="old_ciam_sub_uuid_todo_clear",
|
||||
device_id=device.id,
|
||||
)
|
||||
existing_button_entity = entity_registry.async_get_or_create(
|
||||
config_entry=config_entry,
|
||||
platform=DOMAIN,
|
||||
domain=Platform.BUTTON,
|
||||
unique_id=f"{TEST_UUID}_todo_clear",
|
||||
device_id=device.id,
|
||||
)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert config_entry.unique_id == TEST_UUID
|
||||
|
||||
assert entity_registry.async_get_entity_id(
|
||||
Platform.TODO, DOMAIN, f"{TEST_UUID}_ingredients"
|
||||
)
|
||||
assert (
|
||||
entity_registry.async_get_entity_id(
|
||||
Platform.BUTTON, DOMAIN, f"{TEST_UUID}_todo_clear"
|
||||
)
|
||||
== existing_button_entity.entity_id
|
||||
)
|
||||
assert (
|
||||
entity_registry.async_get_entity_id(
|
||||
Platform.BUTTON, DOMAIN, "old_ciam_sub_uuid_todo_clear"
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
"from_version",
|
||||
|
||||
Reference in New Issue
Block a user