Add confirm step to thread zeroconf flow (#88869)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/88979/head
Erik Montnemery 2023-02-28 10:23:36 +01:00 committed by Paulus Schoutsen
parent 72c0526d87
commit 2112c66804
3 changed files with 49 additions and 8 deletions

View File

@ -1,7 +1,9 @@
"""Config flow for the Thread integration."""
from __future__ import annotations
from homeassistant.components import zeroconf
from typing import Any
from homeassistant.components import onboarding, zeroconf
from homeassistant.config_entries import ConfigFlow
from homeassistant.data_entry_flow import FlowResult
@ -32,4 +34,12 @@ class ThreadConfigFlow(ConfigFlow, domain=DOMAIN):
) -> FlowResult:
"""Set up because the user has border routers."""
await self._async_handle_discovery_without_unique_id()
return self.async_create_entry(title="Thread", data={})
return await self.async_step_confirm()
async def async_step_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Confirm the setup."""
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
return self.async_create_entry(title="Thread", data={})
return self.async_show_form(step_id="confirm")

View File

@ -0,0 +1,9 @@
{
"config": {
"step": {
"confirm": {
"description": "[%key:common::config_flow::description::confirm_setup%]"
}
}
}
}

View File

@ -103,14 +103,18 @@ async def test_user(hass: HomeAssistant) -> None:
async def test_zeroconf(hass: HomeAssistant) -> None:
"""Test the zeroconf flow."""
result = await hass.config_entries.flow.async_init(
thread.DOMAIN, context={"source": "zeroconf"}, data=TEST_ZEROCONF_RECORD
)
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
assert result["step_id"] == "confirm"
with patch(
"homeassistant.components.thread.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
thread.DOMAIN, context={"source": "zeroconf"}, data=TEST_ZEROCONF_RECORD
)
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Thread"
assert result["data"] == {}
@ -124,16 +128,34 @@ async def test_zeroconf(hass: HomeAssistant) -> None:
assert config_entry.unique_id is None
async def test_zeroconf_then_import(hass: HomeAssistant) -> None:
"""Test the import flow."""
async def test_zeroconf_setup_onboarding(hass: HomeAssistant) -> None:
"""Test we automatically finish a zeroconf flow during onboarding."""
with patch(
"homeassistant.components.onboarding.async_is_onboarded", return_value=False
), patch(
"homeassistant.components.thread.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
thread.DOMAIN, context={"source": "zeroconf"}, data=TEST_ZEROCONF_RECORD
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Thread"
assert result["data"] == {}
assert result["options"] == {}
assert len(mock_setup_entry.mock_calls) == 1
async def test_zeroconf_then_import(hass: HomeAssistant) -> None:
"""Test the import flow."""
result = await hass.config_entries.flow.async_init(
thread.DOMAIN, context={"source": "zeroconf"}, data=TEST_ZEROCONF_RECORD
)
with patch(
"homeassistant.components.thread.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == FlowResultType.CREATE_ENTRY
with patch(