diff --git a/custom_components/irm_kmi/__init__.py b/custom_components/irm_kmi/__init__.py index 29db1df..8644ac3 100644 --- a/custom_components/irm_kmi/__init__.py +++ b/custom_components/irm_kmi/__init__.py @@ -7,9 +7,10 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryError -from .const import (CONF_DARK_MODE, CONF_STYLE, OPTION_STYLE_STD, DOMAIN, - PLATFORMS, OPTION_DEPRECATED_FORECAST_NOT_USED, - CONF_USE_DEPRECATED_FORECAST) +from .const import (CONF_DARK_MODE, CONF_STYLE, CONF_USE_DEPRECATED_FORECAST, + CONFIG_FLOW_VERSION, DOMAIN, + OPTION_DEPRECATED_FORECAST_NOT_USED, OPTION_STYLE_STD, + PLATFORMS) from .coordinator import IrmKmiCoordinator from .weather import IrmKmiWeather @@ -51,7 +52,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry): """Migrate old entry.""" _LOGGER.debug(f"Migrating from version {config_entry.version}") - if config_entry.version > 2: + if config_entry.version > CONFIG_FLOW_VERSION - 1: # This means the user has downgraded from a future version return False diff --git a/custom_components/irm_kmi/config_flow.py b/custom_components/irm_kmi/config_flow.py index b55edb0..e907336 100644 --- a/custom_components/irm_kmi/config_flow.py +++ b/custom_components/irm_kmi/config_flow.py @@ -3,7 +3,7 @@ import logging import voluptuous as vol from homeassistant.components.zone import DOMAIN as ZONE_DOMAIN -from homeassistant.config_entries import ConfigFlow, OptionsFlow, ConfigEntry +from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow from homeassistant.const import CONF_ZONE from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult @@ -13,16 +13,18 @@ from homeassistant.helpers.selector import (EntitySelector, SelectSelectorConfig, SelectSelectorMode) -from .utils import get_config_value from .const import (CONF_DARK_MODE, CONF_STYLE, CONF_STYLE_OPTIONS, - OPTION_STYLE_STD, DOMAIN, CONF_USE_DEPRECATED_FORECAST, OPTION_DEPRECATED_FORECAST_NOT_USED, - CONF_USE_DEPRECATED_FORECAST_OPTIONS) + CONF_USE_DEPRECATED_FORECAST, + CONF_USE_DEPRECATED_FORECAST_OPTIONS, CONFIG_FLOW_VERSION, + DOMAIN, OPTION_DEPRECATED_FORECAST_NOT_USED, + OPTION_STYLE_STD) +from .utils import get_config_value _LOGGER = logging.getLogger(__name__) class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN): - VERSION = 3 + VERSION = CONFIG_FLOW_VERSION @staticmethod @callback diff --git a/custom_components/irm_kmi/const.py b/custom_components/irm_kmi/const.py index 8999d78..716e7c5 100644 --- a/custom_components/irm_kmi/const.py +++ b/custom_components/irm_kmi/const.py @@ -16,6 +16,8 @@ from homeassistant.const import Platform DOMAIN: Final = 'irm_kmi' PLATFORMS: Final = [Platform.WEATHER, Platform.CAMERA] +CONFIG_FLOW_VERSION = 3 + OUT_OF_BENELUX: Final = ["außerhalb der Benelux (Brussels)", "Hors de Belgique (Bxl)", "Outside the Benelux (Brussels)", diff --git a/custom_components/irm_kmi/coordinator.py b/custom_components/irm_kmi/coordinator.py index f5c1f17..0685d8b 100644 --- a/custom_components/irm_kmi/coordinator.py +++ b/custom_components/irm_kmi/coordinator.py @@ -7,6 +7,7 @@ from typing import Any, List, Tuple import async_timeout import pytz +from PIL import Image, ImageDraw, ImageFont from homeassistant.components.weather import Forecast from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, CONF_ZONE @@ -15,7 +16,6 @@ from homeassistant.exceptions import ConfigEntryError from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import (DataUpdateCoordinator, UpdateFailed) -from PIL import Image, ImageDraw, ImageFont from .api import IrmKmiApiClient, IrmKmiApiError from .const import CONF_DARK_MODE, CONF_STYLE, OPTION_STYLE_SATELLITE diff --git a/custom_components/irm_kmi/weather.py b/custom_components/irm_kmi/weather.py index 3396e66..4ae149c 100644 --- a/custom_components/irm_kmi/weather.py +++ b/custom_components/irm_kmi/weather.py @@ -1,5 +1,4 @@ """Support for IRM KMI weather.""" -import asyncio import logging from typing import List @@ -13,9 +12,10 @@ from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from . import DOMAIN, CONF_USE_DEPRECATED_FORECAST -from .const import OPTION_DEPRECATED_FORECAST_HOURLY, OPTION_DEPRECATED_FORECAST_NOT_USED, \ - OPTION_DEPRECATED_FORECAST_DAILY +from . import CONF_USE_DEPRECATED_FORECAST, DOMAIN +from .const import (OPTION_DEPRECATED_FORECAST_DAILY, + OPTION_DEPRECATED_FORECAST_HOURLY, + OPTION_DEPRECATED_FORECAST_NOT_USED) from .coordinator import IrmKmiCoordinator from .utils import get_config_value diff --git a/tests/conftest.py b/tests/conftest.py index ef9bfe5..5ad0d4f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,9 +11,9 @@ from pytest_homeassistant_custom_component.common import (MockConfigEntry, load_fixture) from custom_components.irm_kmi.api import IrmKmiApiParametersError -from custom_components.irm_kmi.const import (CONF_DARK_MODE, CONF_STYLE, - OPTION_STYLE_STD, DOMAIN, OPTION_DEPRECATED_FORECAST_NOT_USED, - CONF_USE_DEPRECATED_FORECAST) +from custom_components.irm_kmi.const import ( + CONF_DARK_MODE, CONF_STYLE, CONF_USE_DEPRECATED_FORECAST, DOMAIN, + OPTION_DEPRECATED_FORECAST_NOT_USED, OPTION_STYLE_STD) async def patched(url: str, params: dict | None = None) -> bytes: diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index 38d2a60..7854279 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -7,10 +7,13 @@ from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_ZONE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from pytest_homeassistant_custom_component.common import MockConfigEntry -from custom_components.irm_kmi.const import (CONF_DARK_MODE, CONF_STYLE, - OPTION_STYLE_STD, DOMAIN, CONF_USE_DEPRECATED_FORECAST, - OPTION_DEPRECATED_FORECAST_NOT_USED) +from custom_components.irm_kmi import async_migrate_entry +from custom_components.irm_kmi.const import ( + CONF_DARK_MODE, CONF_STYLE, CONF_USE_DEPRECATED_FORECAST, + CONFIG_FLOW_VERSION, DOMAIN, OPTION_DEPRECATED_FORECAST_NOT_USED, + OPTION_STYLE_STD) async def test_full_user_flow( @@ -38,3 +41,20 @@ async def test_full_user_flow( CONF_STYLE: OPTION_STYLE_STD, CONF_DARK_MODE: False, CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_NOT_USED} + + +async def test_config_entry_migration(hass: HomeAssistant) -> None: + """Ensure that config entry migration takes the configuration to the latest version""" + entry = MockConfigEntry( + title="Home", + domain=DOMAIN, + data={CONF_ZONE: "zone.home"}, + unique_id="zone.home", + version=1 + ) + + entry.add_to_hass(hass) + await async_migrate_entry(hass, entry) + result_entry = hass.config_entries.async_get_entry(entry_id=entry.entry_id) + + assert result_entry.version == CONFIG_FLOW_VERSION