mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Handle discovering user configured Wyoming flow (#134916)
This commit is contained in:
parent
75ce89dc41
commit
b956aa68da
@ -9,7 +9,7 @@ from urllib.parse import urlparse
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import zeroconf
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.config_entries import SOURCE_HASSIO, ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.helpers.service_info.hassio import HassioServiceInfo
|
||||
|
||||
@ -69,6 +69,19 @@ class WyomingConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
await self.async_set_unique_id(discovery_info.uuid)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
uri = urlparse(discovery_info.config["uri"])
|
||||
for entry in self._async_current_entries(include_ignore=True):
|
||||
if (
|
||||
entry.data[CONF_HOST] == uri.hostname
|
||||
and entry.data[CONF_PORT] == uri.port
|
||||
):
|
||||
return self.async_update_reload_and_abort(
|
||||
entry,
|
||||
unique_id=discovery_info.uuid,
|
||||
reload_even_if_entry_is_unchanged=False,
|
||||
reason="already_configured",
|
||||
)
|
||||
|
||||
self._hassio_discovery = discovery_info
|
||||
self.context.update(
|
||||
{
|
||||
@ -126,6 +139,19 @@ class WyomingConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
self.context["title_placeholders"] = {"name": self._name}
|
||||
|
||||
for entry in self._async_current_entries(include_ignore=True):
|
||||
if (
|
||||
entry.data[CONF_HOST] == service.host
|
||||
and entry.data[CONF_PORT] == service.port
|
||||
and entry.source != SOURCE_HASSIO
|
||||
):
|
||||
return self.async_update_reload_and_abort(
|
||||
entry,
|
||||
unique_id=unique_id,
|
||||
reload_even_if_entry_is_unchanged=False,
|
||||
reason="already_configured",
|
||||
)
|
||||
|
||||
self._service = service
|
||||
return await self.async_step_zeroconf_confirm()
|
||||
|
||||
|
@ -178,11 +178,11 @@ async def test_hassio_addon_discovery(
|
||||
|
||||
async def test_hassio_addon_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test we abort discovery if the add-on is already configured."""
|
||||
MockConfigEntry(
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"host": "mock-piper", "port": "10200"},
|
||||
unique_id="1234",
|
||||
).add_to_hass(hass)
|
||||
data={"host": "mock-piper", "port": 10200},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
@ -191,6 +191,7 @@ async def test_hassio_addon_already_configured(hass: HomeAssistant) -> None:
|
||||
)
|
||||
assert result.get("type") is FlowResultType.ABORT
|
||||
assert result.get("reason") == "already_configured"
|
||||
assert entry.unique_id == "1234"
|
||||
|
||||
|
||||
async def test_hassio_addon_cannot_connect(hass: HomeAssistant) -> None:
|
||||
@ -297,3 +298,29 @@ async def test_zeroconf_discovery_no_services(
|
||||
|
||||
assert result.get("type") is FlowResultType.ABORT
|
||||
assert result.get("reason") == "no_services"
|
||||
|
||||
|
||||
async def test_zeroconf_discovery_already_configured(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test config flow initiated by Supervisor."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"host": "127.0.0.1", "port": 12345},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.wyoming.data.load_wyoming_info",
|
||||
return_value=SATELLITE_INFO,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
data=ZEROCONF_DISCOVERY,
|
||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.ABORT
|
||||
assert entry.unique_id == "test_zeroconf_name._wyoming._tcp.local._Test Satellite"
|
||||
|
Loading…
x
Reference in New Issue
Block a user