From a8a033681f4fd3d8d1d50e9f7e6a93c032fd0f8c Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 22 Jun 2022 22:37:49 +0200 Subject: [PATCH] Automatically onboard DiscoveryFlows (#73841) --- homeassistant/helpers/config_entry_flow.py | 4 ++-- tests/helpers/test_config_entry_flow.py | 24 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index fddc5c82725..1190e947eba 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -6,7 +6,7 @@ import logging from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union, cast from homeassistant import config_entries -from homeassistant.components import dhcp, mqtt, ssdp, zeroconf +from homeassistant.components import dhcp, mqtt, onboarding, ssdp, zeroconf from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult @@ -52,7 +52,7 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow, Generic[_R]): self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Confirm setup.""" - if user_input is None: + if user_input is None and onboarding.async_is_onboarded(self.hass): self._set_confirm_only() return self.async_show_form(step_id="confirm") diff --git a/tests/helpers/test_config_entry_flow.py b/tests/helpers/test_config_entry_flow.py index 78b28a8ef10..979aa8bf088 100644 --- a/tests/helpers/test_config_entry_flow.py +++ b/tests/helpers/test_config_entry_flow.py @@ -139,6 +139,30 @@ async def test_discovery_confirmation(hass, discovery_flow_conf, source): assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY +@pytest.mark.parametrize( + "source", + [ + config_entries.SOURCE_DISCOVERY, + config_entries.SOURCE_MQTT, + config_entries.SOURCE_SSDP, + config_entries.SOURCE_ZEROCONF, + config_entries.SOURCE_DHCP, + ], +) +async def test_discovery_during_onboarding(hass, discovery_flow_conf, source): + """Test we create config entry via discovery during onboarding.""" + flow = config_entries.HANDLERS["test"]() + flow.hass = hass + flow.context = {"source": source} + + with patch( + "homeassistant.components.onboarding.async_is_onboarded", return_value=False + ): + result = await getattr(flow, f"async_step_{source}")({}) + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + + async def test_multiple_discoveries(hass, discovery_flow_conf): """Test we only create one instance for multiple discoveries.""" mock_entity_platform(hass, "config_flow.test", None)