diff --git a/homeassistant/components/zwave_js/__init__.py b/homeassistant/components/zwave_js/__init__.py index 5d294931e66..b1fc585394e 100644 --- a/homeassistant/components/zwave_js/__init__.py +++ b/homeassistant/components/zwave_js/__init__.py @@ -770,3 +770,14 @@ def async_ensure_addon_updated(hass: HomeAssistant) -> None: if addon_manager.task_in_progress(): raise ConfigEntryNotReady addon_manager.async_schedule_update_addon(catch_error=True) + + +async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: + """Migrate old entry.""" + if isinstance(config_entry.unique_id, int): # type: ignore[unreachable] + hass.config_entries.async_update_entry( # type: ignore[unreachable] + config_entry, + unique_id=str(config_entry.unique_id), + ) + + return True diff --git a/homeassistant/components/zwave_js/config_flow.py b/homeassistant/components/zwave_js/config_flow.py index c76e0692ad5..609da3390cc 100644 --- a/homeassistant/components/zwave_js/config_flow.py +++ b/homeassistant/components/zwave_js/config_flow.py @@ -411,7 +411,7 @@ class ConfigFlow(BaseZwaveJSFlow, config_entries.ConfigFlow, domain=DOMAIN): errors["base"] = "unknown" else: await self.async_set_unique_id( - version_info.home_id, raise_on_progress=False + str(version_info.home_id), raise_on_progress=False ) # Make sure we disable any add-on handling # if the controller is reconfigured in a manual step. @@ -445,7 +445,7 @@ class ConfigFlow(BaseZwaveJSFlow, config_entries.ConfigFlow, domain=DOMAIN): except CannotConnect: return self.async_abort(reason="cannot_connect") - await self.async_set_unique_id(version_info.home_id) + await self.async_set_unique_id(str(version_info.home_id)) self._abort_if_unique_id_configured(updates={CONF_URL: self.ws_address}) return await self.async_step_hassio_confirm() @@ -579,7 +579,7 @@ class ConfigFlow(BaseZwaveJSFlow, config_entries.ConfigFlow, domain=DOMAIN): raise AbortFlow("cannot_connect") from err await self.async_set_unique_id( - self.version_info.home_id, raise_on_progress=False + str(self.version_info.home_id), raise_on_progress=False ) self._abort_if_unique_id_configured( diff --git a/tests/components/zwave_js/test_config_flow.py b/tests/components/zwave_js/test_config_flow.py index 66fc9934ee1..3e40d67e2a4 100644 --- a/tests/components/zwave_js/test_config_flow.py +++ b/tests/components/zwave_js/test_config_flow.py @@ -169,7 +169,7 @@ async def test_manual(hass): } assert len(mock_setup.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 - assert result2["result"].unique_id == 1234 + assert result2["result"].unique_id == "1234" async def slow_server_version(*args): @@ -243,7 +243,7 @@ async def test_manual_already_configured(hass): "integration_created_addon": True, }, title=TITLE, - unique_id=1234, + unique_id="1234", ) entry.add_to_hass(hass) @@ -401,7 +401,10 @@ async def test_abort_discovery_with_existing_entry( """Test discovery flow is aborted if an entry already exists.""" entry = MockConfigEntry( - domain=DOMAIN, data={"url": "ws://localhost:3000"}, title=TITLE, unique_id=1234 + domain=DOMAIN, + data={"url": "ws://localhost:3000"}, + title=TITLE, + unique_id="1234", ) entry.add_to_hass(hass) @@ -1039,7 +1042,7 @@ async def test_addon_running_already_configured( "s2_unauthenticated_key": "old987", }, title=TITLE, - unique_id=1234, + unique_id="1234", ) entry.add_to_hass(hass) @@ -1370,7 +1373,7 @@ async def test_addon_installed_already_configured( "s2_unauthenticated_key": "old987", }, title=TITLE, - unique_id=1234, + unique_id="1234", ) entry.add_to_hass(hass) diff --git a/tests/components/zwave_js/test_init.py b/tests/components/zwave_js/test_init.py index 7b3fd773839..9c260eda4c1 100644 --- a/tests/components/zwave_js/test_init.py +++ b/tests/components/zwave_js/test_init.py @@ -8,6 +8,7 @@ from zwave_js_server.exceptions import BaseZwaveJSServerError, InvalidServerVers from zwave_js_server.model.node import Node from homeassistant.components.hassio.handler import HassioAPIError +from homeassistant.components.zwave_js import async_migrate_entry from homeassistant.components.zwave_js.const import DOMAIN from homeassistant.components.zwave_js.helpers import get_device_id from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState @@ -1327,3 +1328,12 @@ async def test_disabled_entity_on_value_removed(hass, zp3111, client, integratio | {battery_level_entity, binary_cover_entity, sensor_cover_entity} == new_unavailable_entities ) + + +async def test_async_migrate_entry(hass): + """Test async_migrate_entry.""" + entry = MockConfigEntry(domain=DOMAIN, unique_id=123456789) + assert isinstance(entry.unique_id, int) + await async_migrate_entry(hass, entry) + assert isinstance(entry.unique_id, str) + assert entry.unique_id == "123456789"