2018-04-17 09:44:32 +00:00
|
|
|
"""Helpers for the data entry flow."""
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components.http import HomeAssistantView
|
|
|
|
from homeassistant.components.http.data_validator import RequestDataValidator
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
if result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
|
|
|
data = result.copy()
|
|
|
|
data.pop('result')
|
|
|
|
data.pop('data')
|
|
|
|
return data
|
2018-04-17 09:44:32 +00:00
|
|
|
|
2018-07-23 08:16:05 +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
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
import voluptuous_serialize
|
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
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
schema = data['data_schema']
|
|
|
|
if schema is None:
|
|
|
|
data['data_schema'] = []
|
|
|
|
else:
|
|
|
|
data['data_schema'] = voluptuous_serialize.convert(schema)
|
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
|
|
|
|
|
|
|
@RequestDataValidator(vol.Schema({
|
|
|
|
vol.Required('handler'): vol.Any(str, list),
|
2018-05-10 08:38:11 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA))
|
2018-04-17 09:44:32 +00:00
|
|
|
async def post(self, request, data):
|
|
|
|
"""Handle a POST request."""
|
|
|
|
if isinstance(data['handler'], list):
|
|
|
|
handler = tuple(data['handler'])
|
|
|
|
else:
|
|
|
|
handler = data['handler']
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = await self._flow_mgr.async_init(handler)
|
|
|
|
except data_entry_flow.UnknownHandler:
|
|
|
|
return self.json_message('Invalid handler specified', 404)
|
|
|
|
except data_entry_flow.UnknownStep:
|
|
|
|
return self.json_message('Handler does not support init', 400)
|
|
|
|
|
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:
|
|
|
|
return self.json_message('Invalid flow specified', 404)
|
|
|
|
|
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:
|
|
|
|
return self.json_message('Invalid flow specified', 404)
|
|
|
|
except vol.Invalid:
|
|
|
|
return self.json_message('User input malformed', 400)
|
|
|
|
|
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:
|
|
|
|
return self.json_message('Invalid flow specified', 404)
|
|
|
|
|
|
|
|
return self.json_message('Flow aborted')
|