From 26fc57d1b371158f6a80593833fd626ab79be1e0 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 25 Jun 2019 09:54:40 -0700 Subject: [PATCH] Ignore duplicate tradfri discovery (#24759) * Ignore duplicate tradfri discovery * Update name --- .../components/tradfri/config_flow.py | 13 +++++++++++-- homeassistant/components/tradfri/strings.json | 3 ++- tests/components/tradfri/test_config_flow.py | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index bfabf4fd12a933..7cdf4b9de6c5a3 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -78,13 +78,22 @@ async def async_step_auth(self, user_input=None): 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 38c58486a6a738..868fbbed550c65 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 8fcc72dd4a585c..490f8484bbf679 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