Skip to content

Commit

Permalink
Migrate config entry in anova to remove devices from entry data
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed Oct 21, 2024
1 parent e7a7a18 commit e04d665
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
24 changes: 23 additions & 1 deletion homeassistant/components/anova/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
Expand Down Expand Up @@ -71,3 +71,25 @@ async def async_unload_entry(hass: HomeAssistant, entry: AnovaConfigEntry) -> bo
# Disconnect from WS
await entry.runtime_data.api.disconnect_websocket()
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

Check warning on line 82 in homeassistant/components/anova/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/anova/__init__.py#L82

Added line #L82 was not covered by tests

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
5 changes: 2 additions & 3 deletions homeassistant/components/anova/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import voluptuous as vol

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 .const import DOMAIN
Expand All @@ -16,6 +16,7 @@ class AnovaConfligFlow(ConfigFlow, domain=DOMAIN):
"""Sets up a config flow for Anova."""

VERSION = 1
MINOR_VERSION = 2

async def async_step_user(
self, user_input: dict[str, str] | None = None
Expand All @@ -42,8 +43,6 @@ async def async_step_user(
data={
CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input[CONF_PASSWORD],
# this can be removed in a migration to 1.2 in 2024.11
CONF_DEVICES: [],
},
)

Expand Down
1 change: 1 addition & 0 deletions tests/components/anova/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def create_entry(hass: HomeAssistant, device_id: str = DEVICE_UNIQUE_ID) -> Conf
},
unique_id="sample@gmail.com",
version=1,
minor_version=2,
)
entry.add_to_hass(hass)
return entry
Expand Down
3 changes: 1 addition & 2 deletions tests/components/anova/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant import config_entries
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.data_entry_flow import FlowResultType

Expand All @@ -27,7 +27,6 @@ async def test_flow_user(hass: HomeAssistant, anova_api: AnovaApi) -> None:
assert result["data"] == {
CONF_USERNAME: "sample@gmail.com",
CONF_PASSWORD: "sample",
CONF_DEVICES: [],
}


Expand Down
36 changes: 36 additions & 0 deletions tests/components/anova/test_init.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""Test init for Anova."""

from unittest.mock import patch

from anova_wifi import AnovaApi

from homeassistant.components.anova.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_DEVICES, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant

from . import async_init_integration, create_entry

from tests.common import MockConfigEntry


async def test_async_setup_entry(hass: HomeAssistant, anova_api: AnovaApi) -> None:
"""Test a successful setup entry."""
Expand Down Expand Up @@ -55,3 +60,34 @@ async def test_websocket_failure(
"""Test that we successfully handle a websocket failure on setup."""
entry = await async_init_integration(hass)
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

0 comments on commit e04d665

Please sign in to comment.