2018-06-14 19:17:54 +00:00
|
|
|
"""Tests for the Config Entry Flow helper."""
|
2018-10-23 09:14:46 +00:00
|
|
|
from unittest.mock import patch, Mock
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow, setup
|
2018-06-14 19:17:54 +00:00
|
|
|
from homeassistant.helpers import config_entry_flow
|
2019-04-15 02:07:05 +00:00
|
|
|
from tests.common import (
|
2019-05-13 08:16:55 +00:00
|
|
|
MockConfigEntry, MockModule, mock_coro, mock_integration,
|
|
|
|
mock_entity_platform)
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2018-10-23 09:14:46 +00:00
|
|
|
def discovery_flow_conf(hass):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Register a handler."""
|
|
|
|
handler_conf = {
|
|
|
|
'discovered': False,
|
|
|
|
}
|
|
|
|
|
|
|
|
async def has_discovered_devices(hass):
|
|
|
|
"""Mock if we have discovered devices."""
|
|
|
|
return handler_conf['discovered']
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS):
|
|
|
|
config_entry_flow.register_discovery_flow(
|
2018-09-17 08:12:46 +00:00
|
|
|
'test', 'Test', has_discovered_devices,
|
|
|
|
config_entries.CONN_CLASS_LOCAL_POLL)
|
2018-06-14 19:17:54 +00:00
|
|
|
yield handler_conf
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def webhook_flow_conf(hass):
|
|
|
|
"""Register a handler."""
|
|
|
|
with patch.dict(config_entries.HANDLERS):
|
|
|
|
config_entry_flow.register_webhook_flow(
|
|
|
|
'test_single', 'Test Single', {}, False)
|
|
|
|
config_entry_flow.register_webhook_flow(
|
|
|
|
'test_multiple', 'Test Multiple', {}, True)
|
|
|
|
yield {}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_single_entry_allowed(hass, discovery_flow_conf):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Test only a single entry is allowed."""
|
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
MockConfigEntry(domain='test').add_to_hass(hass)
|
2018-08-09 11:24:14 +00:00
|
|
|
result = await flow.async_step_user()
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result['reason'] == 'single_instance_allowed'
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_user_no_devices_found(hass, discovery_flow_conf):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Test if no devices found."""
|
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
2018-09-21 14:34:37 +00:00
|
|
|
flow.context = {
|
|
|
|
'source': config_entries.SOURCE_USER
|
|
|
|
}
|
|
|
|
result = await flow.async_step_confirm(user_input={})
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result['reason'] == 'no_devices_found'
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_user_has_confirmation(hass, discovery_flow_conf):
|
2018-09-21 14:34:37 +00:00
|
|
|
"""Test user requires no confirmation to setup."""
|
2018-06-14 19:17:54 +00:00
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
2018-10-23 09:14:46 +00:00
|
|
|
discovery_flow_conf['discovered'] = True
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
result = await flow.async_step_user()
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2018-09-21 14:34:37 +00:00
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_discovery_single_instance(hass, discovery_flow_conf):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Test we ask for confirmation via discovery."""
|
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
MockConfigEntry(domain='test').add_to_hass(hass)
|
|
|
|
result = await flow.async_step_discovery({})
|
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result['reason'] == 'single_instance_allowed'
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_discovery_confirmation(hass, discovery_flow_conf):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Test we ask for confirmation via discovery."""
|
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
result = await flow.async_step_discovery({})
|
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result['step_id'] == 'confirm'
|
|
|
|
|
|
|
|
result = await flow.async_step_confirm({})
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_multiple_discoveries(hass, discovery_flow_conf):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Test we only create one instance for multiple discoveries."""
|
2019-05-13 08:16:55 +00:00
|
|
|
mock_entity_platform(hass, 'config_flow.test', None)
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2018-08-09 11:24:14 +00:00
|
|
|
'test', context={'source': config_entries.SOURCE_DISCOVERY}, data={})
|
2018-06-14 19:17:54 +00:00
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Second discovery
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2018-08-09 11:24:14 +00:00
|
|
|
'test', context={'source': config_entries.SOURCE_DISCOVERY}, data={})
|
2018-06-14 19:17:54 +00:00
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_only_one_in_progress(hass, discovery_flow_conf):
|
2018-06-14 19:17:54 +00:00
|
|
|
"""Test a user initialized one will finish and cancel discovered one."""
|
2019-05-13 08:16:55 +00:00
|
|
|
mock_entity_platform(hass, 'config_flow.test', None)
|
2018-06-14 19:17:54 +00:00
|
|
|
|
|
|
|
# Discovery starts flow
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2018-08-09 11:24:14 +00:00
|
|
|
'test', context={'source': config_entries.SOURCE_DISCOVERY}, data={})
|
2018-06-14 19:17:54 +00:00
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# User starts flow
|
2018-08-13 09:27:18 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
'test', context={'source': config_entries.SOURCE_USER}, data={})
|
2018-06-14 19:17:54 +00:00
|
|
|
|
2018-09-21 14:34:37 +00:00
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Discovery flow has not been aborted
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 2
|
|
|
|
|
|
|
|
# Discovery should be aborted once user confirms
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result['flow_id'], {})
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2018-06-14 19:17:54 +00:00
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
2018-07-23 13:08:03 +00:00
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_import_no_confirmation(hass, discovery_flow_conf):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Test import requires no confirmation to set up."""
|
2018-07-23 13:08:03 +00:00
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
2018-10-23 09:14:46 +00:00
|
|
|
discovery_flow_conf['discovered'] = True
|
2018-07-23 13:08:03 +00:00
|
|
|
|
|
|
|
result = await flow.async_step_import(None)
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
|
2018-10-23 09:14:46 +00:00
|
|
|
async def test_import_single_instance(hass, discovery_flow_conf):
|
2018-07-23 13:08:03 +00:00
|
|
|
"""Test import doesn't create second instance."""
|
|
|
|
flow = config_entries.HANDLERS['test']()
|
|
|
|
flow.hass = hass
|
2018-10-23 09:14:46 +00:00
|
|
|
discovery_flow_conf['discovered'] = True
|
2018-07-23 13:08:03 +00:00
|
|
|
MockConfigEntry(domain='test').add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await flow.async_step_import(None)
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
2018-10-23 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_webhook_single_entry_allowed(hass, webhook_flow_conf):
|
|
|
|
"""Test only a single entry is allowed."""
|
|
|
|
flow = config_entries.HANDLERS['test_single']()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
MockConfigEntry(domain='test_single').add_to_hass(hass)
|
|
|
|
result = await flow.async_step_user()
|
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result['reason'] == 'one_instance_allowed'
|
|
|
|
|
|
|
|
|
|
|
|
async def test_webhook_multiple_entries_allowed(hass, webhook_flow_conf):
|
|
|
|
"""Test multiple entries are allowed when specified."""
|
|
|
|
flow = config_entries.HANDLERS['test_multiple']()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
MockConfigEntry(domain='test_multiple').add_to_hass(hass)
|
|
|
|
hass.config.api = Mock(base_url='http://example.com')
|
|
|
|
|
|
|
|
result = await flow.async_step_user()
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
|
|
|
|
async def test_webhook_config_flow_registers_webhook(hass, webhook_flow_conf):
|
|
|
|
"""Test setting up an entry creates a webhook."""
|
|
|
|
flow = config_entries.HANDLERS['test_single']()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
hass.config.api = Mock(base_url='http://example.com')
|
|
|
|
result = await flow.async_step_user(user_input={})
|
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result['data']['webhook_id'] is not None
|
2019-04-01 12:07:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
|
|
|
|
"""Test only a single entry is allowed."""
|
|
|
|
assert await setup.async_setup_component(hass, 'cloud', {})
|
|
|
|
|
|
|
|
async_setup_entry = Mock(return_value=mock_coro(True))
|
|
|
|
async_unload_entry = Mock(return_value=mock_coro(True))
|
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(hass, MockModule(
|
2019-04-01 12:07:12 +00:00
|
|
|
'test_single',
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
async_unload_entry=async_unload_entry,
|
|
|
|
async_remove_entry=config_entry_flow.webhook_async_remove_entry,
|
|
|
|
))
|
2019-05-13 08:16:55 +00:00
|
|
|
mock_entity_platform(hass, 'config_flow.test_single', None)
|
2019-04-01 12:07:12 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
'test_single', context={'source': config_entries.SOURCE_USER})
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
coro = mock_coro({
|
|
|
|
'cloudhook_url': 'https://example.com'
|
|
|
|
})
|
|
|
|
|
|
|
|
with patch('hass_nabucasa.cloudhooks.Cloudhooks.async_create',
|
|
|
|
return_value=coro) as mock_create, \
|
|
|
|
patch('homeassistant.components.cloud.async_active_subscription',
|
|
|
|
return_value=True), \
|
|
|
|
patch('homeassistant.components.cloud.async_is_logged_in',
|
|
|
|
return_value=True):
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result['flow_id'], {})
|
|
|
|
|
|
|
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result['description_placeholders']['webhook_url'] == \
|
|
|
|
'https://example.com'
|
|
|
|
assert len(mock_create.mock_calls) == 1
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
with patch('hass_nabucasa.cloudhooks.Cloudhooks.async_delete',
|
|
|
|
return_value=coro) as mock_delete:
|
|
|
|
|
|
|
|
result = \
|
|
|
|
await hass.config_entries.async_remove(result['result'].entry_id)
|
|
|
|
|
|
|
|
assert len(mock_delete.mock_calls) == 1
|
|
|
|
assert result['require_restart'] is False
|