Add config migration test and optimize imports

This commit is contained in:
Jules 2023-12-30 18:37:45 +01:00
parent e74e7c2873
commit b4685a397f
Signed by: jdejaegh
GPG key ID: 99D6D184CA66933A
7 changed files with 45 additions and 20 deletions

View file

@ -7,9 +7,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError from homeassistant.exceptions import ConfigEntryError
from .const import (CONF_DARK_MODE, CONF_STYLE, OPTION_STYLE_STD, DOMAIN, from .const import (CONF_DARK_MODE, CONF_STYLE, CONF_USE_DEPRECATED_FORECAST,
PLATFORMS, OPTION_DEPRECATED_FORECAST_NOT_USED, CONFIG_FLOW_VERSION, DOMAIN,
CONF_USE_DEPRECATED_FORECAST) OPTION_DEPRECATED_FORECAST_NOT_USED, OPTION_STYLE_STD,
PLATFORMS)
from .coordinator import IrmKmiCoordinator from .coordinator import IrmKmiCoordinator
from .weather import IrmKmiWeather from .weather import IrmKmiWeather
@ -51,7 +52,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
"""Migrate old entry.""" """Migrate old entry."""
_LOGGER.debug(f"Migrating from version {config_entry.version}") _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 # This means the user has downgraded from a future version
return False return False

View file

@ -3,7 +3,7 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.zone import DOMAIN as ZONE_DOMAIN 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.const import CONF_ZONE
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
@ -13,16 +13,18 @@ from homeassistant.helpers.selector import (EntitySelector,
SelectSelectorConfig, SelectSelectorConfig,
SelectSelectorMode) SelectSelectorMode)
from .utils import get_config_value
from .const import (CONF_DARK_MODE, CONF_STYLE, CONF_STYLE_OPTIONS, 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,
CONF_USE_DEPRECATED_FORECAST_OPTIONS) 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__) _LOGGER = logging.getLogger(__name__)
class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN): class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 3 VERSION = CONFIG_FLOW_VERSION
@staticmethod @staticmethod
@callback @callback

View file

@ -16,6 +16,8 @@ from homeassistant.const import Platform
DOMAIN: Final = 'irm_kmi' DOMAIN: Final = 'irm_kmi'
PLATFORMS: Final = [Platform.WEATHER, Platform.CAMERA] PLATFORMS: Final = [Platform.WEATHER, Platform.CAMERA]
CONFIG_FLOW_VERSION = 3
OUT_OF_BENELUX: Final = ["außerhalb der Benelux (Brussels)", OUT_OF_BENELUX: Final = ["außerhalb der Benelux (Brussels)",
"Hors de Belgique (Bxl)", "Hors de Belgique (Bxl)",
"Outside the Benelux (Brussels)", "Outside the Benelux (Brussels)",

View file

@ -7,6 +7,7 @@ from typing import Any, List, Tuple
import async_timeout import async_timeout
import pytz import pytz
from PIL import Image, ImageDraw, ImageFont
from homeassistant.components.weather import Forecast from homeassistant.components.weather import Forecast
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, CONF_ZONE 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.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import (DataUpdateCoordinator, from homeassistant.helpers.update_coordinator import (DataUpdateCoordinator,
UpdateFailed) UpdateFailed)
from PIL import Image, ImageDraw, ImageFont
from .api import IrmKmiApiClient, IrmKmiApiError from .api import IrmKmiApiClient, IrmKmiApiError
from .const import CONF_DARK_MODE, CONF_STYLE, OPTION_STYLE_SATELLITE from .const import CONF_DARK_MODE, CONF_STYLE, OPTION_STYLE_SATELLITE

View file

@ -1,5 +1,4 @@
"""Support for IRM KMI weather.""" """Support for IRM KMI weather."""
import asyncio
import logging import logging
from typing import List 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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import DOMAIN, CONF_USE_DEPRECATED_FORECAST from . import CONF_USE_DEPRECATED_FORECAST, DOMAIN
from .const import OPTION_DEPRECATED_FORECAST_HOURLY, OPTION_DEPRECATED_FORECAST_NOT_USED, \ from .const import (OPTION_DEPRECATED_FORECAST_DAILY,
OPTION_DEPRECATED_FORECAST_DAILY OPTION_DEPRECATED_FORECAST_HOURLY,
OPTION_DEPRECATED_FORECAST_NOT_USED)
from .coordinator import IrmKmiCoordinator from .coordinator import IrmKmiCoordinator
from .utils import get_config_value from .utils import get_config_value

View file

@ -11,9 +11,9 @@ from pytest_homeassistant_custom_component.common import (MockConfigEntry,
load_fixture) load_fixture)
from custom_components.irm_kmi.api import IrmKmiApiParametersError from custom_components.irm_kmi.api import IrmKmiApiParametersError
from custom_components.irm_kmi.const import (CONF_DARK_MODE, CONF_STYLE, from custom_components.irm_kmi.const import (
OPTION_STYLE_STD, DOMAIN, OPTION_DEPRECATED_FORECAST_NOT_USED, CONF_DARK_MODE, CONF_STYLE, CONF_USE_DEPRECATED_FORECAST, DOMAIN,
CONF_USE_DEPRECATED_FORECAST) OPTION_DEPRECATED_FORECAST_NOT_USED, OPTION_STYLE_STD)
async def patched(url: str, params: dict | None = None) -> bytes: async def patched(url: str, params: dict | None = None) -> bytes:

View file

@ -7,10 +7,13 @@ from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_ZONE from homeassistant.const import CONF_ZONE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType 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, from custom_components.irm_kmi import async_migrate_entry
OPTION_STYLE_STD, DOMAIN, CONF_USE_DEPRECATED_FORECAST, from custom_components.irm_kmi.const import (
OPTION_DEPRECATED_FORECAST_NOT_USED) 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( async def test_full_user_flow(
@ -38,3 +41,20 @@ async def test_full_user_flow(
CONF_STYLE: OPTION_STYLE_STD, CONF_STYLE: OPTION_STYLE_STD,
CONF_DARK_MODE: False, CONF_DARK_MODE: False,
CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_NOT_USED} 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