Refactor data entry flow (#15883)

* Refactoring data_entry_flow and config_entry_flow

Move SOURCE_* to config_entries
Change data_entry_flow.FlowManager.async_init() source param default
 to None
Change this first step_id as source or init if source is None
_BaseFlowManagerView pass in SOURCE_USER as default source

* First step of data entry flow decided by _async_create_flow() now

* Lint

* Change helpers.config_entry_flow.DiscoveryFlowHandler default step

* Change FlowManager.async_init source param to context dict param
This commit is contained in:
Jason Hu 2018-08-09 04:24:14 -07:00 committed by Paulus Schoutsen
parent 39d19f2183
commit f58425dd3c
25 changed files with 128 additions and 79 deletions

View file

@ -12,13 +12,18 @@ def manager():
handlers = Registry()
entries = []
async def async_create_flow(handler_name, *, source, data):
async def async_create_flow(handler_name, *, context, data):
handler = handlers.get(handler_name)
if handler is None:
raise data_entry_flow.UnknownHandler
return handler()
flow = handler()
flow.init_step = context.get('init_step', 'init') \
if context is not None else 'init'
flow.source = context.get('source') \
if context is not None else 'user_input'
return flow
async def async_add_entry(result):
if (result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY):
@ -57,12 +62,12 @@ async def test_configure_two_steps(manager):
class TestFlow(data_entry_flow.FlowHandler):
VERSION = 1
async def async_step_init(self, user_input=None):
async def async_step_first(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',
step_id='first',
data_schema=vol.Schema([str])
)
@ -77,7 +82,7 @@ async def test_configure_two_steps(manager):
data_schema=vol.Schema([str])
)
form = await manager.async_init('test')
form = await manager.async_init('test', context={'init_step': 'first'})
with pytest.raises(vol.Invalid):
form = await manager.async_configure(
@ -163,7 +168,7 @@ async def test_create_saves_data(manager):
assert entry['handler'] == 'test'
assert entry['title'] == 'Test Title'
assert entry['data'] == 'Test Data'
assert entry['source'] == data_entry_flow.SOURCE_USER
assert entry['source'] == 'user_input'
async def test_discovery_init_flow(manager):
@ -172,7 +177,7 @@ async def test_discovery_init_flow(manager):
class TestFlow(data_entry_flow.FlowHandler):
VERSION = 5
async def async_step_discovery(self, info):
async def async_step_init(self, info):
return self.async_create_entry(title=info['id'], data=info)
data = {
@ -181,7 +186,7 @@ async def test_discovery_init_flow(manager):
}
await manager.async_init(
'test', source=data_entry_flow.SOURCE_DISCOVERY, data=data)
'test', context={'source': 'discovery'}, data=data)
assert len(manager.async_progress()) == 0
assert len(manager.mock_created_entries) == 1
@ -190,4 +195,4 @@ async def test_discovery_init_flow(manager):
assert entry['handler'] == 'test'
assert entry['title'] == 'hello'
assert entry['data'] == data
assert entry['source'] == data_entry_flow.SOURCE_DISCOVERY
assert entry['source'] == 'discovery'