Fix regression in zwave_js (#69312)

* Handle unique ID update during discovery step

* Use callback to convert unique IDs to strings

* Adjust test to make sure logic works

* Fix other tests

* Move comment

* Move migration to async_setup

* Remove async_migrate_entry since we take care of it during setup

* Remove unused test
This commit is contained in:
Raman Gupta 2022-04-06 00:40:33 -04:00 committed by GitHub
parent add01d434e
commit 79dc551f5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 33 deletions

View file

@ -113,6 +113,11 @@ DATA_INVALID_SERVER_VERSION_LOGGED = "invalid_server_version_logged"
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Z-Wave JS component."""
hass.data[DOMAIN] = {}
for entry in hass.config_entries.async_entries(DOMAIN):
if not isinstance(entry.unique_id, str):
hass.config_entries.async_update_entry(
entry, unique_id=str(entry.unique_id)
)
return True
@ -770,14 +775,3 @@ 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

@ -667,7 +667,7 @@ class OptionsFlowHandler(BaseZwaveJSFlow, config_entries.OptionsFlow):
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
if self.config_entry.unique_id != version_info.home_id:
if self.config_entry.unique_id != str(version_info.home_id):
return self.async_abort(reason="different_device")
# Make sure we disable any add-on handling
@ -827,7 +827,7 @@ class OptionsFlowHandler(BaseZwaveJSFlow, config_entries.OptionsFlow):
except CannotConnect:
return await self.async_revert_addon_config(reason="cannot_connect")
if self.config_entry.unique_id != self.version_info.home_id:
if self.config_entry.unique_id != str(self.version_info.home_id):
return await self.async_revert_addon_config(reason="different_device")
self._async_update_entry(

View file

@ -811,7 +811,10 @@ async def test_abort_usb_discovery_with_existing_flow(hass, supervisor, addon_op
async def test_abort_usb_discovery_already_configured(hass, supervisor, addon_options):
"""Test usb discovery flow is aborted when there is an existing entry."""
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)
@ -1042,9 +1045,10 @@ async def test_addon_running_already_configured(
"s2_unauthenticated_key": "old987",
},
title=TITLE,
unique_id="1234",
unique_id=1234, # Unique ID is purposely set to int to test migration logic
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -1560,7 +1564,7 @@ async def test_install_addon_failure(hass, supervisor, addon_installed, install_
async def test_options_manual(hass, client, integration):
"""Test manual settings in options flow."""
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
assert client.connect.call_count == 1
assert client.disconnect.call_count == 0
@ -1605,7 +1609,7 @@ async def test_options_manual_different_device(hass, integration):
async def test_options_not_addon(hass, client, supervisor, integration):
"""Test options flow and opting out of add-on on Supervisor."""
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
assert client.connect.call_count == 1
assert client.disconnect.call_count == 0
@ -1706,7 +1710,7 @@ async def test_options_addon_running(
"""Test options flow and add-on already running on Supervisor."""
addon_options.update(old_addon_options)
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
data = {**entry.data, **entry_data}
hass.config_entries.async_update_entry(entry, data=data)
@ -1816,7 +1820,7 @@ async def test_options_addon_running_no_changes(
"""Test options flow without changes, and add-on already running on Supervisor."""
addon_options.update(old_addon_options)
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
data = {**entry.data, **entry_data}
hass.config_entries.async_update_entry(entry, data=data)
@ -1929,7 +1933,7 @@ async def test_options_different_device(
"""Test options flow and configuring a different device."""
addon_options.update(old_addon_options)
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
data = {**entry.data, **entry_data}
hass.config_entries.async_update_entry(entry, data=data)
@ -2079,7 +2083,7 @@ async def test_options_addon_restart_failed(
"""Test options flow and add-on restart failure."""
addon_options.update(old_addon_options)
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
data = {**entry.data, **entry_data}
hass.config_entries.async_update_entry(entry, data=data)
@ -2200,7 +2204,7 @@ async def test_options_addon_running_server_info_failure(
"""Test options flow and add-on already running with server info failure."""
addon_options.update(old_addon_options)
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
data = {**entry.data, **entry_data}
hass.config_entries.async_update_entry(entry, data=data)
@ -2304,7 +2308,7 @@ async def test_options_addon_not_installed(
addon_installed.return_value["version"] = None
addon_options.update(old_addon_options)
entry = integration
entry.unique_id = 1234
entry.unique_id = "1234"
data = {**entry.data, **entry_data}
hass.config_entries.async_update_entry(entry, data=data)

View file

@ -8,7 +8,6 @@ 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
@ -1328,12 +1327,3 @@ 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"