Skip to content

Commit

Permalink
Use migration helper in RainMachine (#62328)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya committed Dec 19, 2021
1 parent b63c766 commit 1b17c29
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions homeassistant/components/rainmachine/__init__.py
Expand Up @@ -344,10 +344,9 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if version == 1:
version = entry.version = 2

ent_reg = er.async_get(hass)
for entity_entry in [
e for e in ent_reg.entities.values() if e.config_entry_id == entry.entry_id
]:
@callback
def migrate_unique_id(entity_entry: er.RegistryEntry) -> dict[str, Any]:
"""Migrate the unique ID to a new format."""
unique_id_pieces = entity_entry.unique_id.split("_")
old_mac = unique_id_pieces[0]
new_mac = ":".join(old_mac[i : i + 2] for i in range(0, len(old_mac), 2))
Expand All @@ -356,9 +355,9 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if entity_entry.entity_id.startswith("switch"):
unique_id_pieces[1] = unique_id_pieces[1][11:].lower()

ent_reg.async_update_entity(
entity_entry.entity_id, new_unique_id="_".join(unique_id_pieces)
)
return {"new_unique_id": "_".join(unique_id_pieces)}

await er.async_migrate_entries(hass, entry.entry_id, migrate_unique_id)

LOGGER.info("Migration to version %s successful", version)

Expand Down

0 comments on commit 1b17c29

Please sign in to comment.