2018-04-13 14:14:53 +00:00
|
|
|
"""Classes to help gather user submissions."""
|
|
|
|
import logging
|
2018-09-11 09:21:48 +00:00
|
|
|
from typing import Dict, Any, Callable, Hashable, List, Optional # noqa pylint: disable=unused-import
|
2018-04-13 14:14:53 +00:00
|
|
|
import uuid
|
2018-07-23 08:24:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
from .core import callback, HomeAssistant
|
2018-04-13 14:14:53 +00:00
|
|
|
from .exceptions import HomeAssistantError
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
RESULT_TYPE_FORM = 'form'
|
|
|
|
RESULT_TYPE_CREATE_ENTRY = 'create_entry'
|
|
|
|
RESULT_TYPE_ABORT = 'abort'
|
|
|
|
|
|
|
|
|
|
|
|
class FlowError(HomeAssistantError):
|
|
|
|
"""Error while configuring an account."""
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownHandler(FlowError):
|
|
|
|
"""Unknown handler specified."""
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownFlow(FlowError):
|
|
|
|
"""Uknown flow specified."""
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownStep(FlowError):
|
|
|
|
"""Unknown step specified."""
|
|
|
|
|
|
|
|
|
|
|
|
class FlowManager:
|
|
|
|
"""Manage all the flows that are in progress."""
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def __init__(self, hass: HomeAssistant, async_create_flow: Callable,
|
|
|
|
async_finish_flow: Callable) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Initialize the flow manager."""
|
|
|
|
self.hass = hass
|
2018-07-17 22:28:44 +00:00
|
|
|
self._progress = {} # type: Dict[str, Any]
|
2018-04-14 18:38:24 +00:00
|
|
|
self._async_create_flow = async_create_flow
|
2018-04-17 09:44:32 +00:00
|
|
|
self._async_finish_flow = async_finish_flow
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
@callback
|
2018-07-23 08:24:39 +00:00
|
|
|
def async_progress(self) -> List[Dict]:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Return the flows in progress."""
|
|
|
|
return [{
|
|
|
|
'flow_id': flow.flow_id,
|
2018-04-14 18:38:24 +00:00
|
|
|
'handler': flow.handler,
|
2018-08-13 09:27:18 +00:00
|
|
|
'context': flow.context,
|
2018-04-13 14:14:53 +00:00
|
|
|
} for flow in self._progress.values()]
|
|
|
|
|
2018-08-17 18:22:49 +00:00
|
|
|
async def async_init(self, handler: Hashable, *,
|
|
|
|
context: Optional[Dict] = None,
|
2018-08-09 11:24:14 +00:00
|
|
|
data: Any = None) -> Any:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Start a configuration flow."""
|
2018-08-09 11:24:14 +00:00
|
|
|
flow = await self._async_create_flow(
|
|
|
|
handler, context=context, data=data)
|
2018-04-13 14:14:53 +00:00
|
|
|
flow.hass = self.hass
|
2018-04-14 18:38:24 +00:00
|
|
|
flow.handler = handler
|
|
|
|
flow.flow_id = uuid.uuid4().hex
|
2018-08-13 09:27:18 +00:00
|
|
|
flow.context = context
|
2018-04-14 18:38:24 +00:00
|
|
|
self._progress[flow.flow_id] = flow
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
return await self._async_handle_step(flow, flow.init_step, data)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_configure(
|
2018-08-21 17:48:24 +00:00
|
|
|
self, flow_id: str, user_input: Optional[Dict] = None) -> Any:
|
2018-05-01 16:20:41 +00:00
|
|
|
"""Continue a configuration flow."""
|
2018-04-13 14:14:53 +00:00
|
|
|
flow = self._progress.get(flow_id)
|
|
|
|
|
|
|
|
if flow is None:
|
|
|
|
raise UnknownFlow
|
|
|
|
|
|
|
|
step_id, data_schema = flow.cur_step
|
|
|
|
|
|
|
|
if data_schema is not None and user_input is not None:
|
|
|
|
user_input = data_schema(user_input)
|
|
|
|
|
|
|
|
return await self._async_handle_step(
|
|
|
|
flow, step_id, user_input)
|
|
|
|
|
|
|
|
@callback
|
2018-07-23 08:24:39 +00:00
|
|
|
def async_abort(self, flow_id: str) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Abort a flow."""
|
|
|
|
if self._progress.pop(flow_id, None) is None:
|
|
|
|
raise UnknownFlow
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def _async_handle_step(self, flow: Any, step_id: str,
|
2018-08-21 17:48:24 +00:00
|
|
|
user_input: Optional[Dict]) -> Dict:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Handle a step of a flow."""
|
|
|
|
method = "async_step_{}".format(step_id)
|
|
|
|
|
|
|
|
if not hasattr(flow, method):
|
|
|
|
self._progress.pop(flow.flow_id)
|
|
|
|
raise UnknownStep("Handler {} doesn't support step {}".format(
|
|
|
|
flow.__class__.__name__, step_id))
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
result = await getattr(flow, method)(user_input) # type: Dict
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
if result['type'] not in (RESULT_TYPE_FORM, RESULT_TYPE_CREATE_ENTRY,
|
|
|
|
RESULT_TYPE_ABORT):
|
|
|
|
raise ValueError(
|
|
|
|
'Handler returned incorrect type: {}'.format(result['type']))
|
|
|
|
|
|
|
|
if result['type'] == RESULT_TYPE_FORM:
|
|
|
|
flow.cur_step = (result['step_id'], result['data_schema'])
|
|
|
|
return result
|
|
|
|
|
2018-08-21 17:48:24 +00:00
|
|
|
# We pass a copy of the result because we're mutating our version
|
|
|
|
result = await self._async_finish_flow(flow, dict(result))
|
|
|
|
|
|
|
|
# _async_finish_flow may change result type, check it again
|
|
|
|
if result['type'] == RESULT_TYPE_FORM:
|
|
|
|
flow.cur_step = (result['step_id'], result['data_schema'])
|
|
|
|
return result
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
# Abort and Success results both finish the flow
|
|
|
|
self._progress.pop(flow.flow_id)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class FlowHandler:
|
|
|
|
"""Handle the configuration flow of a component."""
|
|
|
|
|
|
|
|
# Set by flow manager
|
|
|
|
flow_id = None
|
|
|
|
hass = None
|
2018-04-14 18:38:24 +00:00
|
|
|
handler = None
|
2018-04-13 14:14:53 +00:00
|
|
|
cur_step = None
|
2018-08-13 09:27:18 +00:00
|
|
|
context = None
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
# Set by _async_create_flow callback
|
|
|
|
init_step = 'init'
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
# Set by developer
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
@callback
|
2018-07-23 08:24:39 +00:00
|
|
|
def async_show_form(self, *, step_id: str, data_schema: vol.Schema = None,
|
2018-08-17 18:22:49 +00:00
|
|
|
errors: Optional[Dict] = None,
|
|
|
|
description_placeholders: Optional[Dict] = None) \
|
|
|
|
-> Dict:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Return the definition of a form to gather user input."""
|
|
|
|
return {
|
|
|
|
'type': RESULT_TYPE_FORM,
|
|
|
|
'flow_id': self.flow_id,
|
2018-04-14 18:38:24 +00:00
|
|
|
'handler': self.handler,
|
2018-04-13 14:14:53 +00:00
|
|
|
'step_id': step_id,
|
|
|
|
'data_schema': data_schema,
|
|
|
|
'errors': errors,
|
2018-06-13 15:14:52 +00:00
|
|
|
'description_placeholders': description_placeholders,
|
2018-04-13 14:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@callback
|
2018-09-30 12:45:48 +00:00
|
|
|
def async_create_entry(self, *, title: str, data: Dict,
|
|
|
|
description: Optional[str] = None,
|
|
|
|
description_placeholders: Optional[Dict] = None) \
|
|
|
|
-> Dict:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Finish config flow and create a config entry."""
|
|
|
|
return {
|
|
|
|
'version': self.VERSION,
|
|
|
|
'type': RESULT_TYPE_CREATE_ENTRY,
|
|
|
|
'flow_id': self.flow_id,
|
2018-04-14 18:38:24 +00:00
|
|
|
'handler': self.handler,
|
2018-04-13 14:14:53 +00:00
|
|
|
'title': title,
|
|
|
|
'data': data,
|
2018-09-30 12:45:48 +00:00
|
|
|
'description': description,
|
|
|
|
'description_placeholders': description_placeholders,
|
2018-04-13 14:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@callback
|
2019-03-14 19:57:50 +00:00
|
|
|
def async_abort(self, *, reason: str,
|
|
|
|
description_placeholders: Optional[Dict] = None) -> Dict:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Abort the config flow."""
|
|
|
|
return {
|
|
|
|
'type': RESULT_TYPE_ABORT,
|
|
|
|
'flow_id': self.flow_id,
|
2018-04-14 18:38:24 +00:00
|
|
|
'handler': self.handler,
|
2019-03-14 19:57:50 +00:00
|
|
|
'reason': reason,
|
|
|
|
'description_placeholders': description_placeholders,
|
2018-04-13 14:14:53 +00:00
|
|
|
}
|