diff --git a/homeassistant/components/tradfri/__init__.py b/homeassistant/components/tradfri/__init__.py index 079381f8b45..6e91ab338a3 100644 --- a/homeassistant/components/tradfri/__init__.py +++ b/homeassistant/components/tradfri/__init__.py @@ -44,10 +44,16 @@ async def async_setup(hass, config): if conf is None: return True - known_hosts = await hass.async_add_executor_job( + configured_hosts = [entry.data['host'] for entry in + hass.config_entries.async_entries(DOMAIN)] + + legacy_hosts = await hass.async_add_executor_job( load_json, hass.config.path(CONFIG_FILE)) - for host, info in known_hosts.items(): + for host, info in legacy_hosts.items(): + if host in configured_hosts: + continue + info[CONF_HOST] = host info[CONF_IMPORT_GROUPS] = conf[CONF_ALLOW_TRADFRI_GROUPS] @@ -58,7 +64,7 @@ async def async_setup(hass, config): host = conf.get(CONF_HOST) - if host is None or host in known_hosts: + if host is None or host in configured_hosts or host in legacy_hosts: return True hass.async_create_task(hass.config_entries.flow.async_init( diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index 8d8f9af79e6..29aa768dbb5 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -77,6 +77,12 @@ class FlowHandler(config_entries.ConfigFlow): async def async_step_discovery(self, user_input): """Handle discovery.""" + for entry in self._async_current_entries(): + if entry.data[CONF_HOST] == user_input['host']: + return self.async_abort( + reason='already_configured' + ) + self._host = user_input['host'] return await self.async_step_auth() diff --git a/tests/components/tradfri/test_config_flow.py b/tests/components/tradfri/test_config_flow.py index 580e9580d76..99566356f61 100644 --- a/tests/components/tradfri/test_config_flow.py +++ b/tests/components/tradfri/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant import data_entry_flow from homeassistant.components.tradfri import config_flow -from tests.common import mock_coro +from tests.common import mock_coro, MockConfigEntry @pytest.fixture @@ -185,3 +185,35 @@ async def test_import_connection_legacy(hass, mock_gateway_info, assert len(mock_gateway_info.mock_calls) == 1 assert len(mock_entry_setup.mock_calls) == 1 + + +async def test_discovery_duplicate_aborted(hass): + """Test a duplicate discovery host is ignored.""" + MockConfigEntry( + domain='tradfri', + data={'host': 'some-host'} + ).add_to_hass(hass) + + flow = await hass.config_entries.flow.async_init( + 'tradfri', context={'source': 'discovery'}, data={ + 'host': 'some-host' + }) + + assert flow['type'] == data_entry_flow.RESULT_TYPE_ABORT + assert flow['reason'] == 'already_configured' + + +async def test_import_duplicate_aborted(hass): + """Test a duplicate discovery host is ignored.""" + MockConfigEntry( + domain='tradfri', + data={'host': 'some-host'} + ).add_to_hass(hass) + + flow = await hass.config_entries.flow.async_init( + 'tradfri', context={'source': 'import'}, data={ + 'host': 'some-host' + }) + + assert flow['type'] == data_entry_flow.RESULT_TYPE_ABORT + assert flow['reason'] == 'already_configured' diff --git a/tests/components/tradfri/test_init.py b/tests/components/tradfri/test_init.py new file mode 100644 index 00000000000..4527e87f605 --- /dev/null +++ b/tests/components/tradfri/test_init.py @@ -0,0 +1,72 @@ +"""Tests for Tradfri setup.""" +from unittest.mock import patch + +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + + +async def test_config_yaml_host_not_imported(hass): + """Test that we don't import a configured host.""" + MockConfigEntry( + domain='tradfri', + data={'host': 'mock-host'} + ).add_to_hass(hass) + + with patch('homeassistant.components.tradfri.load_json', + return_value={}), \ + patch.object(hass.config_entries.flow, 'async_init') as mock_init: + assert await async_setup_component(hass, 'tradfri', { + 'tradfri': { + 'host': 'mock-host' + } + }) + + assert len(mock_init.mock_calls) == 0 + + +async def test_config_yaml_host_imported(hass): + """Test that we import a configured host.""" + with patch('homeassistant.components.tradfri.load_json', + return_value={}): + assert await async_setup_component(hass, 'tradfri', { + 'tradfri': { + 'host': 'mock-host' + } + }) + + progress = hass.config_entries.flow.async_progress() + assert len(progress) == 1 + assert progress[0]['handler'] == 'tradfri' + assert progress[0]['context'] == {'source': 'import'} + + +async def test_config_json_host_not_imported(hass): + """Test that we don't import a configured host.""" + MockConfigEntry( + domain='tradfri', + data={'host': 'mock-host'} + ).add_to_hass(hass) + + with patch('homeassistant.components.tradfri.load_json', + return_value={'mock-host': {'key': 'some-info'}}), \ + patch.object(hass.config_entries.flow, 'async_init') as mock_init: + assert await async_setup_component(hass, 'tradfri', { + 'tradfri': {} + }) + + assert len(mock_init.mock_calls) == 0 + + +async def test_config_json_host_imported(hass): + """Test that we import a configured host.""" + with patch('homeassistant.components.tradfri.load_json', + return_value={'mock-host': {'key': 'some-info'}}): + assert await async_setup_component(hass, 'tradfri', { + 'tradfri': {} + }) + + progress = hass.config_entries.flow.async_progress() + assert len(progress) == 1 + assert progress[0]['handler'] == 'tradfri' + assert progress[0]['context'] == {'source': 'import'}