Skip to content

Commit

Permalink
feat: migrate domain to midea_lan
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 committed Jun 23, 2024
1 parent 9c77952 commit 64318ad
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
91 changes: 90 additions & 1 deletion custom_components/midea_ac_lan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from typing import Any, cast

import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.device_registry as dr
import homeassistant.helpers.entity_registry as er
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand All @@ -27,7 +29,7 @@
MAJOR_VERSION,
MINOR_VERSION,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.typing import ConfigType
from midealocal.device import DeviceType, ProtocolVersion
from midealocal.devices import device_selector
Expand Down Expand Up @@ -258,3 +260,90 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
# Forward the unloading of an entry to platforms
await hass.config_entries.async_unload_platforms(config_entry, ALL_PLATFORM)
return True


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate old entry."""
_LOGGER.debug(
"Migrating configuration from version %s.%s",
config_entry.version,
config_entry.minor_version,
)

# 1.0 -> 1.1: convert device identifiers from int to str
if config_entry.version == 1 and config_entry.minor_version == 0:
hass.config_entries.async_update_entry(config_entry, minor_version=1)

# Migrate device.
await _async_migrate_device_identifiers(hass, config_entry)

_LOGGER.debug("Migration to configuration version 1.1 successful")

# 1.1 -> 2.0: migrate to new domain name
if config_entry.version == 1 and config_entry.minor_version == 1:
# Migrate config.
new_data = {**config_entry.data}
new_data["domain"] = DOMAIN
hass.config_entries.async_update_entry(
config_entry,
data=new_data,
minor_version=0,
version=2,
)

# Migrate device.
await _async_migrate_device_identifiers(hass, config_entry)

# Migrate entities.
await er.async_migrate_entries(
hass,
config_entry.entry_id,
_migrate_entities_domain,
)

_LOGGER.debug("Migration to configuration version 2.0 successful")

return True


async def _async_migrate_device_identifiers(
hass: HomeAssistant,
config_entry: ConfigEntry,
) -> None:
"""Migrate the device identifiers to the new format."""
device_registry = dr.async_get(hass)
device_entry_found = False
for device_entry in dr.async_entries_for_config_entry(
device_registry,
config_entry.entry_id,
):
for identifier in device_entry.identifiers:
# Device found in registry. Update identifiers.
new_identifiers = {
(
DOMAIN,
str(identifier[1]),
),
}
_LOGGER.debug(
"Migrating device identifiers from %s to %s",
device_entry.identifiers,
new_identifiers,
)
device_registry.async_update_device(
device_id=device_entry.id,
new_identifiers=new_identifiers,
)
# Device entry found. Leave inner for loop.
device_entry_found = True
break

# Leave outer for loop if device entry is already found.
if device_entry_found:
break


@callback
def _migrate_entities_domain(entity_entry: er.RegistryEntry) -> dict[str, Any]: # noqa: ARG001
"""Migrate entities domain to the new name."""
return {"platform": DOMAIN}
2 changes: 1 addition & 1 deletion custom_components/midea_ac_lan/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from homeassistant.const import Platform

DOMAIN = "midea_ac_lan"
DOMAIN = "midea_lan"
COMPONENT = "component"
DEVICES = "devices"

Expand Down
4 changes: 2 additions & 2 deletions custom_components/midea_ac_lan/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"domain": "midea_ac_lan",
"name": "Midea AC LAN",
"domain": "midea_lan",
"name": "Midea LAN",
"codeowners": ["@wuwentao", "@rokam", "@chemelli74", "@Necroneco"],
"config_flow": true,
"dependencies": [],
Expand Down

0 comments on commit 64318ad

Please sign in to comment.