Skip to content

Commit

Permalink
Migrate changed unique_ids where it is possible.
Browse files Browse the repository at this point in the history
Some changes in this version are still unhandled:

ETOP FCU: child lock changed from switch to lock, to match other devices
Holman WX1/2: "Time left" changed from number to sensor
Proscenic M9: Error message changed from sensor to a problem binary_sensor
Rotenso Ronix and Stadler Form Roger: unnamed sensor given PM2.5 class
   (corrects a mistake from previous migration)
Shinco 30D: removes the fault code sensor (moved to an attribute on
   the problem binary_sensor, like other devices)
Vacplus: Fault sensor changed to binary_sensors for general problem
   and tank full
Garza irrigation: alarm binary_sensor changed to problem
   binary_sensor, but cannot be migrated as other legitimate
   binary_sensors for actual alarms are still labelled "alarm"

Issue #1579
  • Loading branch information
make-all committed Mar 3, 2024
1 parent f5cdf53 commit 7057429
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions custom_components/tuya_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 359 in custom_components/tuya_local/__init__.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F821)

custom_components/tuya_local/__init__.py:359:68: F821 Undefined name `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


Expand Down
1 change: 1 addition & 0 deletions custom_components/tuya_local/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down

0 comments on commit 7057429

Please sign in to comment.