Browse Source

fix(migration): migrate device entries when changing unique ids

To avoid creating brand new devices and leaving the old ones zombied with
any user customization lost, we need to migrate the device entries to keep
them in sync with the entity entries when changing the base device id.

Issue #5849
Jason Rumney 10 hours ago
parent
commit
7ccb9cf663
1 changed files with 10 additions and 3 deletions
  1. 10 3
      custom_components/tuya_local/__init__.py

+ 10 - 3
custom_components/tuya_local/__init__.py

@@ -13,9 +13,8 @@ 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_get as async_get_entity_registry,
-)
+from homeassistant.helpers.device_registry import async_get as async_get_device_registry
+from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry
 from homeassistant.helpers.entity_registry import async_migrate_entries
 from homeassistant.util import slugify
 
@@ -1011,6 +1010,14 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
             await async_migrate_entries(
                 hass, entry.entry_id, update_gateway_scoped_unique_id
             )
+            # ensure the device entry itself is updated to the new ID
+            dr = async_get_device_registry(hass)
+            device_entry = dr.async_get_device(identifiers={(DOMAIN, old_device_id)})
+            if device_entry:
+                dr.async_update_device(
+                    device_entry.id,
+                    new_identifiers={(DOMAIN, new_device_id)},
+                )
             hass.config_entries.async_update_entry(entry, unique_id=new_device_id)
         hass.config_entries.async_update_entry(entry, minor_version=22)
     return True