mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Migrate SMA unique id to str (#127732)
This commit is contained in:
parent
3b6f88cfa7
commit
1d132d7a1e
@ -135,3 +135,21 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
data[PYSMA_REMOVE_LISTENER]()
|
||||
|
||||
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
|
||||
|
@ -40,6 +40,7 @@ class SmaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for SMA."""
|
||||
|
||||
VERSION = 1
|
||||
MINOR_VERSION = 2
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize."""
|
||||
@ -76,7 +77,7 @@ class SmaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors["base"] = "unknown"
|
||||
|
||||
if not errors:
|
||||
await self.async_set_unique_id(device_info["serial"])
|
||||
await self.async_set_unique_id(str(device_info["serial"]))
|
||||
self._abort_if_unique_id_configured(updates=self._data)
|
||||
return self.async_create_entry(
|
||||
title=self._data[CONF_HOST], data=self._data
|
||||
|
@ -6,7 +6,7 @@ MOCK_DEVICE = {
|
||||
"manufacturer": "SMA",
|
||||
"name": "SMA Device Name",
|
||||
"type": "Sunny Boy 3.6",
|
||||
"serial": "123456789",
|
||||
"serial": 123456789,
|
||||
}
|
||||
|
||||
MOCK_USER_INPUT = {
|
||||
|
@ -22,9 +22,10 @@ def mock_config_entry() -> MockConfigEntry:
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=MOCK_DEVICE["name"],
|
||||
unique_id=MOCK_DEVICE["serial"],
|
||||
unique_id=str(MOCK_DEVICE["serial"]),
|
||||
data=MOCK_USER_INPUT,
|
||||
source=config_entries.SOURCE_IMPORT,
|
||||
minor_version=2,
|
||||
)
|
||||
|
||||
|
||||
|
27
tests/components/sma/test_init.py
Normal file
27
tests/components/sma/test_init.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""Test the sma init file."""
|
||||
|
||||
from homeassistant.components.sma.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import MOCK_DEVICE, MOCK_USER_INPUT, _patch_async_setup_entry
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_migrate_entry_minor_version_1_2(hass: HomeAssistant) -> None:
|
||||
"""Test migrating a 1.1 config entry to 1.2."""
|
||||
with _patch_async_setup_entry():
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=MOCK_DEVICE["name"],
|
||||
unique_id=MOCK_DEVICE["serial"], # Not converted to str
|
||||
data=MOCK_USER_INPUT,
|
||||
source=SOURCE_IMPORT,
|
||||
minor_version=1,
|
||||
)
|
||||
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 == str(MOCK_DEVICE["serial"])
|
Loading…
x
Reference in New Issue
Block a user