2018-04-13 14:14:53 +00:00
|
|
|
"""Classes to help gather user submissions."""
|
2021-02-12 09:58:20 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
import abc
|
2020-04-15 01:46:41 +00:00
|
|
|
import asyncio
|
2021-10-22 17:19:49 +00:00
|
|
|
from collections.abc import Iterable, Mapping
|
2021-02-13 12:21:37 +00:00
|
|
|
from types import MappingProxyType
|
2021-11-11 15:28:46 +00:00
|
|
|
from typing import Any, TypedDict
|
2018-04-13 14:14:53 +00:00
|
|
|
import uuid
|
2019-12-09 15:42:10 +00:00
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
import voluptuous as vol
|
2019-12-09 15:42:10 +00:00
|
|
|
|
|
|
|
from .core import HomeAssistant, callback
|
2018-04-13 14:14:53 +00:00
|
|
|
from .exceptions import HomeAssistantError
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
RESULT_TYPE_FORM = "form"
|
|
|
|
RESULT_TYPE_CREATE_ENTRY = "create_entry"
|
|
|
|
RESULT_TYPE_ABORT = "abort"
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP = "external"
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP_DONE = "external_done"
|
2020-11-09 17:39:28 +00:00
|
|
|
RESULT_TYPE_SHOW_PROGRESS = "progress"
|
|
|
|
RESULT_TYPE_SHOW_PROGRESS_DONE = "progress_done"
|
2019-05-10 12:33:50 +00:00
|
|
|
|
2020-11-09 17:39:28 +00:00
|
|
|
# Event that is fired when a flow is progressed via external or progress source.
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_DATA_ENTRY_FLOW_PROGRESSED = "data_entry_flow_progressed"
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlowError(HomeAssistantError):
|
|
|
|
"""Error while configuring an account."""
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownHandler(FlowError):
|
|
|
|
"""Unknown handler specified."""
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownFlow(FlowError):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Unknown flow specified."""
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UnknownStep(FlowError):
|
|
|
|
"""Unknown step specified."""
|
|
|
|
|
|
|
|
|
2019-12-16 11:27:43 +00:00
|
|
|
class AbortFlow(FlowError):
|
|
|
|
"""Exception to indicate a flow needs to be aborted."""
|
|
|
|
|
2021-05-20 15:53:29 +00:00
|
|
|
def __init__(
|
|
|
|
self, reason: str, description_placeholders: dict | None = None
|
|
|
|
) -> None:
|
2019-12-16 11:27:43 +00:00
|
|
|
"""Initialize an abort flow exception."""
|
|
|
|
super().__init__(f"Flow aborted: {reason}")
|
|
|
|
self.reason = reason
|
|
|
|
self.description_placeholders = description_placeholders
|
|
|
|
|
|
|
|
|
2021-04-29 11:40:51 +00:00
|
|
|
class FlowResult(TypedDict, total=False):
|
2021-04-15 17:17:07 +00:00
|
|
|
"""Typed result dict."""
|
|
|
|
|
|
|
|
version: int
|
|
|
|
type: str
|
|
|
|
flow_id: str
|
|
|
|
handler: str
|
|
|
|
title: str
|
|
|
|
data: Mapping[str, Any]
|
|
|
|
step_id: str
|
|
|
|
data_schema: vol.Schema
|
|
|
|
extra: str
|
|
|
|
required: bool
|
|
|
|
errors: dict[str, str] | None
|
|
|
|
description: str | None
|
|
|
|
description_placeholders: dict[str, Any] | None
|
|
|
|
progress_action: str
|
|
|
|
url: str
|
|
|
|
reason: str
|
|
|
|
context: dict[str, Any]
|
|
|
|
result: Any
|
2021-04-23 18:02:12 +00:00
|
|
|
last_step: bool | None
|
2021-05-06 05:14:01 +00:00
|
|
|
options: Mapping[str, Any]
|
2021-04-15 17:17:07 +00:00
|
|
|
|
|
|
|
|
2021-10-22 17:19:49 +00:00
|
|
|
@callback
|
|
|
|
def _async_flow_handler_to_flow_result(
|
|
|
|
flows: Iterable[FlowHandler], include_uninitialized: bool
|
|
|
|
) -> list[FlowResult]:
|
|
|
|
"""Convert a list of FlowHandler to a partial FlowResult that can be serialized."""
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"flow_id": flow.flow_id,
|
|
|
|
"handler": flow.handler,
|
|
|
|
"context": flow.context,
|
|
|
|
"step_id": flow.cur_step["step_id"] if flow.cur_step else None,
|
|
|
|
}
|
|
|
|
for flow in flows
|
|
|
|
if include_uninitialized or flow.cur_step is not None
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
class FlowManager(abc.ABC):
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Manage all the flows that are in progress."""
|
|
|
|
|
2020-08-27 11:56:20 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Initialize the flow manager."""
|
|
|
|
self.hass = hass
|
2021-03-17 16:34:55 +00:00
|
|
|
self._initializing: dict[str, list[asyncio.Future]] = {}
|
2021-04-15 17:13:42 +00:00
|
|
|
self._initialize_tasks: dict[str, list[asyncio.Task]] = {}
|
2021-10-22 17:19:49 +00:00
|
|
|
self._progress: dict[str, FlowHandler] = {}
|
|
|
|
self._handler_progress_index: dict[str, set[str]] = {}
|
2020-01-03 10:52:01 +00:00
|
|
|
|
2020-04-15 01:46:41 +00:00
|
|
|
async def async_wait_init_flow_finish(self, handler: str) -> None:
|
|
|
|
"""Wait till all flows in progress are initialized."""
|
2021-09-18 23:31:35 +00:00
|
|
|
if not (current := self._initializing.get(handler)):
|
2020-04-15 01:46:41 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
await asyncio.wait(current)
|
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
@abc.abstractmethod
|
|
|
|
async def async_create_flow(
|
|
|
|
self,
|
|
|
|
handler_key: Any,
|
|
|
|
*,
|
2021-03-17 16:34:55 +00:00
|
|
|
context: dict[str, Any] | None = None,
|
|
|
|
data: dict[str, Any] | None = None,
|
2021-02-12 09:58:20 +00:00
|
|
|
) -> FlowHandler:
|
2020-01-03 10:52:01 +00:00
|
|
|
"""Create a flow for specified handler.
|
|
|
|
|
|
|
|
Handler key is the domain of the component that we want to set up.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
async def async_finish_flow(
|
2021-04-29 11:40:51 +00:00
|
|
|
self, flow: FlowHandler, result: FlowResult
|
|
|
|
) -> FlowResult:
|
2020-01-03 10:52:01 +00:00
|
|
|
"""Finish a config flow and add an entry."""
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2021-04-29 11:40:51 +00:00
|
|
|
async def async_post_init(self, flow: FlowHandler, result: FlowResult) -> None:
|
2020-01-03 16:28:05 +00:00
|
|
|
"""Entry has finished executing its first step asynchronously."""
|
|
|
|
|
2021-10-13 15:37:14 +00:00
|
|
|
@callback
|
|
|
|
def async_has_matching_flow(
|
|
|
|
self, handler: str, context: dict[str, Any], data: Any
|
|
|
|
) -> bool:
|
|
|
|
"""Check if an existing matching flow is in progress with the same handler, context, and data."""
|
|
|
|
return any(
|
|
|
|
flow
|
2021-10-22 17:19:49 +00:00
|
|
|
for flow in self._async_progress_by_handler(handler)
|
|
|
|
if flow.context["source"] == context["source"] and flow.init_data == data
|
2021-10-13 15:37:14 +00:00
|
|
|
)
|
|
|
|
|
2021-10-22 17:19:49 +00:00
|
|
|
@callback
|
|
|
|
def async_get(self, flow_id: str) -> FlowResult | None:
|
|
|
|
"""Return a flow in progress as a partial FlowResult."""
|
|
|
|
if (flow := self._progress.get(flow_id)) is None:
|
|
|
|
raise UnknownFlow
|
|
|
|
return _async_flow_handler_to_flow_result([flow], False)[0]
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
@callback
|
2021-04-29 11:40:51 +00:00
|
|
|
def async_progress(self, include_uninitialized: bool = False) -> list[FlowResult]:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Return the flows in progress as a partial FlowResult."""
|
|
|
|
return _async_flow_handler_to_flow_result(
|
|
|
|
self._progress.values(), include_uninitialized
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_progress_by_handler(
|
|
|
|
self, handler: str, include_uninitialized: bool = False
|
|
|
|
) -> list[FlowResult]:
|
|
|
|
"""Return the flows in progress by handler as a partial FlowResult."""
|
|
|
|
return _async_flow_handler_to_flow_result(
|
|
|
|
self._async_progress_by_handler(handler), include_uninitialized
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_progress_by_handler(self, handler: str) -> list[FlowHandler]:
|
|
|
|
"""Return the flows in progress by handler."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [
|
2021-10-22 17:19:49 +00:00
|
|
|
self._progress[flow_id]
|
|
|
|
for flow_id in self._handler_progress_index.get(handler, {})
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
async def async_init(
|
2021-04-15 17:17:07 +00:00
|
|
|
self, handler: str, *, context: dict[str, Any] | None = None, data: Any = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Start a configuration flow."""
|
2019-05-27 02:48:27 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2020-04-15 01:46:41 +00:00
|
|
|
|
|
|
|
init_done: asyncio.Future = asyncio.Future()
|
|
|
|
self._initializing.setdefault(handler, []).append(init_done)
|
|
|
|
|
2021-04-15 17:13:42 +00:00
|
|
|
task = asyncio.create_task(self._async_init(init_done, handler, context, data))
|
|
|
|
self._initialize_tasks.setdefault(handler, []).append(task)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2020-04-15 01:46:41 +00:00
|
|
|
try:
|
2021-04-15 17:13:42 +00:00
|
|
|
flow, result = await task
|
2020-04-15 01:46:41 +00:00
|
|
|
finally:
|
2021-04-15 17:13:42 +00:00
|
|
|
self._initialize_tasks[handler].remove(task)
|
2020-04-15 01:46:41 +00:00
|
|
|
self._initializing[handler].remove(init_done)
|
2020-01-03 16:28:05 +00:00
|
|
|
|
|
|
|
if result["type"] != RESULT_TYPE_ABORT:
|
|
|
|
await self.async_post_init(flow, result)
|
|
|
|
|
|
|
|
return result
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2021-04-15 17:13:42 +00:00
|
|
|
async def _async_init(
|
|
|
|
self,
|
|
|
|
init_done: asyncio.Future,
|
|
|
|
handler: str,
|
|
|
|
context: dict,
|
|
|
|
data: Any,
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> tuple[FlowHandler, FlowResult]:
|
2021-04-15 17:13:42 +00:00
|
|
|
"""Run the init in a task to allow it to be canceled at shutdown."""
|
|
|
|
flow = await self.async_create_flow(handler, context=context, data=data)
|
|
|
|
if not flow:
|
|
|
|
raise UnknownFlow("Flow was not created")
|
|
|
|
flow.hass = self.hass
|
|
|
|
flow.handler = handler
|
|
|
|
flow.flow_id = uuid.uuid4().hex
|
|
|
|
flow.context = context
|
2021-10-13 15:37:14 +00:00
|
|
|
flow.init_data = data
|
2021-10-22 17:19:49 +00:00
|
|
|
self._async_add_flow_progress(flow)
|
2021-04-15 17:13:42 +00:00
|
|
|
result = await self._async_handle_step(flow, flow.init_step, data, init_done)
|
|
|
|
return flow, result
|
|
|
|
|
|
|
|
async def async_shutdown(self) -> None:
|
|
|
|
"""Cancel any initializing flows."""
|
|
|
|
for task_list in self._initialize_tasks.values():
|
|
|
|
for task in task_list:
|
|
|
|
task.cancel()
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_configure(
|
2021-03-17 16:34:55 +00:00
|
|
|
self, flow_id: str, user_input: dict | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-05-01 16:20:41 +00:00
|
|
|
"""Continue a configuration flow."""
|
2021-09-18 23:31:35 +00:00
|
|
|
if (flow := self._progress.get(flow_id)) is None:
|
2018-04-13 14:14:53 +00:00
|
|
|
raise UnknownFlow
|
|
|
|
|
2019-05-10 12:33:50 +00:00
|
|
|
cur_step = flow.cur_step
|
2021-10-22 17:19:49 +00:00
|
|
|
assert cur_step is not None
|
2019-05-10 12:33:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if cur_step.get("data_schema") is not None and user_input is not None:
|
|
|
|
user_input = cur_step["data_schema"](user_input)
|
2019-05-10 12:33:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await self._async_handle_step(flow, cur_step["step_id"], user_input)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2020-11-09 17:39:28 +00:00
|
|
|
if cur_step["type"] in (RESULT_TYPE_EXTERNAL_STEP, RESULT_TYPE_SHOW_PROGRESS):
|
|
|
|
if cur_step["type"] == RESULT_TYPE_EXTERNAL_STEP and result["type"] not in (
|
2019-07-31 19:25:30 +00:00
|
|
|
RESULT_TYPE_EXTERNAL_STEP,
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP_DONE,
|
|
|
|
):
|
|
|
|
raise ValueError(
|
|
|
|
"External step can only transition to "
|
|
|
|
"external step or external step done."
|
|
|
|
)
|
2020-11-09 17:39:28 +00:00
|
|
|
if cur_step["type"] == RESULT_TYPE_SHOW_PROGRESS and result["type"] not in (
|
|
|
|
RESULT_TYPE_SHOW_PROGRESS,
|
|
|
|
RESULT_TYPE_SHOW_PROGRESS_DONE,
|
|
|
|
):
|
|
|
|
raise ValueError(
|
|
|
|
"Show progress can only transition to show progress or show progress done."
|
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-05-10 12:33:50 +00:00
|
|
|
# If the result has changed from last result, fire event to update
|
|
|
|
# the frontend.
|
2020-11-09 17:39:28 +00:00
|
|
|
if (
|
|
|
|
cur_step["step_id"] != result.get("step_id")
|
|
|
|
or result["type"] == RESULT_TYPE_SHOW_PROGRESS
|
|
|
|
):
|
2019-05-10 12:33:50 +00:00
|
|
|
# Tell frontend to reload the flow state.
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.bus.async_fire(
|
|
|
|
EVENT_DATA_ENTRY_FLOW_PROGRESSED,
|
|
|
|
{"handler": flow.handler, "flow_id": flow_id, "refresh": True},
|
|
|
|
)
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
return result
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
@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."""
|
2021-10-22 17:19:49 +00:00
|
|
|
self._async_remove_flow_progress(flow_id)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_add_flow_progress(self, flow: FlowHandler) -> None:
|
|
|
|
"""Add a flow to in progress."""
|
|
|
|
self._progress[flow.flow_id] = flow
|
|
|
|
self._handler_progress_index.setdefault(flow.handler, set()).add(flow.flow_id)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_remove_flow_progress(self, flow_id: str) -> None:
|
|
|
|
"""Remove a flow from in progress."""
|
2021-10-31 18:01:16 +00:00
|
|
|
if (flow := self._progress.pop(flow_id, None)) is None:
|
2018-04-13 14:14:53 +00:00
|
|
|
raise UnknownFlow
|
2021-10-22 17:19:49 +00:00
|
|
|
handler = flow.handler
|
|
|
|
self._handler_progress_index[handler].remove(flow.flow_id)
|
|
|
|
if not self._handler_progress_index[handler]:
|
|
|
|
del self._handler_progress_index[handler]
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def _async_handle_step(
|
2020-04-15 01:46:41 +00:00
|
|
|
self,
|
|
|
|
flow: Any,
|
|
|
|
step_id: str,
|
2021-03-17 16:34:55 +00:00
|
|
|
user_input: dict | None,
|
|
|
|
step_done: asyncio.Future | None = None,
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Handle a step of a flow."""
|
2019-08-23 16:53:33 +00:00
|
|
|
method = f"async_step_{step_id}"
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
if not hasattr(flow, method):
|
2021-10-22 17:19:49 +00:00
|
|
|
self._async_remove_flow_progress(flow.flow_id)
|
2020-04-15 01:46:41 +00:00
|
|
|
if step_done:
|
|
|
|
step_done.set_result(None)
|
2019-07-31 19:25:30 +00:00
|
|
|
raise UnknownStep(
|
2020-01-03 13:47:06 +00:00
|
|
|
f"Handler {flow.__class__.__name__} doesn't support step {step_id}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-12-16 11:27:43 +00:00
|
|
|
try:
|
2021-04-29 11:40:51 +00:00
|
|
|
result: FlowResult = await getattr(flow, method)(user_input)
|
2019-12-16 11:27:43 +00:00
|
|
|
except AbortFlow as err:
|
|
|
|
result = _create_abort_data(
|
|
|
|
flow.flow_id, flow.handler, err.reason, err.description_placeholders
|
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2020-04-15 01:46:41 +00:00
|
|
|
# Mark the step as done.
|
|
|
|
# We do this before calling async_finish_flow because config entries will hit a
|
|
|
|
# circular dependency where async_finish_flow sets up new entry, which needs the
|
|
|
|
# integration to be set up, which is waiting for init to be done.
|
|
|
|
if step_done:
|
|
|
|
step_done.set_result(None)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] not in (
|
|
|
|
RESULT_TYPE_FORM,
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP,
|
|
|
|
RESULT_TYPE_CREATE_ENTRY,
|
|
|
|
RESULT_TYPE_ABORT,
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP_DONE,
|
2020-11-09 17:39:28 +00:00
|
|
|
RESULT_TYPE_SHOW_PROGRESS,
|
|
|
|
RESULT_TYPE_SHOW_PROGRESS_DONE,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2020-04-05 15:48:55 +00:00
|
|
|
raise ValueError(f"Handler returned incorrect type: {result['type']}")
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
if result["type"] in (
|
|
|
|
RESULT_TYPE_FORM,
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP,
|
|
|
|
RESULT_TYPE_EXTERNAL_STEP_DONE,
|
2020-11-09 17:39:28 +00:00
|
|
|
RESULT_TYPE_SHOW_PROGRESS,
|
|
|
|
RESULT_TYPE_SHOW_PROGRESS_DONE,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-05-10 12:33:50 +00:00
|
|
|
flow.cur_step = result
|
2018-04-13 14:14:53 +00:00
|
|
|
return result
|
|
|
|
|
2018-08-21 17:48:24 +00:00
|
|
|
# We pass a copy of the result because we're mutating our version
|
2021-04-15 17:17:07 +00:00
|
|
|
result = await self.async_finish_flow(flow, result.copy())
|
2018-08-21 17:48:24 +00:00
|
|
|
|
|
|
|
# _async_finish_flow may change result type, check it again
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] == RESULT_TYPE_FORM:
|
2019-05-10 12:33:50 +00:00
|
|
|
flow.cur_step = result
|
2018-08-21 17:48:24 +00:00
|
|
|
return result
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
# Abort and Success results both finish the flow
|
2021-10-22 17:19:49 +00:00
|
|
|
self._async_remove_flow_progress(flow.flow_id)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class FlowHandler:
|
|
|
|
"""Handle the configuration flow of a component."""
|
|
|
|
|
|
|
|
# Set by flow manager
|
2021-10-22 17:19:49 +00:00
|
|
|
cur_step: dict[str, Any] | None = None
|
2021-05-05 06:56:50 +00:00
|
|
|
|
|
|
|
# While not purely typed, it makes typehinting more useful for us
|
|
|
|
# and removes the need for constant None checks or asserts.
|
2021-02-13 12:21:37 +00:00
|
|
|
flow_id: str = None # type: ignore
|
|
|
|
hass: HomeAssistant = None # type: ignore
|
|
|
|
handler: str = None # type: ignore
|
|
|
|
# Ensure the attribute has a subscriptable, but immutable, default value.
|
2021-04-15 17:17:07 +00:00
|
|
|
context: dict[str, Any] = MappingProxyType({}) # type: ignore
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
# Set by _async_create_flow callback
|
2019-07-31 19:25:30 +00:00
|
|
|
init_step = "init"
|
2018-08-09 11:24:14 +00:00
|
|
|
|
2021-10-13 15:37:14 +00:00
|
|
|
# The initial data that was used to start the flow
|
|
|
|
init_data: Any = None
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
# Set by developer
|
|
|
|
VERSION = 1
|
|
|
|
|
2020-04-24 16:31:56 +00:00
|
|
|
@property
|
2021-03-17 16:34:55 +00:00
|
|
|
def source(self) -> str | None:
|
2020-04-24 16:31:56 +00:00
|
|
|
"""Source that initialized the flow."""
|
|
|
|
return self.context.get("source", None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def show_advanced_options(self) -> bool:
|
|
|
|
"""If we should show advanced options."""
|
|
|
|
return self.context.get("show_advanced_options", False)
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_show_form(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
step_id: str,
|
|
|
|
data_schema: vol.Schema = None,
|
2021-04-15 17:17:07 +00:00
|
|
|
errors: dict[str, str] | None = None,
|
|
|
|
description_placeholders: dict[str, Any] | None = None,
|
2021-04-23 18:02:12 +00:00
|
|
|
last_step: bool | None = None,
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Return the definition of a form to gather user input."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"type": RESULT_TYPE_FORM,
|
|
|
|
"flow_id": self.flow_id,
|
|
|
|
"handler": self.handler,
|
|
|
|
"step_id": step_id,
|
|
|
|
"data_schema": data_schema,
|
|
|
|
"errors": errors,
|
|
|
|
"description_placeholders": description_placeholders,
|
2021-04-23 18:02:12 +00:00
|
|
|
"last_step": last_step, # Display next or submit button in frontend
|
2018-04-13 14:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_create_entry(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
title: str,
|
2021-04-11 14:56:33 +00:00
|
|
|
data: Mapping[str, Any],
|
2021-03-17 16:34:55 +00:00
|
|
|
description: str | None = None,
|
|
|
|
description_placeholders: dict | None = None,
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Finish config flow and create a config entry."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"version": self.VERSION,
|
|
|
|
"type": RESULT_TYPE_CREATE_ENTRY,
|
|
|
|
"flow_id": self.flow_id,
|
|
|
|
"handler": self.handler,
|
|
|
|
"title": title,
|
|
|
|
"data": data,
|
|
|
|
"description": description,
|
|
|
|
"description_placeholders": description_placeholders,
|
2018-04-13 14:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_abort(
|
2021-03-17 16:34:55 +00:00
|
|
|
self, *, reason: str, description_placeholders: dict | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Abort the config flow."""
|
2019-12-16 11:27:43 +00:00
|
|
|
return _create_abort_data(
|
2021-02-13 12:21:37 +00:00
|
|
|
self.flow_id, self.handler, reason, description_placeholders
|
2019-12-16 11:27:43 +00:00
|
|
|
)
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_external_step(
|
2021-03-17 16:34:55 +00:00
|
|
|
self, *, step_id: str, url: str, description_placeholders: dict | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2019-05-10 12:33:50 +00:00
|
|
|
"""Return the definition of an external step for the user to take."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"type": RESULT_TYPE_EXTERNAL_STEP,
|
|
|
|
"flow_id": self.flow_id,
|
|
|
|
"handler": self.handler,
|
|
|
|
"step_id": step_id,
|
|
|
|
"url": url,
|
|
|
|
"description_placeholders": description_placeholders,
|
2019-05-10 12:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@callback
|
2021-04-29 11:40:51 +00:00
|
|
|
def async_external_step_done(self, *, next_step_id: str) -> FlowResult:
|
2019-05-10 12:33:50 +00:00
|
|
|
"""Return the definition of an external step for the user to take."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"type": RESULT_TYPE_EXTERNAL_STEP_DONE,
|
|
|
|
"flow_id": self.flow_id,
|
|
|
|
"handler": self.handler,
|
|
|
|
"step_id": next_step_id,
|
2019-05-10 12:33:50 +00:00
|
|
|
}
|
2019-12-16 11:27:43 +00:00
|
|
|
|
2020-11-09 17:39:28 +00:00
|
|
|
@callback
|
|
|
|
def async_show_progress(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
step_id: str,
|
|
|
|
progress_action: str,
|
2021-03-17 16:34:55 +00:00
|
|
|
description_placeholders: dict | None = None,
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2020-11-09 17:39:28 +00:00
|
|
|
"""Show a progress message to the user, without user input allowed."""
|
|
|
|
return {
|
|
|
|
"type": RESULT_TYPE_SHOW_PROGRESS,
|
|
|
|
"flow_id": self.flow_id,
|
|
|
|
"handler": self.handler,
|
|
|
|
"step_id": step_id,
|
|
|
|
"progress_action": progress_action,
|
|
|
|
"description_placeholders": description_placeholders,
|
|
|
|
}
|
|
|
|
|
|
|
|
@callback
|
2021-04-29 11:40:51 +00:00
|
|
|
def async_show_progress_done(self, *, next_step_id: str) -> FlowResult:
|
2020-11-09 17:39:28 +00:00
|
|
|
"""Mark the progress done."""
|
|
|
|
return {
|
|
|
|
"type": RESULT_TYPE_SHOW_PROGRESS_DONE,
|
|
|
|
"flow_id": self.flow_id,
|
|
|
|
"handler": self.handler,
|
|
|
|
"step_id": next_step_id,
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _create_abort_data(
|
|
|
|
flow_id: str,
|
|
|
|
handler: str,
|
|
|
|
reason: str,
|
2021-03-17 16:34:55 +00:00
|
|
|
description_placeholders: dict | None = None,
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2019-12-16 11:27:43 +00:00
|
|
|
"""Return the definition of an external step for the user to take."""
|
|
|
|
return {
|
|
|
|
"type": RESULT_TYPE_ABORT,
|
|
|
|
"flow_id": flow_id,
|
|
|
|
"handler": handler,
|
|
|
|
"reason": reason,
|
|
|
|
"description_placeholders": description_placeholders,
|
|
|
|
}
|