Config flow tradfri (#16665)

* Fix comments

* Add config flow tests

* Fix Tradfri light tests

* Lint

* Remove import group from config flow

* fix stale comments
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-09-19 21:21:43 +02:00
1 parent 3160fa5de8
commit a1c524d372
13 files changed
+519 -262

No files matched your search

+16 -59
View File
@@ -5,10 +5,10 @@ from unittest.mock import Mock, MagicMock, patch, PropertyMock
import pytest
from pytradfri.device import Device, LightControl, Light
from pytradfri import RequestError
from homeassistant.components import tradfri
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
DEFAULT_TEST_FEATURES = {'can_set_dimmer': False,
@@ -199,7 +199,7 @@ def mock_gateway():
@pytest.fixture
def mock_api(mock_gateway):
"""Mock api."""
async def api(self, command):
async def api(command):
"""Mock api function."""
# Store the data for "real" command objects.
if(hasattr(command, '_data') and not isinstance(command, Mock)):
@@ -213,63 +213,20 @@ async def generate_psk(self, code):
return "mock"
async def setup_gateway(hass, mock_gateway, mock_api,
generate_psk=generate_psk,
known_hosts=None):
async def setup_gateway(hass, mock_gateway, mock_api):
"""Load the Tradfri platform with a mock gateway."""
def request_config(_, callback, description, submit_caption, fields):
"""Mock request_config."""
hass.async_add_job(callback, {'security_code': 'mock'})
if known_hosts is None:
known_hosts = {}
with patch('pytradfri.api.aiocoap_api.APIFactory.generate_psk',
generate_psk), \
patch('pytradfri.api.aiocoap_api.APIFactory.request', mock_api), \
patch('pytradfri.Gateway', return_value=mock_gateway), \
patch.object(tradfri, 'load_json', return_value=known_hosts), \
patch.object(tradfri, 'save_json'), \
patch.object(hass.components.configurator, 'request_config',
request_config):
await async_setup_component(hass, tradfri.DOMAIN,
{
tradfri.DOMAIN: {
'host': 'mock-host',
'allow_tradfri_groups': True
}
})
await hass.async_block_till_done()
async def test_setup_gateway(hass, mock_gateway, mock_api):
"""Test that the gateway can be setup without errors."""
await setup_gateway(hass, mock_gateway, mock_api)
async def test_setup_gateway_known_host(hass, mock_gateway, mock_api):
"""Test gateway setup with a known host."""
await setup_gateway(hass, mock_gateway, mock_api,
known_hosts={
'mock-host': {
'identity': 'mock',
'key': 'mock-key'
}
})
async def test_incorrect_security_code(hass, mock_gateway, mock_api):
"""Test that an error is shown if the security code is incorrect."""
async def psk_error(self, code):
"""Raise RequestError when called."""
raise RequestError
with patch.object(hass.components.configurator, 'async_notify_errors') \
as notify_error:
await setup_gateway(hass, mock_gateway, mock_api,
generate_psk=psk_error)
assert len(notify_error.mock_calls) > 0
entry = MockConfigEntry(domain=tradfri.DOMAIN, data={
'host': 'mock-host',
'identity': 'mock-identity',
'key': 'mock-key',
'import_groups': True,
'gateway_id': 'mock-gateway-id',
})
hass.data[tradfri.KEY_GATEWAY] = {entry.entry_id: mock_gateway}
hass.data[tradfri.KEY_API] = {entry.entry_id: mock_api}
await hass.config_entries.async_forward_entry_setup(
entry, 'light'
)
def mock_light(test_features={}, test_state={}, n=0):
+1
View File
@@ -0,0 +1 @@
"""Tests for the tradfri component."""
@@ -0,0 +1,156 @@
"""Test the Tradfri config flow."""
from unittest.mock import patch
import pytest
from homeassistant import data_entry_flow
from homeassistant.components.tradfri import config_flow
from tests.common import mock_coro
@pytest.fixture
def mock_auth():
"""Mock authenticate."""
with patch('homeassistant.components.tradfri.config_flow.'
'authenticate') as 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."""
with patch('homeassistant.components.tradfri.'
'async_setup_entry') as mock_setup:
mock_setup.return_value = mock_coro(True)
yield mock_setup
async def test_user_connection_successful(hass, mock_auth, mock_entry_setup):
"""Test a successful connection."""
mock_auth.side_effect = lambda hass, host, code: mock_coro({
'host': host,
'gateway_id': 'bla'
})
flow = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'user'})
result = await hass.config_entries.flow.async_configure(flow['flow_id'], {
'host': '123.123.123.123',
'security_code': 'abcd',
})
assert len(mock_entry_setup.mock_calls) == 1
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'bla',
'import_groups': False
}
async def test_user_connection_timeout(hass, mock_auth, mock_entry_setup):
"""Test a connection timeout."""
mock_auth.side_effect = config_flow.AuthError('timeout')
flow = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'user'})
result = await hass.config_entries.flow.async_configure(flow['flow_id'], {
'host': '127.0.0.1',
'security_code': 'abcd',
})
assert len(mock_entry_setup.mock_calls) == 0
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['errors'] == {
'base': 'timeout'
}
async def test_user_connection_bad_key(hass, mock_auth, mock_entry_setup):
"""Test a connection with bad key."""
mock_auth.side_effect = config_flow.AuthError('invalid_security_code')
flow = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'user'})
result = await hass.config_entries.flow.async_configure(flow['flow_id'], {
'host': '127.0.0.1',
'security_code': 'abcd',
})
assert len(mock_entry_setup.mock_calls) == 0
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['errors'] == {
'security_code': 'invalid_security_code'
}
async def test_discovery_connection(hass, mock_auth, mock_entry_setup):
"""Test a connection via discovery."""
mock_auth.side_effect = lambda hass, host, code: mock_coro({
'host': host,
'gateway_id': 'bla'
})
flow = await hass.config_entries.flow.async_init(
'tradfri', context={'source': 'discovery'}, data={
'host': '123.123.123.123'
})
result = await hass.config_entries.flow.async_configure(flow['flow_id'], {
'security_code': 'abcd',
})
assert len(mock_entry_setup.mock_calls) == 1
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'bla',
'import_groups': False
}
async def test_import_connection(hass, mock_gateway_info, 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'
})
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
})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'].data == {
'host': '123.123.123.123',
'gateway_id': 'mock-gateway',
'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