2018-04-13 14:14:53 +00:00
|
|
|
"""Classes to help gather user submissions."""
|
2024-03-08 15:36:11 +00:00
|
|
|
|
2021-02-12 09:58:20 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
import abc
|
2024-01-11 11:00:12 +00:00
|
|
|
import asyncio
|
2024-05-22 06:16:08 +00:00
|
|
|
from collections import defaultdict
|
2024-06-26 09:30:07 +00:00
|
|
|
from collections.abc import Callable, Container, Hashable, Iterable, Mapping
|
2024-01-11 11:00:12 +00:00
|
|
|
from contextlib import suppress
|
2022-11-29 09:16:01 +00:00
|
|
|
import copy
|
2021-11-23 12:35:53 +00:00
|
|
|
from dataclasses import dataclass
|
2023-07-23 21:19:24 +00:00
|
|
|
from enum import StrEnum
|
2022-08-29 20:25:34 +00:00
|
|
|
import logging
|
2021-02-13 12:21:37 +00:00
|
|
|
from types import MappingProxyType
|
2024-06-26 09:30:07 +00:00
|
|
|
from typing import Any, Generic, Required, TypedDict, cast
|
2019-12-09 15:42:10 +00:00
|
|
|
|
2024-03-05 21:52:11 +00:00
|
|
|
from typing_extensions import TypeVar
|
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
|
2024-11-07 17:23:35 +00:00
|
|
|
from .helpers.frame import ReportBehavior, report_usage
|
2024-01-13 10:56:05 +00:00
|
|
|
from .loader import async_suggest_report_issue
|
2022-01-22 21:29:16 +00:00
|
|
|
from .util import uuid as uuid_util
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2022-08-29 20:25:34 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-06-08 05:02:44 +00:00
|
|
|
|
|
|
|
class FlowResultType(StrEnum):
|
|
|
|
"""Result type for a data entry flow."""
|
|
|
|
|
|
|
|
FORM = "form"
|
|
|
|
CREATE_ENTRY = "create_entry"
|
|
|
|
ABORT = "abort"
|
|
|
|
EXTERNAL_STEP = "external"
|
|
|
|
EXTERNAL_STEP_DONE = "external_done"
|
|
|
|
SHOW_PROGRESS = "progress"
|
|
|
|
SHOW_PROGRESS_DONE = "progress_done"
|
|
|
|
MENU = "menu"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-12-14 16:32:14 +00:00
|
|
|
FLOW_NOT_COMPLETE_STEPS = {
|
|
|
|
FlowResultType.FORM,
|
|
|
|
FlowResultType.EXTERNAL_STEP,
|
|
|
|
FlowResultType.EXTERNAL_STEP_DONE,
|
|
|
|
FlowResultType.SHOW_PROGRESS,
|
|
|
|
FlowResultType.SHOW_PROGRESS_DONE,
|
|
|
|
FlowResultType.MENU,
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:52:39 +00:00
|
|
|
|
2024-01-11 20:05:20 +00:00
|
|
|
STEP_ID_OPTIONAL_STEPS = {
|
2024-01-15 08:37:57 +00:00
|
|
|
FlowResultType.EXTERNAL_STEP,
|
|
|
|
FlowResultType.FORM,
|
|
|
|
FlowResultType.MENU,
|
2024-01-11 20:05:20 +00:00
|
|
|
FlowResultType.SHOW_PROGRESS,
|
|
|
|
}
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2024-10-08 10:18:45 +00:00
|
|
|
_FlowContextT = TypeVar("_FlowContextT", bound="FlowContext", default="FlowContext")
|
|
|
|
_FlowResultT = TypeVar(
|
|
|
|
"_FlowResultT", bound="FlowResult[Any, Any]", default="FlowResult"
|
|
|
|
)
|
2024-03-07 11:41:14 +00:00
|
|
|
_HandlerT = TypeVar("_HandlerT", default=str)
|
2024-02-29 15:52:39 +00:00
|
|
|
|
|
|
|
|
2023-04-04 10:44:59 +00:00
|
|
|
@dataclass(slots=True)
|
2021-11-23 12:35:53 +00:00
|
|
|
class BaseServiceInfo:
|
|
|
|
"""Base class for discovery ServiceInfo."""
|
|
|
|
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
class FlowError(HomeAssistantError):
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Base class for data entry errors."""
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
|
|
|
|
|
2024-07-02 19:57:09 +00:00
|
|
|
class InvalidData(vol.Invalid):
|
2024-01-30 11:24:19 +00:00
|
|
|
"""Invalid data provided."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
message: str,
|
2024-06-26 09:30:07 +00:00
|
|
|
path: list[Hashable] | None,
|
2024-01-30 11:24:19 +00:00
|
|
|
error_message: str | None,
|
|
|
|
schema_errors: dict[str, Any],
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> None:
|
2024-11-28 23:37:26 +00:00
|
|
|
"""Initialize an invalid data exception."""
|
2024-01-30 11:24:19 +00:00
|
|
|
super().__init__(message, path, error_message, **kwargs)
|
|
|
|
self.schema_errors = schema_errors
|
|
|
|
|
|
|
|
|
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__(
|
2022-05-31 08:33:34 +00:00
|
|
|
self, reason: str, description_placeholders: Mapping[str, str] | None = None
|
2021-05-20 15:53:29 +00:00
|
|
|
) -> 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
|
|
|
|
|
|
|
|
|
2024-10-08 10:18:45 +00:00
|
|
|
class FlowContext(TypedDict, total=False):
|
|
|
|
"""Typed context dict."""
|
|
|
|
|
|
|
|
show_advanced_options: bool
|
|
|
|
source: str
|
|
|
|
|
|
|
|
|
|
|
|
class FlowResult(TypedDict, Generic[_FlowContextT, _HandlerT], total=False):
|
2021-04-15 17:17:07 +00:00
|
|
|
"""Typed result dict."""
|
|
|
|
|
2024-10-08 10:18:45 +00:00
|
|
|
context: _FlowContextT
|
2022-03-04 15:42:02 +00:00
|
|
|
data_schema: vol.Schema | None
|
2022-12-09 09:24:08 +00:00
|
|
|
data: Mapping[str, Any]
|
2024-11-15 13:39:57 +00:00
|
|
|
description_placeholders: Mapping[str, str] | None
|
2022-12-09 09:24:08 +00:00
|
|
|
description: str | None
|
|
|
|
errors: dict[str, str] | None
|
|
|
|
extra: str
|
2022-12-30 11:01:45 +00:00
|
|
|
flow_id: Required[str]
|
2024-03-07 11:41:14 +00:00
|
|
|
handler: Required[_HandlerT]
|
2022-12-09 09:24:08 +00:00
|
|
|
last_step: bool | None
|
2024-04-12 02:14:37 +00:00
|
|
|
menu_options: Container[str]
|
2023-08-22 08:29:16 +00:00
|
|
|
preview: str | None
|
2021-04-15 17:17:07 +00:00
|
|
|
progress_action: str
|
2024-01-11 11:00:12 +00:00
|
|
|
progress_task: asyncio.Task[Any] | None
|
2021-04-15 17:17:07 +00:00
|
|
|
reason: str
|
2022-12-09 09:24:08 +00:00
|
|
|
required: bool
|
2021-04-15 17:17:07 +00:00
|
|
|
result: Any
|
2022-12-09 09:24:08 +00:00
|
|
|
step_id: str
|
|
|
|
title: str
|
2024-02-27 17:28:19 +00:00
|
|
|
translation_domain: str
|
2022-12-09 09:24:08 +00:00
|
|
|
type: FlowResultType
|
|
|
|
url: str
|
2021-04-15 17:17:07 +00:00
|
|
|
|
|
|
|
|
2024-01-30 11:24:19 +00:00
|
|
|
def _map_error_to_schema_errors(
|
|
|
|
schema_errors: dict[str, Any],
|
|
|
|
error: vol.Invalid,
|
|
|
|
data_schema: vol.Schema,
|
|
|
|
) -> None:
|
|
|
|
"""Map an error to the correct position in the schema_errors.
|
|
|
|
|
|
|
|
Raises ValueError if the error path could not be found in the schema.
|
|
|
|
Limitation: Nested schemas are not supported and a ValueError will be raised.
|
|
|
|
"""
|
|
|
|
schema = data_schema.schema
|
|
|
|
error_path = error.path
|
|
|
|
if not error_path or (path_part := error_path[0]) not in schema:
|
|
|
|
raise ValueError("Could not find path in schema")
|
|
|
|
|
|
|
|
if len(error_path) > 1:
|
|
|
|
raise ValueError("Nested schemas are not supported")
|
|
|
|
|
|
|
|
# path_part can also be vol.Marker, but we need a string key
|
|
|
|
path_part_str = str(path_part)
|
|
|
|
schema_errors[path_part_str] = error.error_message
|
|
|
|
|
|
|
|
|
2024-10-08 10:18:45 +00:00
|
|
|
class FlowManager(abc.ABC, Generic[_FlowContextT, _FlowResultT, _HandlerT]):
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Manage all the flows that are in progress."""
|
|
|
|
|
2024-03-27 11:32:29 +00:00
|
|
|
_flow_result: type[_FlowResultT] = FlowResult # type: ignore[assignment]
|
2024-02-29 15:52:39 +00:00
|
|
|
|
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
|
2024-03-07 11:41:14 +00:00
|
|
|
self._preview: set[_HandlerT] = set()
|
2024-10-08 10:18:45 +00:00
|
|
|
self._progress: dict[
|
|
|
|
str, FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]
|
|
|
|
] = {}
|
2024-05-22 06:16:08 +00:00
|
|
|
self._handler_progress_index: defaultdict[
|
2024-10-08 10:18:45 +00:00
|
|
|
_HandlerT, set[FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]]
|
2024-05-22 06:16:08 +00:00
|
|
|
] = defaultdict(set)
|
|
|
|
self._init_data_process_index: defaultdict[
|
2024-10-08 10:18:45 +00:00
|
|
|
type, set[FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]]
|
2024-05-22 06:16:08 +00:00
|
|
|
] = defaultdict(set)
|
2020-01-03 10:52:01 +00:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
async def async_create_flow(
|
|
|
|
self,
|
2024-03-07 11:41:14 +00:00
|
|
|
handler_key: _HandlerT,
|
2020-01-03 10:52:01 +00:00
|
|
|
*,
|
2024-10-08 10:18:45 +00:00
|
|
|
context: _FlowContextT | None = None,
|
2021-03-17 16:34:55 +00:00
|
|
|
data: dict[str, Any] | None = None,
|
2024-10-08 10:18:45 +00:00
|
|
|
) -> FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]:
|
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(
|
2024-10-08 10:18:45 +00:00
|
|
|
self,
|
|
|
|
flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT],
|
|
|
|
result: _FlowResultT,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2024-09-18 16:19:13 +00:00
|
|
|
"""Finish a data entry flow.
|
|
|
|
|
|
|
|
This method is called when a flow step returns FlowResultType.ABORT or
|
|
|
|
FlowResultType.CREATE_ENTRY.
|
|
|
|
"""
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2024-02-29 15:52:39 +00:00
|
|
|
async def async_post_init(
|
2024-10-08 10:18:45 +00:00
|
|
|
self,
|
|
|
|
flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT],
|
|
|
|
result: _FlowResultT,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> None:
|
2020-01-03 16:28:05 +00:00
|
|
|
"""Entry has finished executing its first step asynchronously."""
|
|
|
|
|
2021-10-22 17:19:49 +00:00
|
|
|
@callback
|
2024-02-29 15:52:39 +00:00
|
|
|
def async_get(self, flow_id: str) -> _FlowResultT:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Return a flow in progress as a partial FlowResult."""
|
|
|
|
if (flow := self._progress.get(flow_id)) is None:
|
|
|
|
raise UnknownFlow
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._async_flow_handler_to_flow_result([flow], False)[0]
|
2021-10-22 17:19:49 +00:00
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
@callback
|
2024-02-29 15:52:39 +00:00
|
|
|
def async_progress(self, include_uninitialized: bool = False) -> list[_FlowResultT]:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Return the flows in progress as a partial FlowResult."""
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._async_flow_handler_to_flow_result(
|
2021-10-22 17:19:49 +00:00
|
|
|
self._progress.values(), include_uninitialized
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_progress_by_handler(
|
2023-06-11 08:41:38 +00:00
|
|
|
self,
|
2024-03-07 11:41:14 +00:00
|
|
|
handler: _HandlerT,
|
2023-06-11 08:41:38 +00:00
|
|
|
include_uninitialized: bool = False,
|
|
|
|
match_context: dict[str, Any] | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> list[_FlowResultT]:
|
2023-06-11 08:41:38 +00:00
|
|
|
"""Return the flows in progress by handler as a partial FlowResult.
|
|
|
|
|
|
|
|
If match_context is specified, only return flows with a context that
|
|
|
|
is a superset of match_context.
|
|
|
|
"""
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._async_flow_handler_to_flow_result(
|
2023-06-11 08:41:38 +00:00
|
|
|
self._async_progress_by_handler(handler, match_context),
|
|
|
|
include_uninitialized,
|
2021-10-22 17:19:49 +00:00
|
|
|
)
|
|
|
|
|
2023-02-17 20:51:19 +00:00
|
|
|
@callback
|
|
|
|
def async_progress_by_init_data_type(
|
|
|
|
self,
|
|
|
|
init_data_type: type,
|
|
|
|
matcher: Callable[[Any], bool],
|
|
|
|
include_uninitialized: bool = False,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> list[_FlowResultT]:
|
2023-02-17 20:51:19 +00:00
|
|
|
"""Return flows in progress init matching by data type as a partial FlowResult."""
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._async_flow_handler_to_flow_result(
|
2024-09-22 00:18:53 +00:00
|
|
|
[
|
2023-09-20 09:55:51 +00:00
|
|
|
progress
|
2024-05-22 06:16:08 +00:00
|
|
|
for progress in self._init_data_process_index.get(init_data_type, ())
|
2023-09-20 09:55:51 +00:00
|
|
|
if matcher(progress.init_data)
|
2024-09-22 00:18:53 +00:00
|
|
|
],
|
2023-02-17 20:51:19 +00:00
|
|
|
include_uninitialized,
|
|
|
|
)
|
|
|
|
|
2021-10-22 17:19:49 +00:00
|
|
|
@callback
|
2023-06-11 08:41:38 +00:00
|
|
|
def _async_progress_by_handler(
|
2024-03-07 11:41:14 +00:00
|
|
|
self, handler: _HandlerT, match_context: dict[str, Any] | None
|
2024-10-08 10:18:45 +00:00
|
|
|
) -> list[FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]]:
|
2023-06-11 08:41:38 +00:00
|
|
|
"""Return the flows in progress by handler.
|
|
|
|
|
|
|
|
If match_context is specified, only return flows with a context that
|
|
|
|
is a superset of match_context.
|
|
|
|
"""
|
2023-09-20 09:55:51 +00:00
|
|
|
if not match_context:
|
2024-01-10 17:14:18 +00:00
|
|
|
return list(self._handler_progress_index.get(handler, ()))
|
2023-09-20 09:55:51 +00:00
|
|
|
match_context_items = match_context.items()
|
2019-07-31 19:25:30 +00:00
|
|
|
return [
|
2023-06-11 08:41:38 +00:00
|
|
|
progress
|
2024-01-10 17:14:18 +00:00
|
|
|
for progress in self._handler_progress_index.get(handler, ())
|
2023-09-20 09:55:51 +00:00
|
|
|
if match_context_items <= progress.context.items()
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
async def async_init(
|
2024-03-07 11:41:14 +00:00
|
|
|
self,
|
|
|
|
handler: _HandlerT,
|
|
|
|
*,
|
2024-10-08 10:18:45 +00:00
|
|
|
context: _FlowContextT | None = None,
|
2024-03-07 11:41:14 +00:00
|
|
|
data: Any = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Start a data entry flow."""
|
2019-05-27 02:48:27 +00:00
|
|
|
if context is None:
|
2024-10-08 10:18:45 +00:00
|
|
|
context = cast(_FlowContextT, {})
|
2021-04-15 17:13:42 +00:00
|
|
|
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
|
2022-01-22 21:29:16 +00:00
|
|
|
flow.flow_id = uuid_util.random_uuid_hex()
|
2021-04-15 17:13:42 +00:00
|
|
|
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
|
|
|
|
2023-01-17 14:26:17 +00:00
|
|
|
result = await self._async_handle_step(flow, flow.init_step, data)
|
|
|
|
|
|
|
|
if result["type"] != FlowResultType.ABORT:
|
|
|
|
await self.async_post_init(flow, result)
|
|
|
|
|
|
|
|
return result
|
2021-04-15 17:13:42 +00:00
|
|
|
|
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
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2024-01-16 08:04:27 +00:00
|
|
|
"""Continue a data entry flow."""
|
2024-02-29 15:52:39 +00:00
|
|
|
result: _FlowResultT | None = None
|
2024-05-04 18:17:21 +00:00
|
|
|
|
|
|
|
# Workaround for flow handlers which have not been upgraded to pass a show
|
|
|
|
# progress task, needed because of the change to eager tasks in HA Core 2024.5,
|
|
|
|
# can be removed in HA Core 2024.8.
|
|
|
|
flow = self._progress.get(flow_id)
|
|
|
|
if flow and flow.deprecated_show_progress:
|
|
|
|
if (cur_step := flow.cur_step) and cur_step[
|
|
|
|
"type"
|
|
|
|
] == FlowResultType.SHOW_PROGRESS:
|
|
|
|
# Allow the progress task to finish before we call the flow handler
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
2024-01-16 08:04:27 +00:00
|
|
|
while not result or result["type"] == FlowResultType.SHOW_PROGRESS_DONE:
|
|
|
|
result = await self._async_configure(flow_id, user_input)
|
2024-01-21 21:40:48 +00:00
|
|
|
flow = self._progress.get(flow_id)
|
|
|
|
if flow and flow.deprecated_show_progress:
|
|
|
|
break
|
2024-01-16 08:04:27 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
async def _async_configure(
|
|
|
|
self, flow_id: str, user_input: dict | None = None
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Continue a data entry 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
|
|
|
|
2022-12-15 07:45:54 +00:00
|
|
|
if (
|
|
|
|
data_schema := cur_step.get("data_schema")
|
|
|
|
) is not None and user_input is not None:
|
2024-06-26 09:30:07 +00:00
|
|
|
data_schema = cast(vol.Schema, data_schema)
|
2024-01-30 11:24:19 +00:00
|
|
|
try:
|
2024-07-02 19:57:09 +00:00
|
|
|
user_input = data_schema(user_input)
|
2024-01-30 11:24:19 +00:00
|
|
|
except vol.Invalid as ex:
|
|
|
|
raised_errors = [ex]
|
|
|
|
if isinstance(ex, vol.MultipleInvalid):
|
|
|
|
raised_errors = ex.errors
|
|
|
|
|
|
|
|
schema_errors: dict[str, Any] = {}
|
|
|
|
for error in raised_errors:
|
|
|
|
try:
|
|
|
|
_map_error_to_schema_errors(schema_errors, error, data_schema)
|
|
|
|
except ValueError:
|
|
|
|
# If we get here, the path in the exception does not exist in the schema.
|
|
|
|
schema_errors.setdefault("base", []).append(str(error))
|
|
|
|
raise InvalidData(
|
|
|
|
"Schema validation failed",
|
|
|
|
path=ex.path,
|
|
|
|
error_message=ex.error_message,
|
|
|
|
schema_errors=schema_errors,
|
|
|
|
) from ex
|
2019-05-10 12:33:50 +00:00
|
|
|
|
2022-03-16 21:14:50 +00:00
|
|
|
# Handle a menu navigation choice
|
2022-06-08 05:02:44 +00:00
|
|
|
if cur_step["type"] == FlowResultType.MENU and user_input:
|
2022-03-16 21:14:50 +00:00
|
|
|
result = await self._async_handle_step(
|
|
|
|
flow, user_input["next_step_id"], None
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
result = await self._async_handle_step(
|
|
|
|
flow, cur_step["step_id"], user_input
|
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2022-06-08 05:02:44 +00:00
|
|
|
if cur_step["type"] in (
|
|
|
|
FlowResultType.EXTERNAL_STEP,
|
|
|
|
FlowResultType.SHOW_PROGRESS,
|
|
|
|
):
|
|
|
|
if cur_step["type"] == FlowResultType.EXTERNAL_STEP and result[
|
|
|
|
"type"
|
|
|
|
] not in (
|
|
|
|
FlowResultType.EXTERNAL_STEP,
|
|
|
|
FlowResultType.EXTERNAL_STEP_DONE,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
raise ValueError(
|
|
|
|
"External step can only transition to "
|
|
|
|
"external step or external step done."
|
|
|
|
)
|
2022-06-08 05:02:44 +00:00
|
|
|
if cur_step["type"] == FlowResultType.SHOW_PROGRESS and result[
|
|
|
|
"type"
|
|
|
|
] not in (
|
|
|
|
FlowResultType.SHOW_PROGRESS,
|
|
|
|
FlowResultType.SHOW_PROGRESS_DONE,
|
2020-11-09 17:39:28 +00:00
|
|
|
):
|
|
|
|
raise ValueError(
|
2022-12-22 09:12:50 +00:00
|
|
|
"Show progress can only transition to show progress or show"
|
|
|
|
" progress done."
|
2020-11-09 17:39:28 +00:00
|
|
|
)
|
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
|
2023-10-02 12:15:54 +00:00
|
|
|
# the frontend. The result is considered to have changed if:
|
|
|
|
# - The step has changed
|
|
|
|
# - The step is same but result type is SHOW_PROGRESS and progress_action
|
|
|
|
# or description_placeholders has changed
|
|
|
|
if cur_step["step_id"] != result.get("step_id") or (
|
|
|
|
result["type"] == FlowResultType.SHOW_PROGRESS
|
|
|
|
and (
|
|
|
|
cur_step["progress_action"] != result.get("progress_action")
|
|
|
|
or cur_step["description_placeholders"]
|
|
|
|
!= result.get("description_placeholders")
|
|
|
|
)
|
2020-11-09 17:39:28 +00:00
|
|
|
):
|
2019-05-10 12:33:50 +00:00
|
|
|
# Tell frontend to reload the flow state.
|
2024-04-23 20:28:31 +00:00
|
|
|
self.hass.bus.async_fire_internal(
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
2024-03-07 11:41:14 +00:00
|
|
|
def _async_add_flow_progress(
|
2024-10-08 10:18:45 +00:00
|
|
|
self, flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]
|
2024-03-07 11:41:14 +00:00
|
|
|
) -> None:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Add a flow to in progress."""
|
2023-02-17 20:51:19 +00:00
|
|
|
if flow.init_data is not None:
|
2024-05-22 06:16:08 +00:00
|
|
|
self._init_data_process_index[type(flow.init_data)].add(flow)
|
2021-10-22 17:19:49 +00:00
|
|
|
self._progress[flow.flow_id] = flow
|
2024-05-22 06:16:08 +00:00
|
|
|
self._handler_progress_index[flow.handler].add(flow)
|
2021-10-22 17:19:49 +00:00
|
|
|
|
|
|
|
@callback
|
2024-03-07 11:41:14 +00:00
|
|
|
def _async_remove_flow_from_index(
|
2024-10-08 10:18:45 +00:00
|
|
|
self, flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]
|
2024-03-07 11:41:14 +00:00
|
|
|
) -> None:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Remove a flow from in progress."""
|
2023-02-17 20:51:19 +00:00
|
|
|
if flow.init_data is not None:
|
|
|
|
init_data_type = type(flow.init_data)
|
2023-09-20 09:55:51 +00:00
|
|
|
self._init_data_process_index[init_data_type].remove(flow)
|
2023-02-17 20:51:19 +00:00
|
|
|
if not self._init_data_process_index[init_data_type]:
|
|
|
|
del self._init_data_process_index[init_data_type]
|
2021-10-22 17:19:49 +00:00
|
|
|
handler = flow.handler
|
2023-09-20 09:55:51 +00:00
|
|
|
self._handler_progress_index[handler].remove(flow)
|
2021-10-22 17:19:49 +00:00
|
|
|
if not self._handler_progress_index[handler]:
|
|
|
|
del self._handler_progress_index[handler]
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2023-02-17 20:51:19 +00:00
|
|
|
@callback
|
|
|
|
def _async_remove_flow_progress(self, flow_id: str) -> None:
|
|
|
|
"""Remove a flow from in progress."""
|
|
|
|
if (flow := self._progress.pop(flow_id, None)) is None:
|
|
|
|
raise UnknownFlow
|
|
|
|
self._async_remove_flow_from_index(flow)
|
2024-01-11 11:00:12 +00:00
|
|
|
flow.async_cancel_progress_task()
|
2022-08-29 20:25:34 +00:00
|
|
|
try:
|
|
|
|
flow.async_remove()
|
2024-05-07 12:00:27 +00:00
|
|
|
except Exception:
|
2024-03-29 06:20:36 +00:00
|
|
|
_LOGGER.exception("Error removing %s flow", flow.handler)
|
2022-08-29 20:25:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def _async_handle_step(
|
2024-02-29 15:52:39 +00:00
|
|
|
self,
|
2024-10-08 10:18:45 +00:00
|
|
|
flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT],
|
2024-02-29 15:52:39 +00:00
|
|
|
step_id: str,
|
|
|
|
user_input: dict | BaseServiceInfo | None,
|
|
|
|
) -> _FlowResultT:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Handle a step of a flow."""
|
2023-10-19 11:34:10 +00:00
|
|
|
self._raise_if_step_does_not_exist(flow, step_id)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2023-10-19 11:34:10 +00:00
|
|
|
method = f"async_step_{step_id}"
|
2019-12-16 11:27:43 +00:00
|
|
|
try:
|
2024-02-29 15:52:39 +00:00
|
|
|
result: _FlowResultT = await getattr(flow, method)(user_input)
|
2019-12-16 11:27:43 +00:00
|
|
|
except AbortFlow as err:
|
2024-02-29 15:52:39 +00:00
|
|
|
result = self._flow_result(
|
|
|
|
type=FlowResultType.ABORT,
|
|
|
|
flow_id=flow.flow_id,
|
|
|
|
handler=flow.handler,
|
|
|
|
reason=err.reason,
|
|
|
|
description_placeholders=err.description_placeholders,
|
2019-12-16 11:27:43 +00:00
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2023-08-22 08:29:16 +00:00
|
|
|
# Setup the flow handler's preview if needed
|
|
|
|
if result.get("preview") is not None:
|
|
|
|
await self._async_setup_preview(flow)
|
|
|
|
|
2022-06-08 05:02:44 +00:00
|
|
|
if not isinstance(result["type"], FlowResultType):
|
|
|
|
result["type"] = FlowResultType(result["type"]) # type: ignore[unreachable]
|
2024-11-07 17:23:35 +00:00
|
|
|
report_usage(
|
2024-11-23 17:58:24 +00:00
|
|
|
"does not use FlowResultType enum for data entry flow result type",
|
2024-11-07 17:23:35 +00:00
|
|
|
core_behavior=ReportBehavior.LOG,
|
2024-11-23 17:58:24 +00:00
|
|
|
breaks_in_ha_version="2025.1",
|
2022-06-08 05:02:44 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2024-01-11 11:00:12 +00:00
|
|
|
if (
|
|
|
|
result["type"] == FlowResultType.SHOW_PROGRESS
|
2024-02-29 15:52:39 +00:00
|
|
|
# Mypy does not agree with using pop on _FlowResultT
|
|
|
|
and (progress_task := result.pop("progress_task", None)) # type: ignore[arg-type]
|
2024-01-11 11:00:12 +00:00
|
|
|
and progress_task != flow.async_get_progress_task()
|
|
|
|
):
|
|
|
|
# The flow's progress task was changed, register a callback on it
|
|
|
|
async def call_configure() -> None:
|
|
|
|
with suppress(UnknownFlow):
|
2024-01-16 08:04:27 +00:00
|
|
|
await self._async_configure(flow.flow_id)
|
2024-01-11 11:00:12 +00:00
|
|
|
|
|
|
|
def schedule_configure(_: asyncio.Task) -> None:
|
|
|
|
self.hass.async_create_task(call_configure())
|
|
|
|
|
2024-02-29 15:52:39 +00:00
|
|
|
# The mypy ignores are a consequence of mypy not accepting the pop above
|
|
|
|
progress_task.add_done_callback(schedule_configure) # type: ignore[attr-defined]
|
|
|
|
flow.async_set_progress_task(progress_task) # type: ignore[arg-type]
|
2024-01-11 11:00:12 +00:00
|
|
|
|
|
|
|
elif result["type"] != FlowResultType.SHOW_PROGRESS:
|
|
|
|
flow.async_cancel_progress_task()
|
|
|
|
|
2024-01-11 20:05:20 +00:00
|
|
|
if result["type"] in STEP_ID_OPTIONAL_STEPS:
|
|
|
|
if "step_id" not in result:
|
|
|
|
result["step_id"] = step_id
|
|
|
|
|
2023-12-14 16:32:14 +00:00
|
|
|
if result["type"] in FLOW_NOT_COMPLETE_STEPS:
|
2023-10-19 11:34:10 +00:00
|
|
|
self._raise_if_step_does_not_exist(flow, result["step_id"])
|
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
|
2022-06-08 05:02:44 +00:00
|
|
|
if result["type"] == FlowResultType.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
|
|
|
|
|
2024-02-29 15:52:39 +00:00
|
|
|
def _raise_if_step_does_not_exist(
|
2024-10-08 10:18:45 +00:00
|
|
|
self, flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT], step_id: str
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> None:
|
2023-10-19 11:34:10 +00:00
|
|
|
"""Raise if the step does not exist."""
|
|
|
|
method = f"async_step_{step_id}"
|
|
|
|
|
|
|
|
if not hasattr(flow, method):
|
|
|
|
self._async_remove_flow_progress(flow.flow_id)
|
|
|
|
raise UnknownStep(
|
|
|
|
f"Handler {self.__class__.__name__} doesn't support step {step_id}"
|
|
|
|
)
|
|
|
|
|
2024-03-07 11:41:14 +00:00
|
|
|
async def _async_setup_preview(
|
2024-10-08 10:18:45 +00:00
|
|
|
self, flow: FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]
|
2024-03-07 11:41:14 +00:00
|
|
|
) -> None:
|
2023-08-22 08:29:16 +00:00
|
|
|
"""Set up preview for a flow handler."""
|
|
|
|
if flow.handler not in self._preview:
|
|
|
|
self._preview.add(flow.handler)
|
2023-08-24 09:59:24 +00:00
|
|
|
await flow.async_setup_preview(self.hass)
|
2023-08-22 08:29:16 +00:00
|
|
|
|
2024-02-29 15:52:39 +00:00
|
|
|
@callback
|
|
|
|
def _async_flow_handler_to_flow_result(
|
2024-03-07 11:41:14 +00:00
|
|
|
self,
|
2024-10-08 10:18:45 +00:00
|
|
|
flows: Iterable[FlowHandler[_FlowContextT, _FlowResultT, _HandlerT]],
|
2024-03-07 11:41:14 +00:00
|
|
|
include_uninitialized: bool,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> list[_FlowResultT]:
|
|
|
|
"""Convert a list of FlowHandler to a partial FlowResult that can be serialized."""
|
2024-05-28 01:39:59 +00:00
|
|
|
return [
|
|
|
|
self._flow_result(
|
2024-02-29 15:52:39 +00:00
|
|
|
flow_id=flow.flow_id,
|
|
|
|
handler=flow.handler,
|
|
|
|
context=flow.context,
|
2024-05-28 01:39:59 +00:00
|
|
|
step_id=flow.cur_step["step_id"],
|
2024-02-29 15:52:39 +00:00
|
|
|
)
|
2024-05-28 01:39:59 +00:00
|
|
|
if flow.cur_step
|
|
|
|
else self._flow_result(
|
|
|
|
flow_id=flow.flow_id,
|
|
|
|
handler=flow.handler,
|
|
|
|
context=flow.context,
|
|
|
|
)
|
|
|
|
for flow in flows
|
|
|
|
if include_uninitialized or flow.cur_step is not None
|
|
|
|
]
|
2024-02-29 15:52:39 +00:00
|
|
|
|
|
|
|
|
2024-10-08 10:18:45 +00:00
|
|
|
class FlowHandler(Generic[_FlowContextT, _FlowResultT, _HandlerT]):
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Handle a data entry flow."""
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2024-03-27 11:32:29 +00:00
|
|
|
_flow_result: type[_FlowResultT] = FlowResult # type: ignore[assignment]
|
2024-02-29 15:52:39 +00:00
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
# Set by flow manager
|
2024-02-29 15:52:39 +00:00
|
|
|
cur_step: _FlowResultT | 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.
|
2022-02-18 07:09:22 +00:00
|
|
|
flow_id: str = None # type: ignore[assignment]
|
|
|
|
hass: HomeAssistant = None # type: ignore[assignment]
|
2024-03-07 11:41:14 +00:00
|
|
|
handler: _HandlerT = None # type: ignore[assignment]
|
2021-02-13 12:21:37 +00:00
|
|
|
# Ensure the attribute has a subscriptable, but immutable, default value.
|
2024-10-08 10:18:45 +00:00
|
|
|
context: _FlowContextT = MappingProxyType({}) # type: ignore[assignment]
|
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
|
2023-12-12 07:44:35 +00:00
|
|
|
MINOR_VERSION = 1
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2024-01-11 11:00:12 +00:00
|
|
|
__progress_task: asyncio.Task[Any] | None = None
|
2024-01-13 10:56:05 +00:00
|
|
|
__no_progress_task_reported = False
|
2024-01-21 21:40:48 +00:00
|
|
|
deprecated_show_progress = False
|
2024-01-11 11:00:12 +00:00
|
|
|
|
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."""
|
2024-10-08 10:18:45 +00:00
|
|
|
return self.context.get("source", None) # type: ignore[return-value]
|
2020-04-24 16:31:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def show_advanced_options(self) -> bool:
|
|
|
|
"""If we should show advanced options."""
|
2024-10-08 10:18:45 +00:00
|
|
|
return self.context.get("show_advanced_options", False) # type: ignore[return-value]
|
2020-04-24 16:31:56 +00:00
|
|
|
|
2022-11-29 09:16:01 +00:00
|
|
|
def add_suggested_values_to_schema(
|
2023-01-16 10:36:21 +00:00
|
|
|
self, data_schema: vol.Schema, suggested_values: Mapping[str, Any] | None
|
2022-11-29 09:16:01 +00:00
|
|
|
) -> vol.Schema:
|
|
|
|
"""Make a copy of the schema, populated with suggested values.
|
|
|
|
|
|
|
|
For each schema marker matching items in `suggested_values`,
|
|
|
|
the `suggested_value` will be set. The existing `suggested_value` will
|
|
|
|
be left untouched if there is no matching item.
|
|
|
|
"""
|
|
|
|
schema = {}
|
|
|
|
for key, val in data_schema.schema.items():
|
|
|
|
if isinstance(key, vol.Marker):
|
|
|
|
# Exclude advanced field
|
|
|
|
if (
|
|
|
|
key.description
|
|
|
|
and key.description.get("advanced")
|
|
|
|
and not self.show_advanced_options
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
|
|
|
new_key = key
|
2023-01-16 10:36:21 +00:00
|
|
|
if (
|
|
|
|
suggested_values
|
|
|
|
and key in suggested_values
|
|
|
|
and isinstance(key, vol.Marker)
|
|
|
|
):
|
2022-11-29 09:16:01 +00:00
|
|
|
# Copy the marker to not modify the flow schema
|
|
|
|
new_key = copy.copy(key)
|
2024-06-26 09:30:07 +00:00
|
|
|
new_key.description = {"suggested_value": suggested_values[key.schema]}
|
2022-11-29 09:16:01 +00:00
|
|
|
schema[new_key] = val
|
|
|
|
return vol.Schema(schema)
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_show_form(
|
|
|
|
self,
|
|
|
|
*,
|
2024-01-15 08:37:57 +00:00
|
|
|
step_id: str | None = None,
|
2022-03-04 15:42:02 +00:00
|
|
|
data_schema: vol.Schema | None = None,
|
2021-04-15 17:17:07 +00:00
|
|
|
errors: dict[str, str] | None = None,
|
2024-11-15 13:39:57 +00:00
|
|
|
description_placeholders: Mapping[str, str] | None = None,
|
2021-04-23 18:02:12 +00:00
|
|
|
last_step: bool | None = None,
|
2023-08-22 08:29:16 +00:00
|
|
|
preview: str | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2024-01-15 08:37:57 +00:00
|
|
|
"""Return the definition of a form to gather user input.
|
|
|
|
|
|
|
|
The step_id parameter is deprecated and will be removed in a future release.
|
|
|
|
"""
|
2024-02-29 15:52:39 +00:00
|
|
|
flow_result = self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.FORM,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
data_schema=data_schema,
|
|
|
|
errors=errors,
|
|
|
|
description_placeholders=description_placeholders,
|
|
|
|
last_step=last_step, # Display next or submit button in frontend
|
2023-08-22 08:29:16 +00:00
|
|
|
preview=preview, # Display preview component in frontend
|
2022-06-16 10:57:41 +00:00
|
|
|
)
|
2024-01-15 08:37:57 +00:00
|
|
|
if step_id is not None:
|
|
|
|
flow_result["step_id"] = step_id
|
|
|
|
return flow_result
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_create_entry(
|
|
|
|
self,
|
|
|
|
*,
|
2022-12-09 09:24:08 +00:00
|
|
|
title: str | None = None,
|
2021-04-11 14:56:33 +00:00
|
|
|
data: Mapping[str, Any],
|
2021-03-17 16:34:55 +00:00
|
|
|
description: str | None = None,
|
2022-05-31 08:33:34 +00:00
|
|
|
description_placeholders: Mapping[str, str] | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Finish flow."""
|
2024-02-29 15:52:39 +00:00
|
|
|
flow_result = self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.CREATE_ENTRY,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
data=data,
|
|
|
|
description=description,
|
|
|
|
description_placeholders=description_placeholders,
|
2022-08-29 23:28:42 +00:00
|
|
|
context=self.context,
|
2022-06-16 10:57:41 +00:00
|
|
|
)
|
2022-12-09 09:24:08 +00:00
|
|
|
if title is not None:
|
|
|
|
flow_result["title"] = title
|
|
|
|
return flow_result
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_abort(
|
2022-05-31 08:33:34 +00:00
|
|
|
self,
|
|
|
|
*,
|
|
|
|
reason: str,
|
|
|
|
description_placeholders: Mapping[str, str] | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Abort the flow."""
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._flow_result(
|
|
|
|
type=FlowResultType.ABORT,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
reason=reason,
|
|
|
|
description_placeholders=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(
|
2022-05-31 08:33:34 +00:00
|
|
|
self,
|
|
|
|
*,
|
2024-01-15 08:37:57 +00:00
|
|
|
step_id: str | None = None,
|
2022-05-31 08:33:34 +00:00
|
|
|
url: str,
|
|
|
|
description_placeholders: Mapping[str, str] | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2024-01-15 08:37:57 +00:00
|
|
|
"""Return the definition of an external step for the user to take.
|
|
|
|
|
|
|
|
The step_id parameter is deprecated and will be removed in a future release.
|
|
|
|
"""
|
2024-02-29 15:52:39 +00:00
|
|
|
flow_result = self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.EXTERNAL_STEP,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
url=url,
|
|
|
|
description_placeholders=description_placeholders,
|
|
|
|
)
|
2024-01-15 08:37:57 +00:00
|
|
|
if step_id is not None:
|
|
|
|
flow_result["step_id"] = step_id
|
|
|
|
return flow_result
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
@callback
|
2024-02-29 15:52:39 +00:00
|
|
|
def async_external_step_done(self, *, next_step_id: str) -> _FlowResultT:
|
2019-05-10 12:33:50 +00:00
|
|
|
"""Return the definition of an external step for the user to take."""
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.EXTERNAL_STEP_DONE,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
step_id=next_step_id,
|
|
|
|
)
|
2019-12-16 11:27:43 +00:00
|
|
|
|
2020-11-09 17:39:28 +00:00
|
|
|
@callback
|
|
|
|
def async_show_progress(
|
|
|
|
self,
|
|
|
|
*,
|
2024-01-11 20:05:20 +00:00
|
|
|
step_id: str | None = None,
|
2020-11-09 17:39:28 +00:00
|
|
|
progress_action: str,
|
2022-05-31 08:33:34 +00:00
|
|
|
description_placeholders: Mapping[str, str] | None = None,
|
2024-01-11 11:00:12 +00:00
|
|
|
progress_task: asyncio.Task[Any] | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2024-01-15 08:37:57 +00:00
|
|
|
"""Show a progress message to the user, without user input allowed.
|
|
|
|
|
|
|
|
The step_id parameter is deprecated and will be removed in a future release.
|
|
|
|
"""
|
2024-01-13 10:56:05 +00:00
|
|
|
if progress_task is None and not self.__no_progress_task_reported:
|
|
|
|
self.__no_progress_task_reported = True
|
|
|
|
cls = self.__class__
|
|
|
|
report_issue = async_suggest_report_issue(self.hass, module=cls.__module__)
|
|
|
|
_LOGGER.warning(
|
|
|
|
(
|
|
|
|
"%s::%s calls async_show_progress without passing a progress task, "
|
|
|
|
"this is not valid and will break in Home Assistant Core 2024.8. "
|
|
|
|
"Please %s"
|
|
|
|
),
|
|
|
|
cls.__module__,
|
|
|
|
cls.__name__,
|
|
|
|
report_issue,
|
|
|
|
)
|
|
|
|
|
2024-01-21 21:40:48 +00:00
|
|
|
if progress_task is None:
|
|
|
|
self.deprecated_show_progress = True
|
|
|
|
|
2024-02-29 15:52:39 +00:00
|
|
|
flow_result = self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.SHOW_PROGRESS,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
progress_action=progress_action,
|
|
|
|
description_placeholders=description_placeholders,
|
2024-01-11 11:00:12 +00:00
|
|
|
progress_task=progress_task,
|
2022-06-16 10:57:41 +00:00
|
|
|
)
|
2024-01-11 20:05:20 +00:00
|
|
|
if step_id is not None:
|
2024-01-15 08:37:57 +00:00
|
|
|
flow_result["step_id"] = step_id
|
|
|
|
return flow_result
|
2020-11-09 17:39:28 +00:00
|
|
|
|
|
|
|
@callback
|
2024-02-29 15:52:39 +00:00
|
|
|
def async_show_progress_done(self, *, next_step_id: str) -> _FlowResultT:
|
2020-11-09 17:39:28 +00:00
|
|
|
"""Mark the progress done."""
|
2024-02-29 15:52:39 +00:00
|
|
|
return self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.SHOW_PROGRESS_DONE,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
step_id=next_step_id,
|
|
|
|
)
|
2020-11-09 17:39:28 +00:00
|
|
|
|
2022-03-16 21:14:50 +00:00
|
|
|
@callback
|
|
|
|
def async_show_menu(
|
|
|
|
self,
|
|
|
|
*,
|
2024-01-15 08:37:57 +00:00
|
|
|
step_id: str | None = None,
|
2024-04-12 02:14:37 +00:00
|
|
|
menu_options: Container[str],
|
2022-05-31 08:33:34 +00:00
|
|
|
description_placeholders: Mapping[str, str] | None = None,
|
2024-02-29 15:52:39 +00:00
|
|
|
) -> _FlowResultT:
|
2022-03-16 21:14:50 +00:00
|
|
|
"""Show a navigation menu to the user.
|
|
|
|
|
|
|
|
Options dict maps step_id => i18n label
|
2024-01-15 08:37:57 +00:00
|
|
|
The step_id parameter is deprecated and will be removed in a future release.
|
2022-03-16 21:14:50 +00:00
|
|
|
"""
|
2024-02-29 15:52:39 +00:00
|
|
|
flow_result = self._flow_result(
|
2022-06-16 10:57:41 +00:00
|
|
|
type=FlowResultType.MENU,
|
|
|
|
flow_id=self.flow_id,
|
|
|
|
handler=self.handler,
|
|
|
|
data_schema=vol.Schema({"next_step_id": vol.In(menu_options)}),
|
|
|
|
menu_options=menu_options,
|
|
|
|
description_placeholders=description_placeholders,
|
|
|
|
)
|
2024-01-15 08:37:57 +00:00
|
|
|
if step_id is not None:
|
|
|
|
flow_result["step_id"] = step_id
|
|
|
|
return flow_result
|
2022-03-16 21:14:50 +00:00
|
|
|
|
2022-08-29 20:25:34 +00:00
|
|
|
@callback
|
|
|
|
def async_remove(self) -> None:
|
2023-01-17 14:26:17 +00:00
|
|
|
"""Notification that the flow has been removed."""
|
2022-08-29 20:25:34 +00:00
|
|
|
|
2023-08-22 08:29:16 +00:00
|
|
|
@staticmethod
|
2023-08-24 09:59:24 +00:00
|
|
|
async def async_setup_preview(hass: HomeAssistant) -> None:
|
2023-08-22 08:29:16 +00:00
|
|
|
"""Set up preview."""
|
|
|
|
|
2024-01-11 11:00:12 +00:00
|
|
|
@callback
|
|
|
|
def async_cancel_progress_task(self) -> None:
|
|
|
|
"""Cancel in progress task."""
|
|
|
|
if self.__progress_task and not self.__progress_task.done():
|
|
|
|
self.__progress_task.cancel()
|
|
|
|
self.__progress_task = None
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_get_progress_task(self) -> asyncio.Task[Any] | None:
|
|
|
|
"""Get in progress task."""
|
|
|
|
return self.__progress_task
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_set_progress_task(
|
|
|
|
self,
|
|
|
|
progress_task: asyncio.Task[Any],
|
|
|
|
) -> None:
|
|
|
|
"""Set in progress task."""
|
|
|
|
self.__progress_task = progress_task
|
|
|
|
|
2019-12-16 11:27:43 +00:00
|
|
|
|
2024-06-25 09:02:00 +00:00
|
|
|
class SectionConfig(TypedDict, total=False):
|
|
|
|
"""Class to represent a section config."""
|
|
|
|
|
|
|
|
collapsed: bool
|
|
|
|
|
|
|
|
|
|
|
|
class section:
|
|
|
|
"""Data entry flow section."""
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional("collapsed", default=False): bool,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, schema: vol.Schema, options: SectionConfig | None = None
|
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
|
|
|
self.schema = schema
|
|
|
|
self.options: SectionConfig = self.CONFIG_SCHEMA(options or {})
|
|
|
|
|
|
|
|
def __call__(self, value: Any) -> Any:
|
|
|
|
"""Validate input."""
|
|
|
|
return self.schema(value)
|