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

+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')