Browse Source

Migration: migrate renamed entities where possible.

Migrated different variants of the following to standardize translations:
  - light_display
  - light_embers
  - light_indicator
  - light_logs
  - light_nightlight
  - number_timeout_period
  - sensor_time_remaining
  - switch_anti_frost
  - switch_ionizer
  - switch_uv_sterilization

Changes that will require manual removal of old entities:
  - light_uv_sterilization, replaced with a switch
  - switch_indicator, replaced with a light
  - switch_backlight, replaced with a light
  - switch_display, replaced with a light
  - light_led, replaced with light_indicator or default light depending on usage
  - Loycco sound nightlight: light given a tranlation_key of nightlight
  - deprecated old humidity sensors (replaced with humidifier current_humidity)

Issue #1708
Jason Rumney 1 year ago
parent
commit
a8d0e46079

+ 73 - 0
custom_components/tuya_local/__init__.py

@@ -374,6 +374,79 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         await async_migrate_entries(hass, entry.entry_id, update_unique_id13_2)
         entry.minor_version = 2
 
+    if entry.version == 13 and entry.minor_version < 3:
+        # Migrate unique ids of existing entities to new id taking into
+        # account translation_key, and standardising naming
+        device_id = entry.unique_id
+        conf_file = get_config(entry.data[CONF_TYPE])
+        if conf_file is None:
+            _LOGGER.error(
+                NOT_FOUND,
+                entry.data[CONF_TYPE],
+            )
+            return False
+
+        @callback
+        def update_unique_id13_3(entity_entry):
+            """Update the unique id of an entity entry."""
+            old_id = entity_entry.unique_id
+            platform = entity_entry.entity_id.split(".", 1)[0]
+            # Standardistion of entity naming to use translation_key
+            replacements = {
+                "light_front_display": "light_display",
+                "light_lcd_brightness": "light_display",
+                "light_coal_bed": "light_logs",
+                "light_ember": "light_embers",
+                "light_led_indicator": "light_indicator",
+                "light_status_indicator": "light_indicator",
+                "light_indicator_light": "light_indicator",
+                "light_indicators": "light_indicator",
+                "light_night_light": "light_nightlight",
+                "number_tiemout_period": "number_timeout_period",
+                "sensor_remaining_time": "sensor_time_remaining",
+                "sensor_timer_remain": "sensor_time_remaining",
+                "sensor_timer": "sensor_time_remaining",
+                "sensor_timer_countdown": "sensor_time_remaining",
+                "sensor_timer_remaining": "sensor_time_remaining",
+                "sensor_time_left": "sensor_time_remaining",
+                "sensor_timer_minutes_left": "sensor_time_remaining",
+                "sensor_timer_time_left": "sensor_time_remaining",
+                "sensor_auto_shutoff_time_remaining": "sensor_time_remaining",
+                "sensor_warm_time_remaining": "sensor_time_remaining",
+                "sensor_run_time_remaining": "sensor_time_remaining",
+                "switch_ioniser": "switch_ionizer",
+                "switch_run_uv_cycle": "switch_uv_sterilization",
+                "switch_uv_light": "switch_uv_sterilization",
+                "switch_ihealth": "switch_uv_sterilization",
+                "switch_uv_lamp": "switch_uv_sterilization",
+                "switch_anti_freeze": "switch_anti_frost",
+            }
+            for suffix, new_suffix in replacements.items():
+                if old_id.endswith(suffix):
+                    e = conf_file.primary_entity
+                    if e.entity != platform or e.name:
+                        for e in conf_file.secondary_entities():
+                            if e.entity == platform and not e.name:
+                                break
+                    if e.entity == platform and not e.name:
+                        new_id = e.unique_id(device_id)
+                        if new_suffix and new_id.endswith(new_suffix):
+                            _LOGGER.info(
+                                "Migrating %s unique_id %s to %s",
+                                e.entity,
+                                old_id,
+                                new_id,
+                            )
+                            return {
+                                "new_unique_id": entity_entry.unique_id.replace(
+                                    old_id,
+                                    new_id,
+                                )
+                            }
+
+        await async_migrate_entries(hass, entry.entry_id, update_unique_id13_3)
+        entry.minor_version = 3
+
     return True
 
 

+ 1 - 1
custom_components/tuya_local/config_flow.py

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