Automatically generate config flow list (#23802)

* Add config flow to manifest.json

* Still load config flows via config flow platform

* Fix typo

* Lint

* Update config_flows.py"

* Catch import error when setting up entry

* Lint

* Fix tests

* Fix imports

* Lint

* Fix Unifi tests

* Fix translation test

* Add homekit_controller config flow
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-05-13 01:16:55 -07:00
1 parent b8cbd39985
commit 1e22c8daca
106 files changed
+742 -440

No files matched your search

+1 -1
View File
@@ -926,7 +926,7 @@ async def get_system_health_info(hass, domain):
def mock_integration(hass, module):
"""Mock an integration."""
integration = loader.Integration(
hass, 'homeassisant.components.{}'.format(module.DOMAIN), None,
hass, 'homeassistant.components.{}'.format(module.DOMAIN), None,
module.mock_manifest())
_LOGGER.info("Adding mock integration: %s", module.DOMAIN)
+18 -2
View File
@@ -12,9 +12,11 @@ from homeassistant.config_entries import HANDLERS
from homeassistant.core import callback
from homeassistant.setup import async_setup_component
from homeassistant.components.config import config_entries
from homeassistant.generated import config_flows
from tests.common import (
MockConfigEntry, MockModule, mock_coro_func, mock_integration)
MockConfigEntry, MockModule, mock_coro_func, mock_integration,
mock_entity_platform)
@pytest.fixture(autouse=True)
@@ -121,7 +123,7 @@ async def test_remove_entry_unauth(hass, client, hass_admin_user):
@asyncio.coroutine
def test_available_flows(hass, client):
"""Test querying the available flows."""
with patch.object(core_ce, 'FLOWS', ['hello', 'world']):
with patch.object(config_flows, 'FLOWS', ['hello', 'world']):
resp = yield from client.get(
'/api/config/config_entries/flow_handlers')
assert resp.status == 200
@@ -137,6 +139,8 @@ def test_available_flows(hass, client):
@asyncio.coroutine
def test_initialize_flow(hass, client):
"""Test we can initialize a flow."""
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
@asyncio.coroutine
def async_step_user(self, user_input=None):
@@ -221,6 +225,8 @@ async def test_initialize_flow_unauth(hass, client, hass_admin_user):
@asyncio.coroutine
def test_abort(hass, client):
"""Test a flow that aborts."""
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
@asyncio.coroutine
def async_step_user(self, user_input=None):
@@ -244,6 +250,8 @@ def test_abort(hass, client):
@asyncio.coroutine
def test_create_account(hass, client):
"""Test a flow that creates an account."""
mock_entity_platform(hass, 'config_flow.test', None)
mock_integration(
hass,
MockModule('test', async_setup_entry=mock_coro_func(True)))
@@ -286,6 +294,7 @@ def test_two_step_flow(hass, client):
mock_integration(
hass,
MockModule('test', async_setup_entry=mock_coro_func(True)))
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
VERSION = 1
@@ -352,6 +361,7 @@ async def test_continue_flow_unauth(hass, client, hass_admin_user):
mock_integration(
hass,
MockModule('test', async_setup_entry=mock_coro_func(True)))
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
VERSION = 1
@@ -402,6 +412,8 @@ async def test_continue_flow_unauth(hass, client, hass_admin_user):
@asyncio.coroutine
def test_get_progress_index(hass, client):
"""Test querying for the flows that are in progress."""
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
VERSION = 5
@@ -441,6 +453,8 @@ async def test_get_progress_index_unauth(hass, client, hass_admin_user):
@asyncio.coroutine
def test_get_progress_flow(hass, client):
"""Test we can query the API for same result as we get from init a flow."""
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
@asyncio.coroutine
def async_step_user(self, user_input=None):
@@ -474,6 +488,8 @@ def test_get_progress_flow(hass, client):
async def test_get_progress_flow_unauth(hass, client, hass_admin_user):
"""Test we can can't query the API for result of flow."""
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(core_ce.ConfigFlow):
async def async_step_user(self, user_input=None):
schema = OrderedDict()
+15 -12
View File
@@ -4,24 +4,27 @@ from unittest.mock import Mock, patch
import pytest
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.components import unifi
from homeassistant.components.unifi.const import (
CONF_POE_CONTROL, CONF_CONTROLLER, CONF_SITE_ID)
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
from homeassistant.components.unifi import controller, errors
from tests.common import mock_coro
CONTROLLER_DATA = {
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_PORT: 1234,
unifi.CONF_SITE_ID: 'site',
unifi.CONF_VERIFY_SSL: True
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_PORT: 1234,
CONF_SITE_ID: 'site',
CONF_VERIFY_SSL: True
}
ENTRY_CONFIG = {
unifi.CONF_CONTROLLER: CONTROLLER_DATA,
unifi.CONF_POE_CONTROL: True
}
CONF_CONTROLLER: CONTROLLER_DATA,
CONF_POE_CONTROL: True
}
async def test_controller_setup():
@@ -173,7 +176,7 @@ async def test_reset_unloads_entry_without_poe_control():
hass = Mock()
entry = Mock()
entry.data = dict(ENTRY_CONFIG)
entry.data[unifi.CONF_POE_CONTROL] = False
entry.data[CONF_POE_CONTROL] = False
api = Mock()
api.initialize.return_value = mock_coro(True)
@@ -201,7 +204,7 @@ async def test_get_controller(hass):
async def test_get_controller_verify_ssl_false(hass):
"""Successful call with verify ssl set to false."""
controller_data = dict(CONTROLLER_DATA)
controller_data[unifi.CONF_VERIFY_SSL] = False
controller_data[CONF_VERIFY_SSL] = False
with patch('aiounifi.Controller.login', return_value=mock_coro()):
assert await controller.get_controller(hass, **controller_data)
+51 -46
View File
@@ -2,7 +2,12 @@
from unittest.mock import Mock, patch
from homeassistant.components import unifi
from homeassistant.components.unifi import config_flow
from homeassistant.setup import async_setup_component
from homeassistant.components.unifi.const import (
CONF_POE_CONTROL, CONF_CONTROLLER, CONF_SITE_ID)
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
from tests.common import mock_coro, MockConfigEntry
@@ -137,7 +142,7 @@ async def test_unload_entry(hass):
async def test_flow_works(hass, aioclient_mock):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
with patch('aiounifi.Controller') as mock_controller:
@@ -157,11 +162,11 @@ async def test_flow_works(hass, aioclient_mock):
})
await flow.async_step_user(user_input={
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_PORT: 1234,
unifi.CONF_VERIFY_SSL: True
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_PORT: 1234,
CONF_VERIFY_SSL: True
})
result = await flow.async_step_site(user_input={})
@@ -173,27 +178,27 @@ async def test_flow_works(hass, aioclient_mock):
assert result['type'] == 'create_entry'
assert result['title'] == 'site name'
assert result['data'] == {
unifi.CONF_CONTROLLER: {
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_PORT: 1234,
unifi.CONF_SITE_ID: 'default',
unifi.CONF_VERIFY_SSL: True
CONF_CONTROLLER: {
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_PORT: 1234,
CONF_SITE_ID: 'default',
CONF_VERIFY_SSL: True
},
unifi.CONF_POE_CONTROL: True
CONF_POE_CONTROL: True
}
async def test_controller_multiple_sites(hass):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
flow.config = {
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
}
flow.sites = {
'site1': {
@@ -215,7 +220,7 @@ async def test_controller_multiple_sites(hass):
async def test_controller_site_already_configured(hass):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
entry = MockConfigEntry(domain=unifi.DOMAIN, data={
@@ -227,9 +232,9 @@ async def test_controller_site_already_configured(hass):
entry.add_to_hass(hass)
flow.config = {
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
}
flow.desc = 'site name'
flow.sites = {
@@ -245,7 +250,7 @@ async def test_controller_site_already_configured(hass):
async def test_user_permissions_low(hass, aioclient_mock):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
with patch('aiounifi.Controller') as mock_controller:
@@ -265,11 +270,11 @@ async def test_user_permissions_low(hass, aioclient_mock):
})
await flow.async_step_user(user_input={
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_PORT: 1234,
unifi.CONF_VERIFY_SSL: True
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_PORT: 1234,
CONF_VERIFY_SSL: True
})
result = await flow.async_step_site(user_input={})
@@ -279,16 +284,16 @@ async def test_user_permissions_low(hass, aioclient_mock):
async def test_user_credentials_faulty(hass, aioclient_mock):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
with patch.object(unifi, 'get_controller',
with patch.object(config_flow, 'get_controller',
side_effect=unifi.errors.AuthenticationRequired):
result = await flow.async_step_user({
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_SITE_ID: 'default',
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_SITE_ID: 'default',
})
assert result['type'] == 'form'
@@ -297,16 +302,16 @@ async def test_user_credentials_faulty(hass, aioclient_mock):
async def test_controller_is_unavailable(hass, aioclient_mock):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
with patch.object(unifi, 'get_controller',
with patch.object(config_flow, 'get_controller',
side_effect=unifi.errors.CannotConnect):
result = await flow.async_step_user({
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_SITE_ID: 'default',
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_SITE_ID: 'default',
})
assert result['type'] == 'form'
@@ -315,16 +320,16 @@ async def test_controller_is_unavailable(hass, aioclient_mock):
async def test_controller_unkown_problem(hass, aioclient_mock):
"""Test config flow."""
flow = unifi.UnifiFlowHandler()
flow = config_flow.UnifiFlowHandler()
flow.hass = hass
with patch.object(unifi, 'get_controller',
with patch.object(config_flow, 'get_controller',
side_effect=Exception):
result = await flow.async_step_user({
unifi.CONF_HOST: '1.2.3.4',
unifi.CONF_USERNAME: 'username',
unifi.CONF_PASSWORD: 'password',
unifi.CONF_SITE_ID: 'default',
CONF_HOST: '1.2.3.4',
CONF_USERNAME: 'username',
CONF_PASSWORD: 'password',
CONF_SITE_ID: 'default',
})
assert result['type'] == 'abort'
+12 -8
View File
@@ -10,7 +10,11 @@ from aiounifi.devices import Devices
from homeassistant import config_entries
from homeassistant.components import unifi
from homeassistant.components.unifi.const import (
CONF_POE_CONTROL, CONF_CONTROLLER, CONF_SITE_ID)
from homeassistant.setup import async_setup_component
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
import homeassistant.components.switch as switch
@@ -167,17 +171,17 @@ DEVICE_1 = {
}
CONTROLLER_DATA = {
unifi.CONF_HOST: 'mock-host',
unifi.CONF_USERNAME: 'mock-user',
unifi.CONF_PASSWORD: 'mock-pswd',
unifi.CONF_PORT: 1234,
unifi.CONF_SITE_ID: 'mock-site',
unifi.CONF_VERIFY_SSL: True
CONF_HOST: 'mock-host',
CONF_USERNAME: 'mock-user',
CONF_PASSWORD: 'mock-pswd',
CONF_PORT: 1234,
CONF_SITE_ID: 'mock-site',
CONF_VERIFY_SSL: True
}
ENTRY_CONFIG = {
unifi.CONF_CONTROLLER: CONTROLLER_DATA,
unifi.CONF_POE_CONTROL: True
CONF_CONTROLLER: CONTROLLER_DATA,
CONF_POE_CONTROL: True
}
CONTROLLER_ID = unifi.CONTROLLER_ID.format(host='mock-host', site='mock-site')
+5 -3
View File
@@ -6,7 +6,8 @@ import pytest
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.helpers import config_entry_flow
from tests.common import (
MockConfigEntry, MockModule, mock_coro, mock_integration)
MockConfigEntry, MockModule, mock_coro, mock_integration,
mock_entity_platform)
@pytest.fixture
@@ -102,7 +103,7 @@ async def test_discovery_confirmation(hass, discovery_flow_conf):
async def test_multiple_discoveries(hass, discovery_flow_conf):
"""Test we only create one instance for multiple discoveries."""
mock_integration(hass, MockModule('test'))
mock_entity_platform(hass, 'config_flow.test', None)
result = await hass.config_entries.flow.async_init(
'test', context={'source': config_entries.SOURCE_DISCOVERY}, data={})
@@ -116,7 +117,7 @@ async def test_multiple_discoveries(hass, discovery_flow_conf):
async def test_only_one_in_progress(hass, discovery_flow_conf):
"""Test a user initialized one will finish and cancel discovered one."""
mock_integration(hass, MockModule('test'))
mock_entity_platform(hass, 'config_flow.test', None)
# Discovery starts flow
result = await hass.config_entries.flow.async_init(
@@ -209,6 +210,7 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
async_unload_entry=async_unload_entry,
async_remove_entry=config_entry_flow.webhook_async_remove_entry,
))
mock_entity_platform(hass, 'config_flow.test_single', None)
result = await hass.config_entries.flow.async_init(
'test_single', context={'source': config_entries.SOURCE_USER})
+2 -2
View File
@@ -5,9 +5,9 @@ from unittest.mock import patch
import pytest
from homeassistant import config_entries
import homeassistant.helpers.translation as translation
from homeassistant.setup import async_setup_component
from homeassistant.generated import config_flows
from tests.common import mock_coro
@@ -15,7 +15,7 @@ from tests.common import mock_coro
def mock_config_flows():
"""Mock the config flows."""
flows = []
with patch.object(config_entries, 'FLOWS', flows):
with patch.object(config_flows, 'FLOWS', flows):
yield flows
+16
View File
@@ -53,6 +53,7 @@ async def test_call_setup_entry(hass):
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
result = await async_setup_component(hass, 'comp', {})
assert result
@@ -74,6 +75,7 @@ async def test_call_async_migrate_entry(hass):
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
result = await async_setup_component(hass, 'comp', {})
assert result
@@ -95,6 +97,7 @@ async def test_call_async_migrate_entry_failure_false(hass):
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
result = await async_setup_component(hass, 'comp', {})
assert result
@@ -117,6 +120,7 @@ async def test_call_async_migrate_entry_failure_exception(hass):
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
result = await async_setup_component(hass, 'comp', {})
assert result
@@ -139,6 +143,7 @@ async def test_call_async_migrate_entry_failure_not_bool(hass):
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
result = await async_setup_component(hass, 'comp', {})
assert result
@@ -158,6 +163,7 @@ async def test_call_async_migrate_entry_failure_not_supported(hass):
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
result = await async_setup_component(hass, 'comp', {})
assert result
@@ -201,6 +207,7 @@ async def test_remove_entry(hass, manager):
mock_entity_platform(
hass, 'light.test',
MockPlatform(async_setup_entry=mock_setup_entry_platform))
mock_entity_platform(hass, 'config_flow.test', None)
MockConfigEntry(
domain='test_other', entry_id='test1'
@@ -361,6 +368,7 @@ def test_add_entry_calls_setup_entry(hass, manager):
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, 'config_flow.comp', None)
class TestFlow(config_entries.ConfigFlow):
@@ -416,6 +424,7 @@ async def test_saving_and_loading(hass):
"""Test that we're saving and loading correctly."""
mock_integration(hass, MockModule(
'test', async_setup_entry=lambda *args: mock_coro(True)))
mock_entity_platform(hass, 'config_flow.test', None)
class TestFlow(config_entries.ConfigFlow):
VERSION = 5
@@ -511,6 +520,7 @@ async def test_forward_entry_does_not_setup_entry_if_setup_fails(hass):
async def test_discovery_notification(hass):
"""Test that we create/dismiss a notification when source is discovery."""
mock_integration(hass, MockModule('test'))
mock_entity_platform(hass, 'config_flow.test', None)
await async_setup_component(hass, 'persistent_notification', {})
class TestFlow(config_entries.ConfigFlow):
@@ -548,6 +558,7 @@ async def test_discovery_notification(hass):
async def test_discovery_notification_not_created(hass):
"""Test that we not create a notification when discovery is aborted."""
mock_integration(hass, MockModule('test'))
mock_entity_platform(hass, 'config_flow.test', None)
await async_setup_component(hass, 'persistent_notification', {})
class TestFlow(config_entries.ConfigFlow):
@@ -629,6 +640,7 @@ async def test_setup_raise_not_ready(hass, caplog):
mock_setup_entry = MagicMock(side_effect=ConfigEntryNotReady)
mock_integration(
hass, MockModule('test', async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, 'config_flow.test', None)
with patch('homeassistant.helpers.event.async_call_later') as mock_call:
await entry.async_setup(hass)
@@ -655,6 +667,7 @@ async def test_setup_retrying_during_unload(hass):
mock_setup_entry = MagicMock(side_effect=ConfigEntryNotReady)
mock_integration(
hass, MockModule('test', async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, 'config_flow.test', None)
with patch('homeassistant.helpers.event.async_call_later') as mock_call:
await entry.async_setup(hass)
@@ -720,6 +733,7 @@ async def test_entry_setup_succeed(hass, manager):
async_setup=mock_setup,
async_setup_entry=mock_setup_entry
))
mock_entity_platform(hass, 'config_flow.comp', None)
assert await manager.async_setup(entry.entry_id)
assert len(mock_setup.mock_calls) == 1
@@ -848,6 +862,7 @@ async def test_entry_reload_succeed(hass, manager):
async_setup_entry=async_setup_entry,
async_unload_entry=async_unload_entry
))
mock_entity_platform(hass, 'config_flow.comp', None)
assert await manager.async_reload(entry.entry_id)
assert len(async_unload_entry.mock_calls) == 1
@@ -879,6 +894,7 @@ async def test_entry_reload_not_loaded(hass, manager, state):
async_setup_entry=async_setup_entry,
async_unload_entry=async_unload_entry
))
mock_entity_platform(hass, 'config_flow.comp', None)
assert await manager.async_reload(entry.entry_id)
assert len(async_unload_entry.mock_calls) == 0