Make Laundrify unique id a string (#127092)

pull/127104/head
Joost Lekkerkerker 2024-09-30 13:21:20 +02:00 committed by GitHub
parent 92a6f231a9
commit 352987db7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 2 deletions

View File

@ -2,6 +2,8 @@
from __future__ import annotations
import logging
from laundrify_aio import LaundrifyAPI
from laundrify_aio.exceptions import ApiConnectionException, UnauthorizedException
@ -14,6 +16,8 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DEFAULT_POLL_INTERVAL, DOMAIN
from .coordinator import LaundrifyUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
@ -51,3 +55,21 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate entry."""
_LOGGER.debug("Migrating from version %s", entry.version)
if entry.version == 1:
# 1 -> 2: Unique ID from integer to string
if entry.minor_version == 1:
minor_version = 2
hass.config_entries.async_update_entry(
entry, unique_id=str(entry.unique_id), minor_version=minor_version
)
_LOGGER.debug("Migration successful")
return True

View File

@ -29,6 +29,7 @@ class LaundrifyConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for laundrify."""
VERSION = 1
MINOR_VERSION = 2
async def async_step_user(
self, user_input: dict[str, Any] | None = None
@ -64,7 +65,7 @@ class LaundrifyConfigFlow(ConfigFlow, domain=DOMAIN):
else:
entry_data = {CONF_ACCESS_TOKEN: access_token}
await self.async_set_unique_id(account_id)
await self.async_set_unique_id(str(account_id))
self._abort_if_unique_id_configured()
# Create a new entry if it doesn't exist

View File

@ -41,6 +41,7 @@ async def laundrify_setup_config_entry(
domain=DOMAIN,
unique_id=VALID_ACCOUNT_ID,
data={CONF_ACCESS_TOKEN: access_token},
minor_version=2,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -54,7 +55,7 @@ def laundrify_api_fixture(hass_client: ClientSessionGenerator):
with (
patch(
"laundrify_aio.LaundrifyAPI.get_account_id",
return_value=VALID_ACCOUNT_ID,
return_value=1234,
),
patch(
"laundrify_aio.LaundrifyAPI.validate_token",

View File

@ -32,6 +32,7 @@ async def test_form(hass: HomeAssistant) -> None:
assert result["data"] == {
CONF_ACCESS_TOKEN: VALID_ACCESS_TOKEN,
}
assert result["result"].unique_id == "1234"
async def test_form_invalid_format(hass: HomeAssistant, laundrify_api_mock) -> None:

View File

@ -4,8 +4,11 @@ from laundrify_aio import exceptions
from homeassistant.components.laundrify.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from .const import VALID_ACCESS_TOKEN
from tests.common import MockConfigEntry
@ -53,3 +56,19 @@ async def test_setup_entry_unload(
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert laundrify_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_migrate_entry_minor_version_1_2(hass: HomeAssistant) -> None:
"""Test migrating a 1.1 config entry to 1.2."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_ACCESS_TOKEN: VALID_ACCESS_TOKEN},
version=1,
minor_version=1,
unique_id=123456,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
assert entry.version == 1
assert entry.minor_version == 2
assert entry.unique_id == "123456"