Convert Hue to always use config entries (#13034)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-03-29 20:15:40 -07:00
1 parent 1ae8b6ee08
commit 184f2be83e
14 files changed
+914 -527

No files matched your search

-17
View File
@@ -1,17 +0,0 @@
"""Fixtures for Hue tests."""
from unittest.mock import patch
import pytest
from tests.common import mock_coro_func
@pytest.fixture
def mock_bridge():
"""Mock the HueBridge from initializing."""
with patch('homeassistant.components.hue._find_username_from_config',
return_value=None), \
patch('homeassistant.components.hue.HueBridge') as mock_bridge:
mock_bridge().async_setup = mock_coro_func()
mock_bridge.reset_mock()
yield mock_bridge
+47 -89
View File
@@ -1,99 +1,57 @@
"""Test Hue bridge."""
import asyncio
from unittest.mock import Mock, patch
import aiohue
import pytest
from homeassistant.components import hue
from homeassistant.components.hue import bridge, errors
from tests.common import mock_coro
class MockBridge(hue.HueBridge):
"""Class that sets default for constructor."""
async def test_bridge_setup():
"""Test a successful setup."""
hass = Mock()
entry = Mock()
api = Mock()
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
def __init__(self, hass, host='1.2.3.4', filename='mock-bridge.conf',
username=None, **kwargs):
"""Initialize a mock bridge."""
super().__init__(host, hass, filename, username, **kwargs)
with patch.object(bridge, 'get_bridge', return_value=mock_coro(api)):
assert await hue_bridge.async_setup() is True
@pytest.fixture
def mock_request():
"""Mock configurator.async_request_config."""
with patch('homeassistant.components.configurator.'
'async_request_config') as mock_request:
yield mock_request
async def test_setup_request_config_button_not_pressed(hass, mock_request):
"""Test we request config if link button has not been pressed."""
with patch('aiohue.Bridge.create_user',
side_effect=aiohue.LinkButtonNotPressed):
await MockBridge(hass).async_setup()
assert len(mock_request.mock_calls) == 1
async def test_setup_request_config_invalid_username(hass, mock_request):
"""Test we request config if username is no longer whitelisted."""
with patch('aiohue.Bridge.create_user',
side_effect=aiohue.Unauthorized):
await MockBridge(hass).async_setup()
assert len(mock_request.mock_calls) == 1
async def test_setup_timeout(hass, mock_request):
"""Test we give up when there is a timeout."""
with patch('aiohue.Bridge.create_user',
side_effect=asyncio.TimeoutError):
await MockBridge(hass).async_setup()
assert len(mock_request.mock_calls) == 0
async def test_only_create_no_username(hass):
"""."""
with patch('aiohue.Bridge.create_user') as mock_create, \
patch('aiohue.Bridge.initialize') as mock_init:
await MockBridge(hass, username='bla').async_setup()
assert len(mock_create.mock_calls) == 0
assert len(mock_init.mock_calls) == 1
async def test_configurator_callback(hass, mock_request):
"""."""
hass.data[hue.DOMAIN] = {}
with patch('aiohue.Bridge.create_user',
side_effect=aiohue.LinkButtonNotPressed):
await MockBridge(hass).async_setup()
assert len(mock_request.mock_calls) == 1
callback = mock_request.mock_calls[0][1][2]
mock_init = Mock(return_value=mock_coro())
mock_create = Mock(return_value=mock_coro())
with patch('aiohue.Bridge') as mock_bridge, \
patch('homeassistant.helpers.discovery.async_load_platform',
return_value=mock_coro()) as mock_load_platform, \
patch('homeassistant.components.hue.save_json') as mock_save:
inst = mock_bridge()
inst.username = 'mock-user'
inst.create_user = mock_create
inst.initialize = mock_init
await callback(None)
assert len(mock_create.mock_calls) == 1
assert len(mock_init.mock_calls) == 1
assert len(mock_save.mock_calls) == 1
assert mock_save.mock_calls[0][1][1] == {
'1.2.3.4': {
'username': 'mock-user'
}
assert hue_bridge.api is api
assert len(hass.helpers.discovery.async_load_platform.mock_calls) == 1
assert hass.helpers.discovery.async_load_platform.mock_calls[0][1][2] == {
'host': '1.2.3.4'
}
assert len(mock_load_platform.mock_calls) == 1
async def test_bridge_setup_invalid_username():
"""Test we start config flow if username is no longer whitelisted."""
hass = Mock()
entry = Mock()
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
with patch.object(bridge, 'get_bridge',
side_effect=errors.AuthenticationRequired):
assert await hue_bridge.async_setup() is False
assert len(hass.async_add_job.mock_calls) == 1
assert len(hass.config_entries.flow.async_init.mock_calls) == 1
assert hass.config_entries.flow.async_init.mock_calls[0][2]['data'] == {
'host': '1.2.3.4'
}
async def test_bridge_setup_timeout(hass):
"""Test we retry to connect if we cannot connect."""
hass = Mock()
entry = Mock()
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
with patch.object(bridge, 'get_bridge', side_effect=errors.CannotConnect):
assert await hue_bridge.async_setup() is False
assert len(hass.helpers.event.async_call_later.mock_calls) == 1
# Assert we are going to wait 2 seconds
assert hass.helpers.event.async_call_later.mock_calls[0][1][0] == 2
+192 -21
View File
@@ -1,28 +1,29 @@
"""Tests for Philips Hue config flow."""
import asyncio
from unittest.mock import patch
from unittest.mock import Mock, patch
import aiohue
import pytest
import voluptuous as vol
from homeassistant.components import hue
from homeassistant.components.hue import config_flow, const, errors
from tests.common import MockConfigEntry, mock_coro
async def test_flow_works(hass, aioclient_mock):
"""Test config flow ."""
aioclient_mock.get(hue.API_NUPNP, json=[
aioclient_mock.get(const.API_NUPNP, json=[
{'internalipaddress': '1.2.3.4', 'id': 'bla'}
])
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
await flow.async_step_init()
with patch('aiohue.Bridge') as mock_bridge:
def mock_constructor(host, websession):
def mock_constructor(host, websession, username=None):
"""Fake the bridge constructor."""
mock_bridge.host = host
return mock_bridge
@@ -50,8 +51,8 @@ async def test_flow_works(hass, aioclient_mock):
async def test_flow_no_discovered_bridges(hass, aioclient_mock):
"""Test config flow discovers no bridges."""
aioclient_mock.get(hue.API_NUPNP, json=[])
flow = hue.HueFlowHandler()
aioclient_mock.get(const.API_NUPNP, json=[])
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
@@ -60,13 +61,13 @@ async def test_flow_no_discovered_bridges(hass, aioclient_mock):
async def test_flow_all_discovered_bridges_exist(hass, aioclient_mock):
"""Test config flow discovers only already configured bridges."""
aioclient_mock.get(hue.API_NUPNP, json=[
aioclient_mock.get(const.API_NUPNP, json=[
{'internalipaddress': '1.2.3.4', 'id': 'bla'}
])
MockConfigEntry(domain='hue', data={
'host': '1.2.3.4'
}).add_to_hass(hass)
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
@@ -75,10 +76,10 @@ async def test_flow_all_discovered_bridges_exist(hass, aioclient_mock):
async def test_flow_one_bridge_discovered(hass, aioclient_mock):
"""Test config flow discovers one bridge."""
aioclient_mock.get(hue.API_NUPNP, json=[
aioclient_mock.get(const.API_NUPNP, json=[
{'internalipaddress': '1.2.3.4', 'id': 'bla'}
])
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
@@ -88,11 +89,11 @@ async def test_flow_one_bridge_discovered(hass, aioclient_mock):
async def test_flow_two_bridges_discovered(hass, aioclient_mock):
"""Test config flow discovers two bridges."""
aioclient_mock.get(hue.API_NUPNP, json=[
aioclient_mock.get(const.API_NUPNP, json=[
{'internalipaddress': '1.2.3.4', 'id': 'bla'},
{'internalipaddress': '5.6.7.8', 'id': 'beer'}
])
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
@@ -108,14 +109,14 @@ async def test_flow_two_bridges_discovered(hass, aioclient_mock):
async def test_flow_two_bridges_discovered_one_new(hass, aioclient_mock):
"""Test config flow discovers two bridges."""
aioclient_mock.get(hue.API_NUPNP, json=[
aioclient_mock.get(const.API_NUPNP, json=[
{'internalipaddress': '1.2.3.4', 'id': 'bla'},
{'internalipaddress': '5.6.7.8', 'id': 'beer'}
])
MockConfigEntry(domain='hue', data={
'host': '1.2.3.4'
}).add_to_hass(hass)
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
@@ -126,7 +127,7 @@ async def test_flow_two_bridges_discovered_one_new(hass, aioclient_mock):
async def test_flow_timeout_discovery(hass):
"""Test config flow ."""
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch('aiohue.discovery.discover_nupnp',
@@ -138,7 +139,7 @@ async def test_flow_timeout_discovery(hass):
async def test_flow_link_timeout(hass):
"""Test config flow ."""
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch('aiohue.Bridge.create_user',
@@ -148,13 +149,13 @@ async def test_flow_link_timeout(hass):
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert result['errors'] == {
'base': 'register_failed'
'base': 'linking'
}
async def test_flow_link_button_not_pressed(hass):
"""Test config flow ."""
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch('aiohue.Bridge.create_user',
@@ -170,7 +171,7 @@ async def test_flow_link_button_not_pressed(hass):
async def test_flow_link_unknown_host(hass):
"""Test config flow ."""
flow = hue.HueFlowHandler()
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch('aiohue.Bridge.create_user',
@@ -180,5 +181,175 @@ async def test_flow_link_unknown_host(hass):
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert result['errors'] == {
'base': 'register_failed'
'base': 'linking'
}
async def test_bridge_discovery(hass):
"""Test a bridge being discovered."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch.object(config_flow, 'get_bridge',
side_effect=errors.AuthenticationRequired):
result = await flow.async_step_discovery({
'host': '0.0.0.0',
'serial': '1234'
})
assert result['type'] == 'form'
assert result['step_id'] == 'link'
async def test_bridge_discovery_emulated_hue(hass):
"""Test if discovery info is from an emulated hue instance."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_discovery({
'name': 'HASS Bridge',
'host': '0.0.0.0',
'serial': '1234'
})
assert result['type'] == 'abort'
async def test_bridge_discovery_already_configured(hass):
"""Test if a discovered bridge has already been configured."""
MockConfigEntry(domain='hue', data={
'host': '0.0.0.0'
}).add_to_hass(hass)
flow = config_flow.HueFlowHandler()
flow.hass = hass
result = await flow.async_step_discovery({
'host': '0.0.0.0',
'serial': '1234'
})
assert result['type'] == 'abort'
async def test_import_with_existing_config(hass):
"""Test importing a host with an existing config file."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
bridge = Mock()
bridge.username = 'username-abc'
bridge.config.bridgeid = 'bridge-id-1234'
bridge.config.name = 'Mock Bridge'
bridge.host = '0.0.0.0'
with patch.object(config_flow, '_find_username_from_config',
return_value='mock-user'), \
patch.object(config_flow, 'get_bridge',
return_value=mock_coro(bridge)):
result = await flow.async_step_import({
'host': '0.0.0.0',
'path': 'bla.conf'
})
assert result['type'] == 'create_entry'
assert result['title'] == 'Mock Bridge'
assert result['data'] == {
'host': '0.0.0.0',
'bridge_id': 'bridge-id-1234',
'username': 'username-abc'
}
async def test_import_with_no_config(hass):
"""Test importing a host without an existing config file."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch.object(config_flow, 'get_bridge',
side_effect=errors.AuthenticationRequired):
result = await flow.async_step_import({
'host': '0.0.0.0',
})
assert result['type'] == 'form'
assert result['step_id'] == 'link'
async def test_import_with_existing_but_invalid_config(hass):
"""Test importing a host with a config file with invalid username."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch.object(config_flow, '_find_username_from_config',
return_value='mock-user'), \
patch.object(config_flow, 'get_bridge',
side_effect=errors.AuthenticationRequired):
result = await flow.async_step_import({
'host': '0.0.0.0',
'path': 'bla.conf'
})
assert result['type'] == 'form'
assert result['step_id'] == 'link'
async def test_import_cannot_connect(hass):
"""Test importing a host that we cannot conncet to."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
with patch.object(config_flow, 'get_bridge',
side_effect=errors.CannotConnect):
result = await flow.async_step_import({
'host': '0.0.0.0',
})
assert result['type'] == 'abort'
assert result['reason'] == 'cannot_connect'
async def test_creating_entry_removes_entries_for_same_host_or_bridge(hass):
"""Test that we clean up entries for same host and bridge.
An IP can only hold a single bridge and a single bridge can only be
accessible via a single IP. So when we create a new entry, we'll remove
all existing entries that either have same IP or same bridge_id.
"""
MockConfigEntry(domain='hue', data={
'host': '0.0.0.0',
'bridge_id': 'id-1234'
}).add_to_hass(hass)
MockConfigEntry(domain='hue', data={
'host': '1.2.3.4',
'bridge_id': 'id-1234'
}).add_to_hass(hass)
assert len(hass.config_entries.async_entries('hue')) == 2
flow = config_flow.HueFlowHandler()
flow.hass = hass
bridge = Mock()
bridge.username = 'username-abc'
bridge.config.bridgeid = 'id-1234'
bridge.config.name = 'Mock Bridge'
bridge.host = '0.0.0.0'
with patch.object(config_flow, 'get_bridge',
return_value=mock_coro(bridge)):
result = await flow.async_step_import({
'host': '0.0.0.0',
})
assert result['type'] == 'create_entry'
assert result['title'] == 'Mock Bridge'
assert result['data'] == {
'host': '0.0.0.0',
'bridge_id': 'id-1234',
'username': 'username-abc'
}
# We did not process the result of this entry but already removed the old
# ones. So we should have 0 entries.
assert len(hass.config_entries.async_entries('hue')) == 0
+169
View File
@@ -0,0 +1,169 @@
"""Test Hue setup process."""
from unittest.mock import patch
from homeassistant.setup import async_setup_component
from homeassistant.components import hue
from tests.common import mock_coro, MockConfigEntry
async def test_setup_with_no_config(hass):
"""Test that we do not discover anything or try to setup a bridge."""
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hue, 'configured_hosts', return_value=[]):
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
# No flows started
assert len(mock_config_entries.flow.mock_calls) == 0
# No configs stored
assert hass.data[hue.DOMAIN] == {}
async def test_setup_with_discovery_no_known_auth(hass, aioclient_mock):
"""Test discovering a bridge and not having known auth."""
aioclient_mock.get(hue.API_NUPNP, json=[
{
'internalipaddress': '0.0.0.0',
'id': 'abcd1234'
}
])
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hue, 'configured_hosts', return_value=[]):
mock_config_entries.flow.async_init.return_value = mock_coro()
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {}
}) is True
# Flow started for discovered bridge
assert len(mock_config_entries.flow.mock_calls) == 1
assert mock_config_entries.flow.mock_calls[0][2]['data'] == {
'host': '0.0.0.0',
'path': '.hue_abcd1234.conf',
}
# Config stored for domain.
assert hass.data[hue.DOMAIN] == {
'0.0.0.0': {
hue.CONF_HOST: '0.0.0.0',
hue.CONF_FILENAME: '.hue_abcd1234.conf',
hue.CONF_ALLOW_HUE_GROUPS: hue.DEFAULT_ALLOW_HUE_GROUPS,
hue.CONF_ALLOW_UNREACHABLE: hue.DEFAULT_ALLOW_UNREACHABLE,
}
}
async def test_setup_with_discovery_known_auth(hass, aioclient_mock):
"""Test we don't do anything if we discover already configured hub."""
aioclient_mock.get(hue.API_NUPNP, json=[
{
'internalipaddress': '0.0.0.0',
'id': 'abcd1234'
}
])
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hue, 'configured_hosts', return_value=['0.0.0.0']):
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {}
}) is True
# Flow started for discovered bridge
assert len(mock_config_entries.flow.mock_calls) == 0
# Config stored for domain.
assert hass.data[hue.DOMAIN] == {}
async def test_setup_defined_hosts_known_auth(hass):
"""Test we don't initiate a config entry if config bridge is known."""
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hue, 'configured_hosts', return_value=['0.0.0.0']):
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: '0.0.0.0',
hue.CONF_FILENAME: 'bla.conf',
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True
}
}
}) is True
# Flow started for discovered bridge
assert len(mock_config_entries.flow.mock_calls) == 0
# Config stored for domain.
assert hass.data[hue.DOMAIN] == {
'0.0.0.0': {
hue.CONF_HOST: '0.0.0.0',
hue.CONF_FILENAME: 'bla.conf',
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True
}
}
async def test_setup_defined_hosts_no_known_auth(hass):
"""Test we initiate config entry if config bridge is not known."""
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hue, 'configured_hosts', return_value=[]):
mock_config_entries.flow.async_init.return_value = mock_coro()
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: '0.0.0.0',
hue.CONF_FILENAME: 'bla.conf',
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True
}
}
}) is True
# Flow started for discovered bridge
assert len(mock_config_entries.flow.mock_calls) == 1
assert mock_config_entries.flow.mock_calls[0][2]['data'] == {
'host': '0.0.0.0',
'path': 'bla.conf',
}
# Config stored for domain.
assert hass.data[hue.DOMAIN] == {
'0.0.0.0': {
hue.CONF_HOST: '0.0.0.0',
hue.CONF_FILENAME: 'bla.conf',
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True
}
}
async def test_config_passed_to_config_entry(hass):
"""Test that configured options for a host are loaded via config entry."""
entry = MockConfigEntry(domain=hue.DOMAIN, data={
'host': '0.0.0.0',
})
entry.add_to_hass(hass)
with patch.object(hue, 'HueBridge') as mock_bridge:
mock_bridge.return_value.async_setup.return_value = mock_coro(True)
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: '0.0.0.0',
hue.CONF_FILENAME: 'bla.conf',
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True
}
}
}) is True
assert len(mock_bridge.mock_calls) == 2
p_hass, p_entry, p_allow_unreachable, p_allow_groups = \
mock_bridge.mock_calls[0][1]
assert p_hass is hass
assert p_entry is entry
assert p_allow_unreachable is True
assert p_allow_groups is False
-70
View File
@@ -1,70 +0,0 @@
"""Test Hue setup process."""
from homeassistant.setup import async_setup_component
from homeassistant.components import hue
from homeassistant.components.discovery import SERVICE_HUE
async def test_setup_with_multiple_hosts(hass, mock_bridge):
"""Multiple hosts specified in the config file."""
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {
hue.CONF_BRIDGES: [
{hue.CONF_HOST: '127.0.0.1'},
{hue.CONF_HOST: '192.168.1.10'},
]
}
})
assert len(mock_bridge.mock_calls) == 2
hosts = sorted(mock_call[1][0] for mock_call in mock_bridge.mock_calls)
assert hosts == ['127.0.0.1', '192.168.1.10']
async def test_bridge_discovered(hass, mock_bridge):
"""Bridge discovery."""
assert await async_setup_component(hass, hue.DOMAIN, {})
await hass.helpers.discovery.async_discover(SERVICE_HUE, {
'host': '192.168.1.10',
'serial': '1234567',
})
await hass.async_block_till_done()
assert len(mock_bridge.mock_calls) == 1
assert mock_bridge.mock_calls[0][1][0] == '192.168.1.10'
async def test_bridge_configure_and_discovered(hass, mock_bridge):
"""Bridge is in the config file, then we discover it."""
assert await async_setup_component(hass, hue.DOMAIN, {
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: '192.168.1.10'
}
}
})
assert len(mock_bridge.mock_calls) == 1
assert mock_bridge.mock_calls[0][1][0] == '192.168.1.10'
hass.data[hue.DOMAIN] = {'192.168.1.10': {}}
mock_bridge.reset_mock()
await hass.helpers.discovery.async_discover(SERVICE_HUE, {
'host': '192.168.1.10',
'serial': '1234567',
})
await hass.async_block_till_done()
assert len(mock_bridge.mock_calls) == 0
async def test_setup_no_host(hass, aioclient_mock):
"""Check we call discovery if domain specified but no bridges."""
aioclient_mock.get(hue.API_NUPNP, json=[])
result = await async_setup_component(
hass, hue.DOMAIN, {hue.DOMAIN: {}})
assert result
assert len(aioclient_mock.mock_calls) == 1
+28 -6
View File
@@ -5,6 +5,7 @@ from unittest.mock import patch, MagicMock
import pytest
from homeassistant import config_entries
from homeassistant.bootstrap import async_setup_component
from homeassistant.components import discovery
from homeassistant.util.dt import utcnow
@@ -44,13 +45,12 @@ def netdisco_mock():
yield
@asyncio.coroutine
def mock_discovery(hass, discoveries, config=BASE_CONFIG):
async def mock_discovery(hass, discoveries, config=BASE_CONFIG):
"""Helper to mock discoveries."""
result = yield from async_setup_component(hass, 'discovery', config)
result = await async_setup_component(hass, 'discovery', config)
assert result
yield from hass.async_start()
await hass.async_start()
with patch.object(discovery, '_discover', discoveries), \
patch('homeassistant.components.discovery.async_discover',
@@ -59,8 +59,8 @@ def mock_discovery(hass, discoveries, config=BASE_CONFIG):
return_value=mock_coro()) as mock_platform:
async_fire_time_changed(hass, utcnow())
# Work around an issue where our loop.call_soon not get caught
yield from hass.async_block_till_done()
yield from hass.async_block_till_done()
await hass.async_block_till_done()
await hass.async_block_till_done()
return mock_discover, mock_platform
@@ -154,3 +154,25 @@ def test_load_component_hassio(hass):
yield from mock_discovery(hass, discover)
assert mock_hassio.called
async def test_discover_config_flow(hass):
"""Test discovery triggering a config flow."""
discovery_info = {
'hello': 'world'
}
def discover(netdisco):
"""Fake discovery."""
return [('mock-service', discovery_info)]
with patch.dict(discovery.CONFIG_ENTRY_HANDLERS, {
'mock-service': 'mock-component'}), patch(
'homeassistant.config_entries.FlowManager.async_init') as m_init:
await mock_discovery(hass, discover)
assert len(m_init.mock_calls) == 1
args, kwargs = m_init.mock_calls[0][1:]
assert args == ('mock-component',)
assert kwargs['source'] == config_entries.SOURCE_DISCOVERY
assert kwargs['data'] == discovery_info