diff --git a/custom_components/tuya_local/__init__.py b/custom_components/tuya_local/__init__.py index b5ab97a689..92e16abff5 100644 --- a/custom_components/tuya_local/__init__.py +++ b/custom_components/tuya_local/__init__.py @@ -304,6 +304,76 @@ def update_unique_id13(entity_entry): await async_migrate_entries(hass, entry.entry_id, update_unique_id13) entry.version = 13 + if entry.version == 13 and entry.minor_version < 2: + # 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_2(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 = { + # special meaning of None to handle _full and _empty variants + "binary_sensor_tank": None, + "binary_sensor_tank_full_or_missing": "binary_sensor_tank_full", + "binary_sensor_water_tank_full": "binary_sensor_tank_full", + "binary_sensor_low_water": "binary_sensor_tank_empty", + "binary_sensor_water_tank_empty": "binary_sensor_tank_empty", + "binary_sensor_fault": "binary_sensor_problem", + "binary_sensor_error": "binary_sensor_problem", + "binary_sensor_fault_alarm": "binary_sensor_problem", + "binary_sensor_errors": "binary_sensor_problem", + "binary_sensor_defrosting": "binary_sensor_defrost", + "binary_sensor_anti_frost": "binary_sensor_defrost", + "binary_sensor_anti_freeze": "binary_sensor_defrost", + "binary_sensor_low_battery": "binary_sensor_battery", + "binary_sensor_low_battery_alarm": "binary_sensor_battery", + "select_temperature_units": "select_temperature_unit", + "select_display_temperature_unit": "select_temperature_unit", + "select_display_unit": "select_temperature_unit", + "select_display_units": "select_temperature_unit", + "select_temperature_display_units": "select_temperature_unit", + "switch_defrost": "switch_anti_frost", + "switch_frost_protection": "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)) or ( + new_suffix is None and new_id.contains(old_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_2) + entry.minor_version = 2 + return True diff --git a/custom_components/tuya_local/config_flow.py b/custom_components/tuya_local/config_flow.py index eb9a331a1d..498ab116fd 100644 --- a/custom_components/tuya_local/config_flow.py +++ b/custom_components/tuya_local/config_flow.py @@ -26,6 +26,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 13 + MINOR_VERSION = 2 CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH device = None data = {}