Fix race in otbr config flow (#111044)

* fix otbr

* Update homeassistant/components/otbr/config_flow.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

* unique ids should not change

* handle missing unique id

* handle missing unique id

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
pull/111119/head^2
J. Nick Koston 2024-02-21 21:02:44 -06:00 committed by GitHub
parent 345228429e
commit 5267e4f5db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 12 deletions

View File

@ -144,13 +144,14 @@ class OTBRConfigFlow(ConfigFlow, domain=DOMAIN):
for current_entry in current_entries:
if current_entry.source != SOURCE_HASSIO:
continue
if current_entry.unique_id != discovery_info.uuid:
self.hass.config_entries.async_update_entry(
current_entry, unique_id=discovery_info.uuid
)
current_url = yarl.URL(current_entry.data["url"])
if (
current_url.host != config["host"]
# The first version did not set a unique_id
# so if the entry does not have a unique_id
# we have to assume it's the first version
current_entry.unique_id
and (current_entry.unique_id != discovery_info.uuid)
or current_url.host != config["host"]
or current_url.port == config["port"]
):
continue

View File

@ -368,14 +368,14 @@ async def test_hassio_discovery_flow_2x_addons(
"homeassistant.components.otbr.async_setup_entry",
return_value=True,
) as mock_setup_entry:
results = await asyncio.gather(
hass.config_entries.flow.async_init(
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
),
hass.config_entries.flow.async_init(
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA_2
),
result1 = await hass.config_entries.flow.async_init(
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
)
result2 = await hass.config_entries.flow.async_init(
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA_2
)
results = [result1, result2]
expected_data = {
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
@ -578,6 +578,38 @@ async def test_hassio_discovery_flow_404(
assert result["reason"] == "unknown"
async def test_hassio_discovery_flow_new_port_missing_unique_id(
hass: HomeAssistant,
) -> None:
"""Test the port can be updated when the unique id is missing."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']+1}"
},
domain=otbr.DOMAIN,
options={},
source="hassio",
title="Open Thread Border Router",
)
config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"
expected_data = {
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
}
config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0]
assert config_entry.data == expected_data
async def test_hassio_discovery_flow_new_port(hass: HomeAssistant) -> None:
"""Test the port can be updated."""
mock_integration(hass, MockModule("hassio"))
@ -591,6 +623,7 @@ async def test_hassio_discovery_flow_new_port(hass: HomeAssistant) -> None:
options={},
source="hassio",
title="Open Thread Border Router",
unique_id=HASSIO_DATA.uuid,
)
config_entry.add_to_hass(hass)