Просмотр исходного кода

fix (migration): remove rather than update entity entries with invalid id

Since entity entries will be automatically created when the entities
are created, the invalid entity entries have already gone dormant and
been replaced by new entries with the correct id. The earlier attempt
to fix the unique id was therefore unsuccessful, as it duplicated the
already recreated entry.

Instead, remove the invalid entries.

Bump the minor version so this gets run regardless of whether the
migration to 3.11 already ran successfully or not.

Issue #4130
Jason Rumney 2 месяцев назад
Родитель
Сommit
3a023e6f4d
2 измененных файлов с 19 добавлено и 11 удалено
  1. 18 10
      custom_components/tuya_local/__init__.py
  2. 1 1
      custom_components/tuya_local/config_flow.py

+ 18 - 10
custom_components/tuya_local/__init__.py

@@ -13,7 +13,10 @@ from homeassistant.config_entries import ConfigEntry
 from homeassistant.const import CONF_HOST
 from homeassistant.core import HomeAssistant, callback
 from homeassistant.exceptions import ConfigEntryNotReady
-from homeassistant.helpers.entity_registry import async_migrate_entries
+from homeassistant.helpers.entity_registry import (
+    async_migrate_entries,
+    async_get as async_get_entity_registry,
+)
 from homeassistant.util import slugify
 
 from .const import (
@@ -677,7 +680,7 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         await async_migrate_entries(hass, entry.entry_id, update_unique_id13_10)
         hass.config_entries.async_update_entry(entry, minor_version=10)
 
-    if entry.version == 13 and entry.minor_version < 11:
+    if entry.version == 13 and entry.minor_version < 12:
         # at some point HA stopped populating unique_id for device entries,
         # and our migrations have been using the wrong value. Migration
         # to correct that
@@ -690,17 +693,22 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
             )
 
         @callback
-        def update_unique_id3_11(entity_entry):
+        def update_unique_id3_12(entity_entry):
             """Update the unique id of badly migrated entities."""
             old_id = entity_entry.unique_id
             if old_id.startswith("None-"):
-                new_id = f"{device_id}-{old_id[5:]}"
-                return {
-                    "new_unique_id": new_id,
-                }
-
-        await async_migrate_entries(hass, entry.entry_id, update_unique_id3_11)
-        hass.config_entries.async_update_entry(entry, minor_version=11)
+                # new_id = f"{device_id}-{old_id[5:]}"
+                # return {
+                #     "new_unique_id": new_id,
+                # }
+                # Rather than update, delete the bogus entities, as HA will
+                # recreate them correctly, and maybe already has so duplicates
+                # could result if they are migrated.
+                registry = async_get_entity_registry(hass)
+                registry.async_remove(entity_entry.entity_id)
+
+        await async_migrate_entries(hass, entry.entry_id, update_unique_id3_12)
+        hass.config_entries.async_update_entry(entry, minor_version=12)
 
     return True
 

+ 1 - 1
custom_components/tuya_local/config_flow.py

@@ -47,7 +47,7 @@ _LOGGER = logging.getLogger(__name__)
 
 class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
     VERSION = 13
-    MINOR_VERSION = 11
+    MINOR_VERSION = 12
     CONNECTION_CLASS = CONN_CLASS_LOCAL_PUSH
     device = None
     data = {}