Skip to content

Commit

Permalink
UPB integration: Change unique ID from int to string. (#130832)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwww authored Nov 18, 2024
1 parent e9eaeed commit 75199a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
21 changes: 21 additions & 0 deletions homeassistant/components/upb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Support the UPB PIM."""

import logging

import upb_lib

from homeassistant.config_entries import ConfigEntry
Expand All @@ -14,6 +16,7 @@
EVENT_UPB_SCENE_CHANGED,
)

_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.LIGHT, Platform.SCENE]


Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion homeassistant/components/upb/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -98,7 +99,7 @@ async def async_step_user(
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(
Expand Down
25 changes: 25 additions & 0 deletions tests/components/upb/test_init.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 75199a9

Please sign in to comment.