Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions homeassistant/components/tradfri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
KEY_GATEWAY = 'tradfri_gateway'
KEY_API = 'tradfri_api'
CONF_ALLOW_TRADFRI_GROUPS = 'allow_tradfri_groups'
DEFAULT_ALLOW_TRADFRI_GROUPS = True
DEFAULT_ALLOW_TRADFRI_GROUPS = False

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Inclusive(CONF_HOST, 'gateway'): cv.string,
vol.Optional(CONF_HOST): cv.string,
vol.Optional(CONF_ALLOW_TRADFRI_GROUPS,
default=DEFAULT_ALLOW_TRADFRI_GROUPS): cv.boolean,
})
Expand Down Expand Up @@ -64,13 +64,14 @@ async def async_setup(hass, config):
))

host = conf.get(CONF_HOST)
import_groups = conf[CONF_ALLOW_TRADFRI_GROUPS]

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(
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
data={'host': host}
data={CONF_HOST: host, CONF_IMPORT_GROUPS: import_groups}
))

return True
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/tradfri/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FlowHandler(config_entries.ConfigFlow):
def __init__(self):
"""Initialize flow."""
self._host = None
self._import_groups = False

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
Expand All @@ -52,7 +53,8 @@ async def async_step_auth(self, user_input=None):

# We don't ask for import group anymore as group state
# is not reliable, don't want to show that to the user.
auth[CONF_IMPORT_GROUPS] = False
# But we still allow specifying import group via config yaml.
auth[CONF_IMPORT_GROUPS] = self._import_groups

return await self._entry_from_data(auth)

Expand Down Expand Up @@ -97,6 +99,7 @@ async def async_step_import(self, user_input):
# Happens if user has host directly in configuration.yaml
if 'key' not in user_input:
self._host = user_input['host']
self._import_groups = user_input[CONF_IMPORT_GROUPS]
return await self.async_step_auth()

try:
Expand Down
12 changes: 12 additions & 0 deletions tests/components/tradfri/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Common tradfri test fixtures."""
from unittest.mock import patch

import pytest


@pytest.fixture
def mock_gateway_info():
"""Mock get_gateway_info."""
with patch('homeassistant.components.tradfri.config_flow.'
'get_gateway_info') as mock_gateway:
yield mock_gateway
86 changes: 70 additions & 16 deletions tests/components/tradfri/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ def mock_auth():
yield mock_auth


@pytest.fixture
def mock_gateway_info():
"""Mock get_gateway_info."""
with patch('homeassistant.components.tradfri.config_flow.'
'get_gateway_info') as mock_gateway:
yield mock_gateway


@pytest.fixture
def mock_entry_setup():
"""Mock entry setup."""
Expand Down Expand Up @@ -125,7 +117,70 @@ async def test_discovery_connection(hass, mock_auth, mock_entry_setup):
}


async def test_import_connection(hass, mock_gateway_info, mock_entry_setup):
async def test_import_connection(hass, mock_auth, mock_entry_setup):
"""Test a connection via import."""
mock_auth.side_effect = lambda hass, host, code: mock_coro({
'host': host,
'gateway_id': 'bla',
'identity': 'mock-iden',
'key': 'mock-key',
})

flow = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'import'}, data={
'host': '123.123.123.123',
'import_groups': True
})

result = await hass.config_entries.flow.async_configure(flow['flow_id'], {
'security_code': 'abcd',
})

assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'bla',
'identity': 'mock-iden',
'key': 'mock-key',
'import_groups': True
}

assert len(mock_entry_setup.mock_calls) == 1


async def test_import_connection_no_groups(hass, mock_auth, mock_entry_setup):
"""Test a connection via import and no groups allowed."""
mock_auth.side_effect = lambda hass, host, code: mock_coro({
'host': host,
'gateway_id': 'bla',
'identity': 'mock-iden',
'key': 'mock-key',
})

flow = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'import'}, data={
'host': '123.123.123.123',
'import_groups': False
})

result = await hass.config_entries.flow.async_configure(flow['flow_id'], {
'security_code': 'abcd',
})

assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'bla',
'identity': 'mock-iden',
'key': 'mock-key',
'import_groups': False
}

assert len(mock_entry_setup.mock_calls) == 1


async def test_import_connection_legacy(hass, mock_gateway_info,
mock_entry_setup):
"""Test a connection via import."""
mock_gateway_info.side_effect = \
lambda hass, host, identity, key: mock_coro({
Expand All @@ -138,7 +193,6 @@ async def test_import_connection(hass, mock_gateway_info, mock_entry_setup):
result = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'import'}, data={
'host': '123.123.123.123',
'identity': 'mock-iden',
'key': 'mock-key',
'import_groups': True
})
Expand All @@ -147,7 +201,7 @@ async def test_import_connection(hass, mock_gateway_info, mock_entry_setup):
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'mock-gateway',
'identity': 'mock-iden',
'identity': 'homeassistant',
'key': 'mock-key',
'import_groups': True
}
Expand All @@ -156,9 +210,9 @@ async def test_import_connection(hass, mock_gateway_info, mock_entry_setup):
assert len(mock_entry_setup.mock_calls) == 1


async def test_import_connection_legacy(hass, mock_gateway_info,
mock_entry_setup):
"""Test a connection via import."""
async def test_import_connection_legacy_no_groups(
hass, mock_gateway_info, mock_entry_setup):
"""Test a connection via legacy import and no groups allowed."""
mock_gateway_info.side_effect = \
lambda hass, host, identity, key: mock_coro({
'host': host,
Expand All @@ -171,7 +225,7 @@ async def test_import_connection_legacy(hass, mock_gateway_info,
'tradfri', context={'source': 'import'}, data={
'host': '123.123.123.123',
'key': 'mock-key',
'import_groups': True
'import_groups': False
})

assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Expand All @@ -180,7 +234,7 @@ async def test_import_connection_legacy(hass, mock_gateway_info,
'gateway_id': 'mock-gateway',
'identity': 'homeassistant',
'key': 'mock-key',
'import_groups': True
'import_groups': False
}

assert len(mock_gateway_info.mock_calls) == 1
Expand Down
2 changes: 1 addition & 1 deletion tests/components/tradfri/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def test_config_json_host_not_imported(hass):
assert len(mock_init.mock_calls) == 0


async def test_config_json_host_imported(hass):
async def test_config_json_host_imported(hass, mock_gateway_info):
"""Test that we import a configured host."""
with patch('homeassistant.components.tradfri.load_json',
return_value={'mock-host': {'key': 'some-info'}}):
Expand Down