From ea1f7919cbbf6e7ef5d069abc7e46897e6b7e2d0 Mon Sep 17 00:00:00 2001 From: moehrem <105084896+moehrem@users.noreply.github.com> Date: Mon, 3 Nov 2025 12:50:27 +0000 Subject: [PATCH] hotfix: migration, version, tests Changes to be committed: modified: custom_components/diveracontrol/__init__.py modified: custom_components/diveracontrol/manifest.json modified: tests/test_init.py --- custom_components/diveracontrol/__init__.py | 31 +++++++++++++------ custom_components/diveracontrol/manifest.json | 2 +- tests/test_init.py | 11 +++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/custom_components/diveracontrol/__init__.py b/custom_components/diveracontrol/__init__.py index 3ec9c4b..55e992f 100644 --- a/custom_components/diveracontrol/__init__.py +++ b/custom_components/diveracontrol/__init__.py @@ -124,18 +124,21 @@ async def async_unload_entry( async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Migrate old config_entry to the respective version. - HA Standard: method will be called if manifest version does not match the config_entry version. So no need to compare versions in coding, just check for the respective version. - Expect downgrades! + Note: config_entry.version and config_entry.minor_version are the CONFIG ENTRY + SCHEMA version numbers. They must be explicitly set during migration to match + the versions defined in ConfigFlow (VERSION and MINOR_VERSION). """ - - integrated_version = config_entry.data.get(D_INTEGRATION_VERSION, "0.0.0") + _LOGGER.debug( + "Checking migration from config_entry version %s.%s", + config_entry.version, + config_entry.minor_version, + ) # changing to v1.2.0 - # all versions before 1.2.0 do not have an integrated version - if integrated_version == "0.0.0": + if MINOR_VERSION == 2: _LOGGER.info( - "Migrating config entry to version %s.%s.%s", + "Migrating config entry to integration version %s.%s.%s", VERSION, MINOR_VERSION, PATCH_VERSION, @@ -143,6 +146,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> if D_INTEGRATION_VERSION not in config_entry.data: _LOGGER.info("Adding integration version to existing config entry") + # Update both the data AND the schema version hass.config_entries.async_update_entry( config_entry, data={ @@ -190,11 +194,11 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> "Failed to remove old entity registry entries during migration" ) - # changing to v1.2.1 + # changing to v1.3.0 # add new base_url parameter to config entry - if integrated_version == "1.2.0": + if MINOR_VERSION == 3: _LOGGER.info( - "Migrating config entry to version %s.%s.%s", + "Migrating config entry to integration version %s.%s.%s", VERSION, MINOR_VERSION, PATCH_VERSION, @@ -203,14 +207,21 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> if D_BASE_API_URL not in config_entry.data: _LOGGER.info("Adding base_url to existing config entry") + # Update both the data AND the schema version hass.config_entries.async_update_entry( config_entry, data={ **config_entry.data, D_BASE_API_URL: BASE_API_URL, + D_INTEGRATION_VERSION: f"{VERSION}.{MINOR_VERSION}.{PATCH_VERSION}", }, version=VERSION, minor_version=MINOR_VERSION, ) + _LOGGER.debug( + "Migration complete, config_entry is now at version %s.%s", + config_entry.version, + config_entry.minor_version, + ) return True diff --git a/custom_components/diveracontrol/manifest.json b/custom_components/diveracontrol/manifest.json index 60f6e2c..e6355ce 100644 --- a/custom_components/diveracontrol/manifest.json +++ b/custom_components/diveracontrol/manifest.json @@ -9,5 +9,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/moehrem/DiveraControl/issues", "requirements": [], - "version": "1.2.1" + "version": "1.3.0" } diff --git a/tests/test_init.py b/tests/test_init.py index 78fd232..3c1217b 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -3,7 +3,6 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest -from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from pytest_homeassistant_custom_component.common import MockConfigEntry @@ -21,9 +20,6 @@ D_INTEGRATION_VERSION, D_UCR_ID, DOMAIN, - MINOR_VERSION, - PATCH_VERSION, - VERSION, ) @@ -58,9 +54,10 @@ async def test_async_migrate_entry_from_v0_9(hass: HomeAssistant) -> None: ): result = await async_migrate_entry(hass, old_entry) - assert result is True - assert old_entry.version == VERSION - assert old_entry.minor_version == MINOR_VERSION + # Verify migration was successful and versions were updated to patched values + assert result is True + assert old_entry.version == 1 + assert old_entry.minor_version == 2 async def test_async_migrate_entry_from_v0_8_succeeds(hass: HomeAssistant) -> None: