This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -9,6 +9,7 @@ from homeassistant.components.http.data_validator import RequestDataValidator
# mypy: allow-untyped-calls, allow-untyped-defs
class _BaseFlowManagerView(HomeAssistantView):
"""Foundation for flow manager views."""
@ -19,24 +20,24 @@ class _BaseFlowManagerView(HomeAssistantView):
# 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:
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
data = result.copy()
data.pop('result')
data.pop('data')
data.pop("result")
data.pop("data")
return data
if result['type'] != data_entry_flow.RESULT_TYPE_FORM:
if result["type"] != data_entry_flow.RESULT_TYPE_FORM:
return result
import voluptuous_serialize
data = result.copy()
schema = data['data_schema']
schema = data["data_schema"]
if schema is None:
data['data_schema'] = []
data["data_schema"] = []
else:
data['data_schema'] = voluptuous_serialize.convert(schema)
data["data_schema"] = voluptuous_serialize.convert(schema)
return data
@ -44,23 +45,24 @@ class _BaseFlowManagerView(HomeAssistantView):
class FlowManagerIndexView(_BaseFlowManagerView):
"""View to create config flows."""
@RequestDataValidator(vol.Schema({
vol.Required('handler'): vol.Any(str, list),
}, extra=vol.ALLOW_EXTRA))
@RequestDataValidator(
vol.Schema({vol.Required("handler"): vol.Any(str, list)}, extra=vol.ALLOW_EXTRA)
)
async def post(self, request, data):
"""Handle a POST request."""
if isinstance(data['handler'], list):
handler = tuple(data['handler'])
if isinstance(data["handler"], list):
handler = tuple(data["handler"])
else:
handler = data['handler']
handler = data["handler"]
try:
result = await self._flow_mgr.async_init(
handler, context={'source': config_entries.SOURCE_USER})
handler, context={"source": config_entries.SOURCE_USER}
)
except data_entry_flow.UnknownHandler:
return self.json_message('Invalid handler specified', 404)
return self.json_message("Invalid handler specified", 404)
except data_entry_flow.UnknownStep:
return self.json_message('Handler does not support user', 400)
return self.json_message("Handler does not support user", 400)
result = self._prepare_result_json(result)
@ -75,7 +77,7 @@ class FlowManagerResourceView(_BaseFlowManagerView):
try:
result = await self._flow_mgr.async_configure(flow_id)
except data_entry_flow.UnknownFlow:
return self.json_message('Invalid flow specified', 404)
return self.json_message("Invalid flow specified", 404)
result = self._prepare_result_json(result)
@ -87,9 +89,9 @@ class FlowManagerResourceView(_BaseFlowManagerView):
try:
result = await self._flow_mgr.async_configure(flow_id, data)
except data_entry_flow.UnknownFlow:
return self.json_message('Invalid flow specified', 404)
return self.json_message("Invalid flow specified", 404)
except vol.Invalid:
return self.json_message('User input malformed', 400)
return self.json_message("User input malformed", 400)
result = self._prepare_result_json(result)
@ -100,6 +102,6 @@ class FlowManagerResourceView(_BaseFlowManagerView):
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("Invalid flow specified", 404)
return self.json_message('Flow aborted')
return self.json_message("Flow aborted")