diff --git a/custom_components/solvis_control/__init__.py b/custom_components/solvis_control/__init__.py index 1529e3f..3b929a4 100644 --- a/custom_components/solvis_control/__init__.py +++ b/custom_components/solvis_control/__init__.py @@ -7,6 +7,7 @@ """Solvis integration.""" from datetime import timedelta +import logging from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_IP_ADDRESS, Platform @@ -32,10 +33,15 @@ Platform.SWITCH, ] +_LOGGER = logging.getLogger(__name__) + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Solvis device from a config entry.""" + if not await async_migrate_entry(hass, entry): + return False + conf_host = entry.data.get(CONF_HOST) conf_port = entry.data.get(CONF_PORT) @@ -91,3 +97,36 @@ async def options_update_listener(hass: HomeAssistant, config_entry: ConfigEntry # Refresh the coordinator to get the latest data coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR] await coordinator.async_refresh() + + +async def async_migrate_entry(hass, config_entry: ConfigEntry): + """Migrate old entry.""" + _LOGGER.debug( + "Migrating configuration from version %s.%s", + config_entry.version, + config_entry.minor_version, + ) + + if config_entry.version > 1: + # This means the user has downgraded from a future version + return False + + if config_entry.version == 1: + + new_data = {**config_entry.data} + if config_entry.minor_version < 3: + _LOGGER.info(f"Migrating from version {config_entry.version}") + if CONF_OPTION_1 not in new_data: + new_data[CONF_OPTION_1] = False + if CONF_OPTION_2 not in new_data: + new_data[CONF_OPTION_2] = False + if CONF_OPTION_3 not in new_data: + new_data[CONF_OPTION_3] = False + if CONF_OPTION_4 not in new_data: + new_data[CONF_OPTION_4] = False + if DEVICE_VERSION not in new_data: + new_data[DEVICE_VERSION] = "SC3" + config_entry.minor_version = 3 + hass.config_entries.async_update_entry(config_entry, data=new_data) + _LOGGER.info(f"Migration to version {config_entry.version} successful") + return True diff --git a/custom_components/solvis_control/config_flow.py b/custom_components/solvis_control/config_flow.py index 979dab5..d2dc8b9 100644 --- a/custom_components/solvis_control/config_flow.py +++ b/custom_components/solvis_control/config_flow.py @@ -95,7 +95,7 @@ def get_host_schema_options(data: ConfigType) -> Schema: class SolvisConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 - MINOR_VERSION = 2 + MINOR_VERSION = 3 def __init__(self) -> None: """Init the ConfigFlow.""" @@ -167,7 +167,7 @@ def async_get_options_flow( class SolvisOptionsFlow(config_entries.OptionsFlow): VERSION = 1 - MINOR_VERSION = 2 + MINOR_VERSION = 3 def __init__(self, config) -> None: """Init the ConfigFlow."""