2018-04-17 09:44:32 +00:00
|
|
|
"""Helpers for the data entry flow."""
|
2021-03-17 17:34:19 +00:00
|
|
|
from __future__ import annotations
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2021-09-22 18:59:52 +00:00
|
|
|
from http import HTTPStatus
|
2021-03-17 17:34:19 +00:00
|
|
|
from typing import Any
|
2020-05-26 14:28:22 +00:00
|
|
|
|
|
|
|
from aiohttp import web
|
2018-04-17 09:44:32 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-09 15:42:10 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2018-04-17 09:44:32 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
|
|
|
from homeassistant.components.http.data_validator import RequestDataValidator
|
2021-12-23 19:14:47 +00:00
|
|
|
|
|
|
|
from . import config_validation as cv
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
class _BaseFlowManagerView(HomeAssistantView):
|
|
|
|
"""Foundation for flow manager views."""
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2020-05-26 14:28:22 +00:00
|
|
|
def __init__(self, flow_mgr: data_entry_flow.FlowManager) -> None:
|
2018-05-01 16:20:41 +00:00
|
|
|
"""Initialize the flow manager index view."""
|
|
|
|
self._flow_mgr = flow_mgr
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
# pylint: disable=no-self-use
|
2021-04-15 17:17:07 +00:00
|
|
|
def _prepare_result_json(
|
2021-04-29 11:40:51 +00:00
|
|
|
self, result: data_entry_flow.FlowResult
|
|
|
|
) -> data_entry_flow.FlowResult:
|
2018-05-01 16:20:41 +00:00
|
|
|
"""Convert result to JSON."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
2018-05-01 16:20:41 +00:00
|
|
|
data = result.copy()
|
2019-07-31 19:25:30 +00:00
|
|
|
data.pop("result")
|
|
|
|
data.pop("data")
|
2018-05-01 16:20:41 +00:00
|
|
|
return data
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] != data_entry_flow.RESULT_TYPE_FORM:
|
2018-05-01 16:20:41 +00:00
|
|
|
return result
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2020-04-04 15:07:36 +00:00
|
|
|
import voluptuous_serialize # pylint: disable=import-outside-toplevel
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
data = result.copy()
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2021-10-17 18:08:11 +00:00
|
|
|
if (schema := data["data_schema"]) is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["data_schema"] = []
|
2018-05-01 16:20:41 +00:00
|
|
|
else:
|
2020-02-15 23:36:57 +00:00
|
|
|
data["data_schema"] = voluptuous_serialize.convert(
|
|
|
|
schema, custom_serializer=cv.custom_serializer
|
|
|
|
)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
return data
|
2018-04-17 09:44:32 +00:00
|
|
|
|
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
class FlowManagerIndexView(_BaseFlowManagerView):
|
|
|
|
"""View to create config flows."""
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@RequestDataValidator(
|
2020-04-24 16:31:56 +00:00
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required("handler"): vol.Any(str, list),
|
|
|
|
vol.Optional("show_advanced_options", default=False): cv.boolean,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-03-17 17:34:19 +00:00
|
|
|
async def post(self, request: web.Request, data: dict[str, Any]) -> web.Response:
|
2018-04-17 09:44:32 +00:00
|
|
|
"""Handle a POST request."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if isinstance(data["handler"], list):
|
|
|
|
handler = tuple(data["handler"])
|
2018-04-17 09:44:32 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
handler = data["handler"]
|
2018-04-17 09:44:32 +00:00
|
|
|
|
|
|
|
try:
|
2018-08-09 11:24:14 +00:00
|
|
|
result = await self._flow_mgr.async_init(
|
2020-05-26 14:28:22 +00:00
|
|
|
handler, # type: ignore
|
2020-04-24 16:31:56 +00:00
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_USER,
|
|
|
|
"show_advanced_options": data["show_advanced_options"],
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-04-17 09:44:32 +00:00
|
|
|
except data_entry_flow.UnknownHandler:
|
2021-09-22 18:59:52 +00:00
|
|
|
return self.json_message("Invalid handler specified", HTTPStatus.NOT_FOUND)
|
2018-04-17 09:44:32 +00:00
|
|
|
except data_entry_flow.UnknownStep:
|
2021-09-22 18:59:52 +00:00
|
|
|
return self.json_message(
|
|
|
|
"Handler does not support user", HTTPStatus.BAD_REQUEST
|
|
|
|
)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
result = self._prepare_result_json(result)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
|
|
|
return self.json(result)
|
|
|
|
|
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
class FlowManagerResourceView(_BaseFlowManagerView):
|
2018-04-17 09:44:32 +00:00
|
|
|
"""View to interact with the flow manager."""
|
|
|
|
|
2020-05-26 14:28:22 +00:00
|
|
|
async def get(self, request: web.Request, flow_id: str) -> web.Response:
|
2018-04-17 09:44:32 +00:00
|
|
|
"""Get the current state of a data_entry_flow."""
|
|
|
|
try:
|
|
|
|
result = await self._flow_mgr.async_configure(flow_id)
|
|
|
|
except data_entry_flow.UnknownFlow:
|
2021-09-22 18:59:52 +00:00
|
|
|
return self.json_message("Invalid flow specified", HTTPStatus.NOT_FOUND)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
result = self._prepare_result_json(result)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
|
|
|
return self.json(result)
|
|
|
|
|
|
|
|
@RequestDataValidator(vol.Schema(dict), allow_empty=True)
|
2020-05-26 14:28:22 +00:00
|
|
|
async def post(
|
2021-03-17 17:34:19 +00:00
|
|
|
self, request: web.Request, flow_id: str, data: dict[str, Any]
|
2020-05-26 14:28:22 +00:00
|
|
|
) -> web.Response:
|
2018-04-17 09:44:32 +00:00
|
|
|
"""Handle a POST request."""
|
|
|
|
try:
|
|
|
|
result = await self._flow_mgr.async_configure(flow_id, data)
|
|
|
|
except data_entry_flow.UnknownFlow:
|
2021-09-22 18:59:52 +00:00
|
|
|
return self.json_message("Invalid flow specified", HTTPStatus.NOT_FOUND)
|
2021-11-23 21:50:54 +00:00
|
|
|
except vol.Invalid as ex:
|
|
|
|
return self.json_message(
|
|
|
|
f"User input malformed: {ex}", HTTPStatus.BAD_REQUEST
|
|
|
|
)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
result = self._prepare_result_json(result)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
|
|
|
return self.json(result)
|
|
|
|
|
2020-05-26 14:28:22 +00:00
|
|
|
async def delete(self, request: web.Request, flow_id: str) -> web.Response:
|
2018-04-17 09:44:32 +00:00
|
|
|
"""Cancel a flow in progress."""
|
|
|
|
try:
|
|
|
|
self._flow_mgr.async_abort(flow_id)
|
|
|
|
except data_entry_flow.UnknownFlow:
|
2021-09-22 18:59:52 +00:00
|
|
|
return self.json_message("Invalid flow specified", HTTPStatus.NOT_FOUND)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("Flow aborted")
|