From 14bff5a3759c1187e6a6fa2710b2e581038f8a6a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 10 Jun 2020 22:46:14 +0200 Subject: [PATCH] Abort other config flows on import (#36608) * Abort other flows on import * Add test --- homeassistant/helpers/config_entry_flow.py | 10 +++++++++- tests/helpers/test_config_entry_flow.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index 81881d943cd..43d281aa5bc 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -59,6 +59,9 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow): for flow in in_progress: self.hass.config_entries.flow.async_abort(flow["flow_id"]) + if self._async_current_entries(): + return self.async_abort(reason="single_instance_allowed") + return self.async_create_entry(title=self._title, data={}) async def async_step_discovery(self, discovery_info): @@ -76,9 +79,14 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow): async def async_step_import(self, _): """Handle a flow initialized by import.""" - if self._async_in_progress() or self._async_current_entries(): + if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") + # Cancel other flows. + in_progress = self._async_in_progress() + for flow in in_progress: + self.hass.config_entries.flow.async_abort(flow["flow_id"]) + return self.async_create_entry(title=self._title, data={}) diff --git a/tests/helpers/test_config_entry_flow.py b/tests/helpers/test_config_entry_flow.py index 7130514f47f..582dc79a310 100644 --- a/tests/helpers/test_config_entry_flow.py +++ b/tests/helpers/test_config_entry_flow.py @@ -149,6 +149,27 @@ async def test_only_one_in_progress(hass, discovery_flow_conf): assert len(hass.config_entries.flow.async_progress()) == 0 +async def test_import_abort_discovery(hass, discovery_flow_conf): + """Test import will finish and cancel discovered one.""" + mock_entity_platform(hass, "config_flow.test", None) + + # Discovery starts flow + result = await hass.config_entries.flow.async_init( + "test", context={"source": config_entries.SOURCE_DISCOVERY}, data={} + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + + # Start import flow + result = await hass.config_entries.flow.async_init( + "test", context={"source": config_entries.SOURCE_IMPORT}, data={} + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + + # Discovery flow has been aborted + assert len(hass.config_entries.flow.async_progress()) == 0 + + async def test_import_no_confirmation(hass, discovery_flow_conf): """Test import requires no confirmation to set up.""" flow = config_entries.HANDLERS["test"]()