diff --git a/homeassistant/components/thread/config_flow.py b/homeassistant/components/thread/config_flow.py index 070378b3429..b294dfa51e7 100644 --- a/homeassistant/components/thread/config_flow.py +++ b/homeassistant/components/thread/config_flow.py @@ -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") diff --git a/homeassistant/components/thread/strings.json b/homeassistant/components/thread/strings.json new file mode 100644 index 00000000000..0a9cf0004bc --- /dev/null +++ b/homeassistant/components/thread/strings.json @@ -0,0 +1,9 @@ +{ + "config": { + "step": { + "confirm": { + "description": "[%key:common::config_flow::description::confirm_setup%]" + } + } + } +} diff --git a/tests/components/thread/test_config_flow.py b/tests/components/thread/test_config_flow.py index a514760212b..7ff096795ca 100644 --- a/tests/components/thread/test_config_flow.py +++ b/tests/components/thread/test_config_flow.py @@ -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(