Update OTRB config entry if REST API port has changed (#90101)

* Update OTRB config entry if REST API port has changed

* Improve test coverage
This commit is contained in:
Erik Montnemery 2023-03-22 14:03:39 +01:00 committed by GitHub
parent 0ecd043cb2
commit 130c8ea5f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 9 deletions

View file

@ -8,10 +8,11 @@ import aiohttp
import python_otbr_api
from python_otbr_api import tlv_parser
import voluptuous as vol
import yarl
from homeassistant.components.hassio import HassioServiceInfo
from homeassistant.components.thread import async_get_preferred_dataset
from homeassistant.config_entries import ConfigFlow
from homeassistant.config_entries import SOURCE_HASSIO, ConfigFlow
from homeassistant.const import CONF_URL
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -86,11 +87,25 @@ class OTBRConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_step_hassio(self, discovery_info: HassioServiceInfo) -> FlowResult:
"""Handle hassio discovery."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
config = discovery_info.config
url = f"http://{config['host']}:{config['port']}"
config_entry_data = {"url": url}
if current_entries := self._async_current_entries():
for current_entry in current_entries:
if current_entry.source != SOURCE_HASSIO:
continue
current_url = yarl.URL(current_entry.data["url"])
if (
current_url.host != config["host"]
or current_url.port == config["port"]
):
continue
# Update URL with the new port
self.hass.config_entries.async_update_entry(
current_entry, data=config_entry_data
)
return self.async_abort(reason="single_instance_allowed")
try:
await self._connect_and_create_dataset(url)
@ -101,5 +116,5 @@ class OTBRConfigFlow(ConfigFlow, domain=DOMAIN):
await self.async_set_unique_id(DOMAIN)
return self.async_create_entry(
title="Open Thread Border Router",
data={"url": url},
data=config_entry_data,
)