1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Rework FlowManager to use inheritance (#30133)

* Pull async_finish_flow/async_create_flow out of ConfigEntries

* Towards refactoring

* mypy fixes

* Mark Flow manager with abc.* annotations

* Flake8 fixes

* Mypy fixes

* Blacken data_entry_flow

* Blacken longer signatures caused by mypy changes

* test fixes

* Test fixes

* Fix typo

* Avoid protected member lint (W0212) in config_entries

* More protected member fixes

* Missing await
This commit is contained in:
Jc2k
2020-01-03 10:52:01 +00:00
committed by Paulus Schoutsen
parent 0a4f3ec1ec
commit fdfedd086b
12 changed files with 313 additions and 258 deletions

View File

@@ -14,27 +14,32 @@ def manager():
handlers = Registry()
entries = []
async def async_create_flow(handler_name, *, context, data):
handler = handlers.get(handler_name)
class FlowManager(data_entry_flow.FlowManager):
"""Test flow manager."""
if handler is None:
raise data_entry_flow.UnknownHandler
async def async_create_flow(self, handler_key, *, context, data):
"""Test create flow."""
handler = handlers.get(handler_key)
flow = handler()
flow.init_step = context.get("init_step", "init")
flow.source = context.get("source")
return flow
if handler is None:
raise data_entry_flow.UnknownHandler
async def async_add_entry(flow, result):
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
result["source"] = flow.context.get("source")
entries.append(result)
return result
flow = handler()
flow.init_step = context.get("init_step", "init")
flow.source = context.get("source")
return flow
manager = data_entry_flow.FlowManager(None, async_create_flow, async_add_entry)
manager.mock_created_entries = entries
manager.mock_reg_handler = handlers.register
return manager
async def async_finish_flow(self, flow, result):
"""Test finish flow."""
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
result["source"] = flow.context.get("source")
entries.append(result)
return result
mgr = FlowManager(None)
mgr.mock_created_entries = entries
mgr.mock_reg_handler = handlers.register
return mgr
async def test_configure_reuses_handler_instance(manager):
@@ -194,22 +199,23 @@ async def test_finish_callback_change_result_type(hass):
step_id="init", data_schema=vol.Schema({"count": int})
)
async def async_create_flow(handler_name, *, context, data):
"""Create a test flow."""
return TestFlow()
class FlowManager(data_entry_flow.FlowManager):
async def async_create_flow(self, handler_name, *, context, data):
"""Create a test flow."""
return TestFlow()
async def async_finish_flow(flow, result):
"""Redirect to init form if count <= 1."""
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
if result["data"] is None or result["data"].get("count", 0) <= 1:
return flow.async_show_form(
step_id="init", data_schema=vol.Schema({"count": int})
)
else:
result["result"] = result["data"]["count"]
return result
async def async_finish_flow(self, flow, result):
"""Redirect to init form if count <= 1."""
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
if result["data"] is None or result["data"].get("count", 0) <= 1:
return flow.async_show_form(
step_id="init", data_schema=vol.Schema({"count": int})
)
else:
result["result"] = result["data"]["count"]
return result
manager = data_entry_flow.FlowManager(hass, async_create_flow, async_finish_flow)
manager = FlowManager(hass)
result = await manager.async_init("test")
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM