From 7161a0bf2a5eeb4791e7d81d8cd968fbf1e0e72b Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Sat, 20 Nov 2021 23:46:11 +0100 Subject: [PATCH] Add guard for already migrated Hue entity (#59930) Co-authored-by: Paulus Schoutsen --- homeassistant/components/hue/migration.py | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/hue/migration.py b/homeassistant/components/hue/migration.py index fb584a19100..ca41478b97c 100644 --- a/homeassistant/components/hue/migration.py +++ b/homeassistant/components/hue/migration.py @@ -147,7 +147,18 @@ async def handle_v2_migration(hass: core.HomeAssistant, entry: ConfigEntry) -> N ent.unique_id, new_unique_id, ) - ent_reg.async_update_entity(ent.entity_id, new_unique_id=sensor.id) + try: + ent_reg.async_update_entity( + ent.entity_id, new_unique_id=sensor.id + ) + except ValueError: + # assume edge case where the entity was already migrated in a previous run + # which got aborted somehow and we do not want + # to crash the entire integration init + LOGGER.warning( + "Skip migration of %s because it already exists", + ent.entity_id, + ) break # migrate entities that are not connected to a device (groups) @@ -170,5 +181,14 @@ async def handle_v2_migration(hass: core.HomeAssistant, entry: ConfigEntry) -> N ent.unique_id, new_unique_id, ) - ent_reg.async_update_entity(ent.entity_id, new_unique_id=new_unique_id) + try: + ent_reg.async_update_entity(ent.entity_id, new_unique_id=new_unique_id) + except ValueError: + # assume edge case where the entity was already migrated in a previous run + # which got aborted somehow and we do not want + # to crash the entire integration init + LOGGER.warning( + "Skip migration of %s because it already exists", + ent.entity_id, + ) LOGGER.info("Migration of devices and entities to support API schema 2 finished")