Extract config flow to own module (#13840)
* Extract config flow to own module * Lint * fix lint * fix typo * ConfigFlowHandler -> FlowHandler * Rename to data_entry_flow
This commit is contained in:
1 parent
ddd2003629
commit
60508f7215
11 files changed
+428
-388
No files matched your search
+2
-2
@@ -10,7 +10,7 @@ import logging
|
||||
import threading
|
||||
from contextlib import contextmanager
|
||||
|
||||
from homeassistant import core as ha, loader, config_entries
|
||||
from homeassistant import core as ha, loader, data_entry_flow, config_entries
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.config import async_process_component_config
|
||||
from homeassistant.helpers import (
|
||||
@@ -455,7 +455,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
|
||||
"""Helper for creating config entries that adds some defaults."""
|
||||
|
||||
def __init__(self, *, domain='test', data=None, version=0, entry_id=None,
|
||||
source=config_entries.SOURCE_USER, title='Mock Title',
|
||||
source=data_entry_flow.SOURCE_USER, title='Mock Title',
|
||||
state=None):
|
||||
"""Initialize a mock config entry."""
|
||||
kwargs = {
|
||||
|
||||
@@ -8,7 +8,8 @@ import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries as core_ce
|
||||
from homeassistant.config_entries import ConfigFlowHandler, HANDLERS
|
||||
from homeassistant.config_entries import HANDLERS
|
||||
from homeassistant.data_entry_flow import FlowHandler
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.config import config_entries
|
||||
from homeassistant.loader import set_component
|
||||
@@ -93,7 +94,7 @@ def test_available_flows(hass, client):
|
||||
@asyncio.coroutine
|
||||
def test_initialize_flow(hass, client):
|
||||
"""Test we can initialize a flow."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
class TestFlow(FlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
schema = OrderedDict()
|
||||
@@ -142,7 +143,7 @@ def test_initialize_flow(hass, client):
|
||||
@asyncio.coroutine
|
||||
def test_abort(hass, client):
|
||||
"""Test a flow that aborts."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
class TestFlow(FlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
return self.async_abort(reason='bla')
|
||||
@@ -167,7 +168,7 @@ def test_create_account(hass, client):
|
||||
set_component(
|
||||
'test', MockModule('test', async_setup_entry=mock_coro_func(True)))
|
||||
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
class TestFlow(FlowHandler):
|
||||
VERSION = 1
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -187,7 +188,9 @@ def test_create_account(hass, client):
|
||||
assert data == {
|
||||
'domain': 'test',
|
||||
'title': 'Test Entry',
|
||||
'type': 'create_entry'
|
||||
'type': 'create_entry',
|
||||
'source': 'user',
|
||||
'version': 1,
|
||||
}
|
||||
|
||||
|
||||
@@ -197,7 +200,7 @@ def test_two_step_flow(hass, client):
|
||||
set_component(
|
||||
'test', MockModule('test', async_setup_entry=mock_coro_func(True)))
|
||||
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
class TestFlow(FlowHandler):
|
||||
VERSION = 1
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -245,13 +248,15 @@ def test_two_step_flow(hass, client):
|
||||
'domain': 'test',
|
||||
'type': 'create_entry',
|
||||
'title': 'user-title',
|
||||
'version': 1,
|
||||
'source': 'user',
|
||||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_get_progress_index(hass, client):
|
||||
"""Test querying for the flows that are in progress."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
class TestFlow(FlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -283,7 +288,7 @@ def test_get_progress_index(hass, client):
|
||||
@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."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
class TestFlow(FlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
schema = OrderedDict()
|
||||
|
||||
@@ -5,7 +5,7 @@ from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import discovery
|
||||
from homeassistant.util.dt import utcnow
|
||||
@@ -174,5 +174,5 @@ async def test_discover_config_flow(hass):
|
||||
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['source'] == data_entry_flow.SOURCE_DISCOVERY
|
||||
assert kwargs['data'] == discovery_info
|
||||
@@ -3,9 +3,8 @@ import asyncio
|
||||
from unittest.mock import MagicMock, patch, mock_open
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, loader
|
||||
from homeassistant import config_entries, loader, data_entry_flow
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockModule, mock_coro, MockConfigEntry
|
||||
@@ -100,7 +99,7 @@ def test_add_entry_calls_setup_entry(hass, manager):
|
||||
'comp',
|
||||
MockModule('comp', async_setup_entry=mock_setup_entry))
|
||||
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
@@ -112,7 +111,7 @@ def test_add_entry_calls_setup_entry(hass, manager):
|
||||
'token': 'supersecret'
|
||||
})
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'comp': TestFlow}):
|
||||
with patch.dict(config_entries.HANDLERS, {'comp': TestFlow, 'beer': 5}):
|
||||
yield from manager.flow.async_init('comp')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
@@ -152,7 +151,7 @@ def test_domains_gets_uniques(manager):
|
||||
@asyncio.coroutine
|
||||
def test_saving_and_loading(hass):
|
||||
"""Test that we're saving and loading correctly."""
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -167,7 +166,7 @@ def test_saving_and_loading(hass):
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
yield from hass.config_entries.flow.async_init('test')
|
||||
|
||||
class Test2Flow(config_entries.ConfigFlowHandler):
|
||||
class Test2Flow(data_entry_flow.FlowHandler):
|
||||
VERSION = 3
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -212,185 +211,6 @@ def test_saving_and_loading(hass):
|
||||
assert orig.source == loaded.source
|
||||
|
||||
|
||||
#######################
|
||||
# FLOW MANAGER TESTS #
|
||||
#######################
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_configure_reuses_handler_instance(manager):
|
||||
"""Test that we reuse instances."""
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
handle_count = 0
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
self.handle_count += 1
|
||||
return self.async_show_form(
|
||||
errors={'base': str(self.handle_count)},
|
||||
step_id='init')
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
form = yield from manager.flow.async_init('test')
|
||||
assert form['errors']['base'] == '1'
|
||||
form = yield from manager.flow.async_configure(form['flow_id'])
|
||||
assert form['errors']['base'] == '2'
|
||||
assert len(manager.flow.async_progress()) == 1
|
||||
assert len(manager.async_entries()) == 0
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_configure_two_steps(manager):
|
||||
"""Test that we reuse instances."""
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
VERSION = 1
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
if user_input is not None:
|
||||
self.init_data = user_input
|
||||
return self.async_step_second()
|
||||
return self.async_show_form(
|
||||
step_id='init',
|
||||
data_schema=vol.Schema([str])
|
||||
)
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_second(self, user_input=None):
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title='Test Entry',
|
||||
data=self.init_data + user_input
|
||||
)
|
||||
return self.async_show_form(
|
||||
step_id='second',
|
||||
data_schema=vol.Schema([str])
|
||||
)
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
form = yield from manager.flow.async_init('test')
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
form = yield from manager.flow.async_configure(
|
||||
form['flow_id'], 'INCORRECT-DATA')
|
||||
|
||||
form = yield from manager.flow.async_configure(
|
||||
form['flow_id'], ['INIT-DATA'])
|
||||
form = yield from manager.flow.async_configure(
|
||||
form['flow_id'], ['SECOND-DATA'])
|
||||
assert form['type'] == config_entries.RESULT_TYPE_CREATE_ENTRY
|
||||
assert len(manager.flow.async_progress()) == 0
|
||||
assert len(manager.async_entries()) == 1
|
||||
entry = manager.async_entries()[0]
|
||||
assert entry.domain == 'test'
|
||||
assert entry.data == ['INIT-DATA', 'SECOND-DATA']
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_show_form(manager):
|
||||
"""Test that abort removes the flow from progress."""
|
||||
schema = vol.Schema({
|
||||
vol.Required('username'): str,
|
||||
vol.Required('password'): str
|
||||
})
|
||||
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
return self.async_show_form(
|
||||
step_id='init',
|
||||
data_schema=schema,
|
||||
errors={
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
)
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
form = yield from manager.flow.async_init('test')
|
||||
assert form['type'] == 'form'
|
||||
assert form['data_schema'] is schema
|
||||
assert form['errors'] == {
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_abort_removes_instance(manager):
|
||||
"""Test that abort removes the flow from progress."""
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
is_new = True
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
old = self.is_new
|
||||
self.is_new = False
|
||||
return self.async_abort(reason=str(old))
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
form = yield from manager.flow.async_init('test')
|
||||
assert form['reason'] == 'True'
|
||||
assert len(manager.flow.async_progress()) == 0
|
||||
assert len(manager.async_entries()) == 0
|
||||
form = yield from manager.flow.async_init('test')
|
||||
assert form['reason'] == 'True'
|
||||
assert len(manager.flow.async_progress()) == 0
|
||||
assert len(manager.async_entries()) == 0
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_create_saves_data(manager):
|
||||
"""Test creating a config entry."""
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
return self.async_create_entry(
|
||||
title='Test Title',
|
||||
data='Test Data'
|
||||
)
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
yield from manager.flow.async_init('test')
|
||||
assert len(manager.flow.async_progress()) == 0
|
||||
assert len(manager.async_entries()) == 1
|
||||
|
||||
entry = manager.async_entries()[0]
|
||||
assert entry.version == 5
|
||||
assert entry.domain == 'test'
|
||||
assert entry.title == 'Test Title'
|
||||
assert entry.data == 'Test Data'
|
||||
assert entry.source == config_entries.SOURCE_USER
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_discovery_init_flow(manager):
|
||||
"""Test a flow initialized by discovery."""
|
||||
class TestFlow(config_entries.ConfigFlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_discovery(self, info):
|
||||
return self.async_create_entry(title=info['id'], data=info)
|
||||
|
||||
data = {
|
||||
'id': 'hello',
|
||||
'token': 'secret'
|
||||
}
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
|
||||
yield from manager.flow.async_init(
|
||||
'test', source=config_entries.SOURCE_DISCOVERY, data=data)
|
||||
assert len(manager.flow.async_progress()) == 0
|
||||
assert len(manager.async_entries()) == 1
|
||||
|
||||
entry = manager.async_entries()[0]
|
||||
assert entry.version == 5
|
||||
assert entry.domain == 'test'
|
||||
assert entry.title == 'hello'
|
||||
assert entry.data == data
|
||||
assert entry.source == config_entries.SOURCE_DISCOVERY
|
||||
|
||||
|
||||
async def test_forward_entry_sets_up_component(hass):
|
||||
"""Test we setup the component entry is forwarded to."""
|
||||
entry = MockConfigEntry(domain='original')
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
"""Test the flow classes."""
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.util.decorator import Registry
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager():
|
||||
"""Return a flow manager."""
|
||||
handlers = Registry()
|
||||
entries = []
|
||||
|
||||
async def async_add_entry(result):
|
||||
entries.append(result)
|
||||
|
||||
manager = data_entry_flow.FlowManager(
|
||||
None, handlers, mock_coro, async_add_entry)
|
||||
manager.mock_created_entries = entries
|
||||
manager.mock_reg_handler = handlers.register
|
||||
return manager
|
||||
|
||||
|
||||
async def test_configure_reuses_handler_instance(manager):
|
||||
"""Test that we reuse instances."""
|
||||
@manager.mock_reg_handler('test')
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
handle_count = 0
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
self.handle_count += 1
|
||||
return self.async_show_form(
|
||||
errors={'base': str(self.handle_count)},
|
||||
step_id='init')
|
||||
|
||||
form = await manager.async_init('test')
|
||||
assert form['errors']['base'] == '1'
|
||||
form = await manager.async_configure(form['flow_id'])
|
||||
assert form['errors']['base'] == '2'
|
||||
assert len(manager.async_progress()) == 1
|
||||
assert len(manager.mock_created_entries) == 0
|
||||
|
||||
|
||||
async def test_configure_two_steps(manager):
|
||||
"""Test that we reuse instances."""
|
||||
@manager.mock_reg_handler('test')
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
if user_input is not None:
|
||||
self.init_data = user_input
|
||||
return await self.async_step_second()
|
||||
return self.async_show_form(
|
||||
step_id='init',
|
||||
data_schema=vol.Schema([str])
|
||||
)
|
||||
|
||||
async def async_step_second(self, user_input=None):
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title='Test Entry',
|
||||
data=self.init_data + user_input
|
||||
)
|
||||
return self.async_show_form(
|
||||
step_id='second',
|
||||
data_schema=vol.Schema([str])
|
||||
)
|
||||
|
||||
form = await manager.async_init('test')
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
form = await manager.async_configure(
|
||||
form['flow_id'], 'INCORRECT-DATA')
|
||||
|
||||
form = await manager.async_configure(
|
||||
form['flow_id'], ['INIT-DATA'])
|
||||
form = await manager.async_configure(
|
||||
form['flow_id'], ['SECOND-DATA'])
|
||||
assert form['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert len(manager.async_progress()) == 0
|
||||
assert len(manager.mock_created_entries) == 1
|
||||
result = manager.mock_created_entries[0]
|
||||
assert result['domain'] == 'test'
|
||||
assert result['data'] == ['INIT-DATA', 'SECOND-DATA']
|
||||
|
||||
|
||||
async def test_show_form(manager):
|
||||
"""Test that abort removes the flow from progress."""
|
||||
schema = vol.Schema({
|
||||
vol.Required('username'): str,
|
||||
vol.Required('password'): str
|
||||
})
|
||||
|
||||
@manager.mock_reg_handler('test')
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
async def async_step_init(self, user_input=None):
|
||||
return self.async_show_form(
|
||||
step_id='init',
|
||||
data_schema=schema,
|
||||
errors={
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
)
|
||||
|
||||
form = await manager.async_init('test')
|
||||
assert form['type'] == 'form'
|
||||
assert form['data_schema'] is schema
|
||||
assert form['errors'] == {
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
|
||||
|
||||
async def test_abort_removes_instance(manager):
|
||||
"""Test that abort removes the flow from progress."""
|
||||
@manager.mock_reg_handler('test')
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
is_new = True
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
old = self.is_new
|
||||
self.is_new = False
|
||||
return self.async_abort(reason=str(old))
|
||||
|
||||
form = await manager.async_init('test')
|
||||
assert form['reason'] == 'True'
|
||||
assert len(manager.async_progress()) == 0
|
||||
assert len(manager.mock_created_entries) == 0
|
||||
form = await manager.async_init('test')
|
||||
assert form['reason'] == 'True'
|
||||
assert len(manager.async_progress()) == 0
|
||||
assert len(manager.mock_created_entries) == 0
|
||||
|
||||
|
||||
async def test_create_saves_data(manager):
|
||||
"""Test creating a config entry."""
|
||||
@manager.mock_reg_handler('test')
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
return self.async_create_entry(
|
||||
title='Test Title',
|
||||
data='Test Data'
|
||||
)
|
||||
|
||||
await manager.async_init('test')
|
||||
assert len(manager.async_progress()) == 0
|
||||
assert len(manager.mock_created_entries) == 1
|
||||
|
||||
entry = manager.mock_created_entries[0]
|
||||
assert entry['version'] == 5
|
||||
assert entry['domain'] == 'test'
|
||||
assert entry['title'] == 'Test Title'
|
||||
assert entry['data'] == 'Test Data'
|
||||
assert entry['source'] == data_entry_flow.SOURCE_USER
|
||||
|
||||
|
||||
async def test_discovery_init_flow(manager):
|
||||
"""Test a flow initialized by discovery."""
|
||||
@manager.mock_reg_handler('test')
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
async def async_step_discovery(self, info):
|
||||
return self.async_create_entry(title=info['id'], data=info)
|
||||
|
||||
data = {
|
||||
'id': 'hello',
|
||||
'token': 'secret'
|
||||
}
|
||||
|
||||
await manager.async_init(
|
||||
'test', source=data_entry_flow.SOURCE_DISCOVERY, data=data)
|
||||
assert len(manager.async_progress()) == 0
|
||||
assert len(manager.mock_created_entries) == 1
|
||||
|
||||
entry = manager.mock_created_entries[0]
|
||||
assert entry['version'] == 5
|
||||
assert entry['domain'] == 'test'
|
||||
assert entry['title'] == 'hello'
|
||||
assert entry['data'] == data
|
||||
assert entry['source'] == data_entry_flow.SOURCE_DISCOVERY
|
||||
Reference in new issue
Block a user