Make zwave_js config entry unique ID a string (#69163)

* Make zwave_js config entry unique ID a string

* Add test

* Fix tests
pull/69171/head
Raman Gupta 2022-04-02 23:52:07 -04:00 committed by GitHub
parent b92fc42f0f
commit e06856f965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 8 deletions

View File

@ -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

View File

@ -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(

View File

@ -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)

View File

@ -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"