Allow tradfri groups for new imported entries (#17310)

* Clean up leftover config schema option

* Allow import groups via new config yaml setup

* Fix and add test

* Add a test without groups for legacy import

* Change default import groups to False

* Fix I/O in test
This commit is contained in:
Martin Hjelmare authored and Paulus Schoutsen committed 2018-10-12 14:59:16 +02:00
1 parent 05ae8f9dd4
commit 8778b707f1
5 files changed
+96 -26

No files matched your search

+12
View File
@@ -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
+75 -21
View File
@@ -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."""
@@ -125,34 +117,65 @@ 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_gateway_info.side_effect = \
lambda hass, host, identity, key: mock_coro({
'host': host,
'identity': identity,
'key': key,
'gateway_id': 'mock-gateway'
})
mock_auth.side_effect = lambda hass, host, code: mock_coro({
'host': host,
'gateway_id': 'bla',
'identity': 'mock-iden',
'key': 'mock-key',
})
result = await hass.config_entries.flow.async_init(
flow = 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
})
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': 'mock-gateway',
'gateway_id': 'bla',
'identity': 'mock-iden',
'key': 'mock-key',
'import_groups': True
}
assert len(mock_gateway_info.mock_calls) == 1
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
@@ -187,6 +210,37 @@ async def test_import_connection_legacy(hass, mock_gateway_info,
assert len(mock_entry_setup.mock_calls) == 1
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,
'identity': identity,
'key': key,
'gateway_id': 'mock-gateway'
})
result = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'import'}, data={
'host': '123.123.123.123',
'key': 'mock-key',
'import_groups': False
})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'mock-gateway',
'identity': 'homeassistant',
'key': 'mock-key',
'import_groups': False
}
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(
+1 -1
View File
@@ -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'}}):