Migrate config entry in anova to remove devices from entry data (#128934)

pull/129143/head
G Johansson 2024-10-25 12:03:39 +02:00 committed by GitHub
parent 3c342077d6
commit 48a0eb90a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 63 additions and 6 deletions

View File

@ -13,7 +13,7 @@ from anova_wifi import (
WebsocketFailure, WebsocketFailure,
) )
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.const import CONF_DEVICES, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
@ -71,3 +71,25 @@ async def async_unload_entry(hass: HomeAssistant, entry: AnovaConfigEntry) -> bo
# Disconnect from WS # Disconnect from WS
await entry.runtime_data.api.disconnect_websocket() await entry.runtime_data.api.disconnect_websocket()
return unload_ok return unload_ok
async def async_migrate_entry(hass: HomeAssistant, entry: AnovaConfigEntry) -> bool:
"""Migrate entry."""
_LOGGER.debug("Migrating from version %s:%s", entry.version, entry.minor_version)
if entry.version > 1:
# This means the user has downgraded from a future version
return False
if entry.version == 1 and entry.minor_version == 1:
new_data = {**entry.data}
if CONF_DEVICES in new_data:
new_data.pop(CONF_DEVICES)
hass.config_entries.async_update_entry(entry, data=new_data, minor_version=2)
_LOGGER.debug(
"Migration to version %s:%s successful", entry.version, entry.minor_version
)
return True

View File

@ -6,7 +6,7 @@ from anova_wifi import AnovaApi, InvalidLogin
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_DEVICES, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN from .const import DOMAIN
@ -16,6 +16,7 @@ class AnovaConfligFlow(ConfigFlow, domain=DOMAIN):
"""Sets up a config flow for Anova.""" """Sets up a config flow for Anova."""
VERSION = 1 VERSION = 1
MINOR_VERSION = 2
async def async_step_user( async def async_step_user(
self, user_input: dict[str, str] | None = None self, user_input: dict[str, str] | None = None
@ -42,8 +43,6 @@ class AnovaConfligFlow(ConfigFlow, domain=DOMAIN):
data={ data={
CONF_USERNAME: user_input[CONF_USERNAME], CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input[CONF_PASSWORD], CONF_PASSWORD: user_input[CONF_PASSWORD],
# this can be removed in a migration to 1.2 in 2024.11
CONF_DEVICES: [],
}, },
) )

View File

@ -36,6 +36,7 @@ def create_entry(hass: HomeAssistant, device_id: str = DEVICE_UNIQUE_ID) -> Conf
}, },
unique_id="sample@gmail.com", unique_id="sample@gmail.com",
version=1, version=1,
minor_version=2,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
return entry return entry

View File

@ -6,7 +6,7 @@ from anova_wifi import AnovaApi, InvalidLogin
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.anova.const import DOMAIN from homeassistant.components.anova.const import DOMAIN
from homeassistant.const import CONF_DEVICES, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
@ -27,7 +27,6 @@ async def test_flow_user(hass: HomeAssistant, anova_api: AnovaApi) -> None:
assert result["data"] == { assert result["data"] == {
CONF_USERNAME: "sample@gmail.com", CONF_USERNAME: "sample@gmail.com",
CONF_PASSWORD: "sample", CONF_PASSWORD: "sample",
CONF_DEVICES: [],
} }

View File

@ -1,13 +1,18 @@
"""Test init for Anova.""" """Test init for Anova."""
from unittest.mock import patch
from anova_wifi import AnovaApi from anova_wifi import AnovaApi
from homeassistant.components.anova.const import DOMAIN from homeassistant.components.anova.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_DEVICES, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import async_init_integration, create_entry from . import async_init_integration, create_entry
from tests.common import MockConfigEntry
async def test_async_setup_entry(hass: HomeAssistant, anova_api: AnovaApi) -> None: async def test_async_setup_entry(hass: HomeAssistant, anova_api: AnovaApi) -> None:
"""Test a successful setup entry.""" """Test a successful setup entry."""
@ -55,3 +60,34 @@ async def test_websocket_failure(
"""Test that we successfully handle a websocket failure on setup.""" """Test that we successfully handle a websocket failure on setup."""
entry = await async_init_integration(hass) entry = await async_init_integration(hass)
assert entry.state is ConfigEntryState.SETUP_RETRY assert entry.state is ConfigEntryState.SETUP_RETRY
async def test_migration_removing_devices_in_config_entry(
hass: HomeAssistant, anova_api: AnovaApi
) -> None:
"""Test a successful setup entry."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Anova",
data={
CONF_USERNAME: "sample@gmail.com",
CONF_PASSWORD: "sample",
CONF_DEVICES: [],
},
unique_id="sample@gmail.com",
version=1,
minor_version=1,
)
entry.add_to_hass(hass)
with patch("homeassistant.components.anova.AnovaApi.authenticate"):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.anova_precision_cooker_mode")
assert state is not None
assert state.state == "idle"
assert entry.version == 1
assert entry.minor_version == 2
assert CONF_DEVICES not in entry.data