1 """Helpers for the data entry flow."""
3 from __future__
import annotations
5 from http
import HTTPStatus
6 from typing
import Any, Generic
8 from aiohttp
import web
9 from typing_extensions
import TypeVar
10 import voluptuous
as vol
11 import voluptuous_serialize
13 from homeassistant
import data_entry_flow
17 from .
import config_validation
as cv
19 _FlowManagerT = TypeVar(
27 """Foundation for flow manager views."""
29 def __init__(self, flow_mgr: _FlowManagerT) ->
None:
30 """Initialize the flow manager index view."""
34 self, result: data_entry_flow.FlowResult
36 """Convert result to JSON."""
37 if result[
"type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
44 if "data_schema" not in result:
49 if (schema := data[
"data_schema"])
is None:
50 data[
"data_schema"] = []
52 data[
"data_schema"] = voluptuous_serialize.convert(
53 schema, custom_serializer=cv.custom_serializer
60 """View to create config flows."""
62 @RequestDataValidator(
vol.Schema(
{
vol.Required("handler"): str,
63 vol.Optional(
"show_advanced_options", default=
False): cv.boolean,
65 extra=vol.ALLOW_EXTRA,
68 async
def post(self, request: web.Request, data: dict[str, Any]) -> web.Response:
69 """Initialize a POST request.
71 Override `_post_impl` in subclasses which need
72 to implement their own `RequestDataValidator`
74 return await self.
_post_impl_post_impl(request, data)
77 self, request: web.Request, data: dict[str, Any]
79 """Handle a POST request."""
86 return self.json_message(
"Invalid handler specified", HTTPStatus.NOT_FOUND)
88 return self.json_message(
str(err), HTTPStatus.BAD_REQUEST)
92 return self.json(result)
94 def get_context(self, data: dict[str, Any]) -> dict[str, Any]:
96 return {
"show_advanced_options": data[
"show_advanced_options"]}
100 """View to interact with the flow manager."""
102 async
def get(self, request: web.Request, /, flow_id: str) -> web.Response:
103 """Get the current state of a data_entry_flow."""
105 result = await self.
_flow_mgr_flow_mgr.async_configure(flow_id)
107 return self.json_message(
"Invalid flow specified", HTTPStatus.NOT_FOUND)
111 return self.json(result)
113 @RequestDataValidator(vol.Schema(dict), allow_empty=
True)
115 self, request: web.Request, data: dict[str, Any], flow_id: str
117 """Handle a POST request."""
119 result = await self.
_flow_mgr_flow_mgr.async_configure(flow_id, data)
121 return self.json_message(
"Invalid flow specified", HTTPStatus.NOT_FOUND)
123 return self.json({
"errors": ex.schema_errors}, HTTPStatus.BAD_REQUEST)
127 return self.json(result)
129 async
def delete(self, request: web.Request, flow_id: str) -> web.Response:
130 """Cancel a flow in progress."""
132 self.
_flow_mgr_flow_mgr.async_abort(flow_id)
134 return self.json_message(
"Invalid flow specified", HTTPStatus.NOT_FOUND)
136 return self.json_message(
"Flow aborted")
137
web.Response post(self, web.Request request, dict[str, Any] data)
web.Response _post_impl(self, web.Request request, dict[str, Any] data)
dict[str, Any] get_context(self, dict[str, Any] data)
web.Response get(self, web.Request request, str flow_id)
web.Response post(self, web.Request request, dict[str, Any] data, str flow_id)
None __init__(self, _FlowManagerT flow_mgr)
data_entry_flow.FlowResult _prepare_result_json(self, data_entry_flow.FlowResult result)
web.Response delete(self, web.Request request, str config_key)