From 6dd9aec8b3104b2d7f874d80b3341148bacd1273 Mon Sep 17 00:00:00 2001 From: DanielV Date: Thu, 24 Apr 2025 06:56:23 +0000 Subject: [PATCH 1/2] Correct version migration --- custom_components/poollab/__init__.py | 43 ++++++++++++++---------- custom_components/poollab/config_flow.py | 5 ++- custom_components/poollab/const.py | 9 +++++ 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 custom_components/poollab/const.py diff --git a/custom_components/poollab/__init__.py b/custom_components/poollab/__init__.py index 8ee2f14..c91c76d 100644 --- a/custom_components/poollab/__init__.py +++ b/custom_components/poollab/__init__.py @@ -11,25 +11,18 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import CONF_API_KEY, CONF_URL from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError +from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from .config_flow import PoolLabConfigFlow +from .const import DOMAIN from .poollab import API_ENDPOINT, CloudAccount, PoolLabApi -DOMAIN = "poollab" PLATFORMS = ["sensor"] _LOGGER = logging.getLogger(__name__) -class InvalidAuth(HomeAssistantError): - """Error to indicate there is invalid auth.""" - - -class PoolLabConfigException(HomeAssistantError): - """Error to indicate there is an error in config.""" - - class PoolLabCoordinator(DataUpdateCoordinator[CloudAccount]): """Coordinator for the PoolLab API.""" @@ -101,16 +94,32 @@ async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Migrate old entry.""" - version = config_entry.version - _LOGGER.debug("Migrating from version %s", version) + + installed_version = PoolLabConfigFlow.VERSION + installed_minor_version = PoolLabConfigFlow.MINOR_VERSION new_data = {**config_entry.data} + new_options = {**config_entry.options} + + _LOGGER.debug( + "Migrating from version %s.%s", config_entry.version, config_entry.minor_version + ) - if version == 1: - config_entry.version = 2 + if CONF_URL not in config_entry.data: new_data[CONF_URL] = API_ENDPOINT - hass.config_entries.async_update_entry(config_entry, data=new_data) - _LOGGER.info("Migration to version %s successful", config_entry.version) - return True + + hass.config_entries.async_update_entry( + config_entry, + data=new_data, + options=new_options, + version=installed_version, + minor_version=installed_minor_version, + ) + _LOGGER.info( + "Migration to version %s.%s successful", + installed_version, + installed_minor_version, + ) + return True return False diff --git a/custom_components/poollab/config_flow.py b/custom_components/poollab/config_flow.py index 00d89c0..af860b3 100644 --- a/custom_components/poollab/config_flow.py +++ b/custom_components/poollab/config_flow.py @@ -13,7 +13,7 @@ from homeassistant.data_entry_flow import FlowResult import homeassistant.helpers.config_validation as cv -from . import DOMAIN, InvalidAuth +from .const import DOMAIN, InvalidAuth from .poollab import API_ENDPOINT, PoolLabApi _LOGGER = logging.getLogger(__name__) @@ -28,6 +28,9 @@ class PoolLabConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """PoolLab config flow.""" VERSION = 2 + MINOR_VERSION = 2 + data = None + options = None _reauth_entry: config_entries.ConfigEntry | None = None async def async_step_user( diff --git a/custom_components/poollab/const.py b/custom_components/poollab/const.py new file mode 100644 index 0000000..dc29132 --- /dev/null +++ b/custom_components/poollab/const.py @@ -0,0 +1,9 @@ +"""Common constants for integration.""" + +from homeassistant.exceptions import HomeAssistantError + +DOMAIN = "poollab" + + +class InvalidAuth(HomeAssistantError): + """Error to indicate there is invalid auth.""" From e76a3e746e46730e55458750c6d5da83096529a8 Mon Sep 17 00:00:00 2001 From: DanielV Date: Thu, 24 Apr 2025 06:58:43 +0000 Subject: [PATCH 2/2] Set config version to 1.2 --- custom_components/poollab/config_flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/poollab/config_flow.py b/custom_components/poollab/config_flow.py index af860b3..4712979 100644 --- a/custom_components/poollab/config_flow.py +++ b/custom_components/poollab/config_flow.py @@ -27,7 +27,7 @@ class PoolLabConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """PoolLab config flow.""" - VERSION = 2 + VERSION = 1 MINOR_VERSION = 2 data = None options = None