2018-04-17 09:44:32 +00:00
|
|
|
"""Helpers for the data entry flow."""
|
|
|
|
|
|
|
|
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
|
2020-04-08 22:57:47 +00:00
|
|
|
from homeassistant.const import HTTP_NOT_FOUND
|
2020-02-15 23:36:57 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2019-07-21 16:59:02 +00:00
|
|
|
# mypy: allow-untyped-calls, allow-untyped-defs
|
|
|
|
|
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
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
def __init__(self, flow_mgr):
|
|
|
|
"""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
|
|
|
|
def _prepare_result_json(self, result):
|
|
|
|
"""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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
schema = data["data_schema"]
|
2018-05-01 16:20:41 +00:00
|
|
|
if 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(
|
|
|
|
vol.Schema({vol.Required("handler"): vol.Any(str, list)}, extra=vol.ALLOW_EXTRA)
|
|
|
|
)
|
2018-04-17 09:44:32 +00:00
|
|
|
async def post(self, request, data):
|
|
|
|
"""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(
|
2019-07-31 19:25:30 +00:00
|
|
|
handler, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2018-04-17 09:44:32 +00:00
|
|
|
except data_entry_flow.UnknownHandler:
|
2020-04-08 22:57:47 +00:00
|
|
|
return self.json_message("Invalid handler specified", HTTP_NOT_FOUND)
|
2018-04-17 09:44:32 +00:00
|
|
|
except data_entry_flow.UnknownStep:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("Handler does not support user", 400)
|
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."""
|
|
|
|
|
|
|
|
async def get(self, request, flow_id):
|
|
|
|
"""Get the current state of a data_entry_flow."""
|
|
|
|
try:
|
|
|
|
result = await self._flow_mgr.async_configure(flow_id)
|
|
|
|
except data_entry_flow.UnknownFlow:
|
2020-04-08 22:57:47 +00:00
|
|
|
return self.json_message("Invalid flow specified", HTTP_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)
|
|
|
|
async def post(self, request, flow_id, data):
|
|
|
|
"""Handle a POST request."""
|
|
|
|
try:
|
|
|
|
result = await self._flow_mgr.async_configure(flow_id, data)
|
|
|
|
except data_entry_flow.UnknownFlow:
|
2020-04-08 22:57:47 +00:00
|
|
|
return self.json_message("Invalid flow specified", HTTP_NOT_FOUND)
|
2018-04-17 09:44:32 +00:00
|
|
|
except vol.Invalid:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("User input malformed", 400)
|
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)
|
|
|
|
|
|
|
|
async def delete(self, request, flow_id):
|
|
|
|
"""Cancel a flow in progress."""
|
|
|
|
try:
|
|
|
|
self._flow_mgr.async_abort(flow_id)
|
|
|
|
except data_entry_flow.UnknownFlow:
|
2020-04-08 22:57:47 +00:00
|
|
|
return self.json_message("Invalid flow specified", HTTP_NOT_FOUND)
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("Flow aborted")
|