UPB integration: Change unique ID from int to string. (#130832)

This commit is contained in:
Glenn Waters 2024-11-18 04:56:47 -05:00 committed by Franck Nijhof
parent 5b1aca53ac
commit a024acf096
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 48 additions and 1 deletions

View File

@ -1,5 +1,7 @@
"""Support the UPB PIM."""
import logging
import upb_lib
from homeassistant.config_entries import ConfigEntry
@ -14,6 +16,7 @@ from .const import (
EVENT_UPB_SCENE_CHANGED,
)
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.LIGHT, Platform.SCENE]
@ -63,3 +66,21 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
upb.disconnect()
hass.data[DOMAIN].pop(config_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

@ -78,6 +78,7 @@ class UPBConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for UPB PIM."""
VERSION = 1
MINOR_VERSION = 2
async def async_step_user(
self, user_input: dict[str, Any] | None = None
@ -98,7 +99,7 @@ class UPBConfigFlow(ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown"
if "base" not in errors:
await self.async_set_unique_id(network_id)
await self.async_set_unique_id(str(network_id))
self._abort_if_unique_id_configured()
return self.async_create_entry(

View File

@ -0,0 +1,25 @@
"""The init tests for the UPB platform."""
from unittest.mock import patch
from homeassistant.components.upb.const import DOMAIN
from homeassistant.core import HomeAssistant
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("homeassistant.components.upb.async_setup_entry", return_value=True):
entry = MockConfigEntry(
domain=DOMAIN,
data={"protocol": "TCP", "address": "1.2.3.4", "file_path": "upb.upe"},
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"