Improve data entry flow typing (#83901)

This commit is contained in:
Erik Montnemery 2022-12-15 08:45:54 +01:00 committed by GitHub
parent cfa08c5229
commit 033a16b67e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -112,16 +112,19 @@ def _async_flow_handler_to_flow_result(
flows: Iterable[FlowHandler], include_uninitialized: bool flows: Iterable[FlowHandler], include_uninitialized: bool
) -> list[FlowResult]: ) -> list[FlowResult]:
"""Convert a list of FlowHandler to a partial FlowResult that can be serialized.""" """Convert a list of FlowHandler to a partial FlowResult that can be serialized."""
return [ results = []
FlowResult( for flow in flows:
if not include_uninitialized and flow.cur_step is None:
continue
result = FlowResult(
flow_id=flow.flow_id, flow_id=flow.flow_id,
handler=flow.handler, handler=flow.handler,
context=flow.context, context=flow.context,
step_id=flow.cur_step["step_id"] if flow.cur_step else None,
) )
for flow in flows if flow.cur_step:
if include_uninitialized or flow.cur_step is not None result["step_id"] = flow.cur_step["step_id"]
] results.append(result)
return results
class FlowManager(abc.ABC): class FlowManager(abc.ABC):
@ -269,8 +272,10 @@ class FlowManager(abc.ABC):
cur_step = flow.cur_step cur_step = flow.cur_step
assert cur_step is not None assert cur_step is not None
if cur_step.get("data_schema") is not None and user_input is not None: if (
user_input = cur_step["data_schema"](user_input) data_schema := cur_step.get("data_schema")
) is not None and user_input is not None:
user_input = data_schema(user_input)
# Handle a menu navigation choice # Handle a menu navigation choice
if cur_step["type"] == FlowResultType.MENU and user_input: if cur_step["type"] == FlowResultType.MENU and user_input:
@ -348,7 +353,7 @@ class FlowManager(abc.ABC):
async def _async_handle_step( async def _async_handle_step(
self, self,
flow: Any, flow: FlowHandler,
step_id: str, step_id: str,
user_input: dict | BaseServiceInfo | None, user_input: dict | BaseServiceInfo | None,
step_done: asyncio.Future | None = None, step_done: asyncio.Future | None = None,
@ -415,7 +420,7 @@ class FlowHandler:
"""Handle the configuration flow of a component.""" """Handle the configuration flow of a component."""
# Set by flow manager # Set by flow manager
cur_step: dict[str, Any] | None = None cur_step: FlowResult | None = None
# While not purely typed, it makes typehinting more useful for us # While not purely typed, it makes typehinting more useful for us
# and removes the need for constant None checks or asserts. # and removes the need for constant None checks or asserts.