diff --git a/custom_components/midea_ac_lan/__init__.py b/custom_components/midea_ac_lan/__init__.py index 1d983176..8226a424 100644 --- a/custom_components/midea_ac_lan/__init__.py +++ b/custom_components/midea_ac_lan/__init__.py @@ -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 ( @@ -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 @@ -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} diff --git a/custom_components/midea_ac_lan/const.py b/custom_components/midea_ac_lan/const.py index 9a21b780..25efbbd0 100644 --- a/custom_components/midea_ac_lan/const.py +++ b/custom_components/midea_ac_lan/const.py @@ -4,7 +4,7 @@ from homeassistant.const import Platform -DOMAIN = "midea_ac_lan" +DOMAIN = "midea_lan" COMPONENT = "component" DEVICES = "devices" diff --git a/custom_components/midea_ac_lan/manifest.json b/custom_components/midea_ac_lan/manifest.json index 8cde87fb..0142b2f5 100644 --- a/custom_components/midea_ac_lan/manifest.json +++ b/custom_components/midea_ac_lan/manifest.json @@ -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": [],