Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Min/Max integration."""
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.components.input_number import DOMAIN as INPUT_NUMBER_DOMAIN
11 from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
12 from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
13 from homeassistant.const import CONF_TYPE
14 from homeassistant.helpers import selector
16  SchemaConfigFlowHandler,
17  SchemaFlowFormStep,
18 )
19 
20 from .const import CONF_ENTITY_IDS, CONF_ROUND_DIGITS, DOMAIN
21 
22 _STATISTIC_MEASURES = [
23  "min",
24  "max",
25  "mean",
26  "median",
27  "last",
28  "range",
29  "sum",
30 ]
31 
32 
33 OPTIONS_SCHEMA = vol.Schema(
34  {
35  vol.Required(CONF_ENTITY_IDS): selector.EntitySelector(
36  selector.EntitySelectorConfig(
37  domain=[SENSOR_DOMAIN, NUMBER_DOMAIN, INPUT_NUMBER_DOMAIN],
38  multiple=True,
39  ),
40  ),
41  vol.Required(CONF_TYPE): selector.SelectSelector(
42  selector.SelectSelectorConfig(
43  options=_STATISTIC_MEASURES, translation_key=CONF_TYPE
44  ),
45  ),
46  vol.Required(CONF_ROUND_DIGITS, default=2): selector.NumberSelector(
47  selector.NumberSelectorConfig(
48  min=0, max=6, mode=selector.NumberSelectorMode.BOX
49  ),
50  ),
51  }
52 )
53 
54 CONFIG_SCHEMA = vol.Schema(
55  {
56  vol.Required("name"): selector.TextSelector(),
57  }
58 ).extend(OPTIONS_SCHEMA.schema)
59 
60 CONFIG_FLOW = {
61  "user": SchemaFlowFormStep(CONFIG_SCHEMA),
62 }
63 
64 OPTIONS_FLOW = {
65  "init": SchemaFlowFormStep(OPTIONS_SCHEMA),
66 }
67 
68 
70  """Handle a config or options flow for Min/Max."""
71 
72  config_flow = CONFIG_FLOW
73  options_flow = OPTIONS_FLOW
74 
75  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
76  """Return config entry title."""
77  return cast(str, options["name"]) if "name" in options else ""
str async_config_entry_title(self, Mapping[str, Any] options)
Definition: config_flow.py:75