Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """The history_stats component config flow."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any, cast
7 
8 import voluptuous as vol
9 
10 from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE
12  SchemaCommonFlowHandler,
13  SchemaConfigFlowHandler,
14  SchemaFlowError,
15  SchemaFlowFormStep,
16 )
18  DurationSelector,
19  DurationSelectorConfig,
20  EntitySelector,
21  SelectSelector,
22  SelectSelectorConfig,
23  SelectSelectorMode,
24  TemplateSelector,
25  TextSelector,
26  TextSelectorConfig,
27 )
28 
29 from .const import (
30  CONF_DURATION,
31  CONF_END,
32  CONF_PERIOD_KEYS,
33  CONF_START,
34  CONF_TYPE_KEYS,
35  CONF_TYPE_TIME,
36  DEFAULT_NAME,
37  DOMAIN,
38 )
39 
40 
41 async def validate_options(
42  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
43 ) -> dict[str, Any]:
44  """Validate options selected."""
45  if sum(param in user_input for param in CONF_PERIOD_KEYS) != 2:
46  raise SchemaFlowError("only_two_keys_allowed")
47 
48  handler.parent_handler._async_abort_entries_match({**handler.options, **user_input}) # noqa: SLF001
49 
50  return user_input
51 
52 
53 DATA_SCHEMA_SETUP = vol.Schema(
54  {
55  vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
56  vol.Required(CONF_ENTITY_ID): EntitySelector(),
57  vol.Required(CONF_STATE): TextSelector(TextSelectorConfig(multiple=True)),
58  vol.Required(CONF_TYPE, default=CONF_TYPE_TIME): SelectSelector(
60  options=CONF_TYPE_KEYS,
61  mode=SelectSelectorMode.DROPDOWN,
62  translation_key=CONF_TYPE,
63  )
64  ),
65  }
66 )
67 DATA_SCHEMA_OPTIONS = vol.Schema(
68  {
69  vol.Optional(CONF_START): TemplateSelector(),
70  vol.Optional(CONF_END): TemplateSelector(),
71  vol.Optional(CONF_DURATION): DurationSelector(
72  DurationSelectorConfig(enable_day=True, allow_negative=False)
73  ),
74  }
75 )
76 
77 CONFIG_FLOW = {
78  "user": SchemaFlowFormStep(
79  schema=DATA_SCHEMA_SETUP,
80  next_step="options",
81  ),
82  "options": SchemaFlowFormStep(
83  schema=DATA_SCHEMA_OPTIONS,
84  validate_user_input=validate_options,
85  ),
86 }
87 OPTIONS_FLOW = {
88  "init": SchemaFlowFormStep(
89  DATA_SCHEMA_OPTIONS,
90  validate_user_input=validate_options,
91  ),
92 }
93 
94 
96  """Handle a config flow for History stats."""
97 
98  config_flow = CONFIG_FLOW
99  options_flow = OPTIONS_FLOW
100 
101  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
102  """Return config entry title."""
103  return cast(str, options[CONF_NAME])
dict[str, Any] validate_options(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:43