diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index bfabf4fd12a9..7cdf4b9de6c5 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -78,13 +78,22 @@ class FlowHandler(config_entries.ConfigFlow): async def async_step_zeroconf(self, user_input): """Handle zeroconf discovery.""" + host = user_input['host'] + + # pylint: disable=unsupported-assignment-operation + self.context['host'] = host + + if any(host == flow['context']['host'] + for flow in self._async_in_progress()): + return self.async_abort(reason='already_in_progress') + for entry in self._async_current_entries(): - if entry.data[CONF_HOST] == user_input['host']: + if entry.data[CONF_HOST] == host: return self.async_abort( reason='already_configured' ) - self._host = user_input['host'] + self._host = host return await self.async_step_auth() async_step_homekit = async_step_zeroconf diff --git a/homeassistant/components/tradfri/strings.json b/homeassistant/components/tradfri/strings.json index 38c58486a6a7..868fbbed550c 100644 --- a/homeassistant/components/tradfri/strings.json +++ b/homeassistant/components/tradfri/strings.json @@ -17,7 +17,8 @@ "timeout": "Timeout validating the code." }, "abort": { - "already_configured": "Bridge is already configured" + "already_configured": "Bridge is already configured.", + "already_in_progress": "Bridge configuration is already in progress." } } } diff --git a/tests/components/tradfri/test_config_flow.py b/tests/components/tradfri/test_config_flow.py index 8fcc72dd4a58..490f8484bbf6 100644 --- a/tests/components/tradfri/test_config_flow.py +++ b/tests/components/tradfri/test_config_flow.py @@ -258,7 +258,7 @@ async def test_discovery_duplicate_aborted(hass): async def test_import_duplicate_aborted(hass): - """Test a duplicate discovery host is ignored.""" + """Test a duplicate import host is ignored.""" MockConfigEntry( domain='tradfri', data={'host': 'some-host'} @@ -271,3 +271,20 @@ async def test_import_duplicate_aborted(hass): assert flow['type'] == data_entry_flow.RESULT_TYPE_ABORT assert flow['reason'] == 'already_configured' + + +async def test_duplicate_discovery(hass, mock_auth, mock_entry_setup): + """Test a duplicate discovery in progress is ignored.""" + result = await hass.config_entries.flow.async_init( + 'tradfri', context={'source': 'zeroconf'}, data={ + 'host': '123.123.123.123' + }) + + assert result['type'] == data_entry_flow.RESULT_TYPE_FORM + + result2 = await hass.config_entries.flow.async_init( + 'tradfri', context={'source': 'zeroconf'}, data={ + 'host': '123.123.123.123' + }) + + assert result2['type'] == data_entry_flow.RESULT_TYPE_ABORT