Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Trend 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.sensor import DOMAIN as SENSOR_DOMAIN
11 from homeassistant.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_NAME, UnitOfTime
12 from homeassistant.helpers import selector
14  SchemaCommonFlowHandler,
15  SchemaConfigFlowHandler,
16  SchemaFlowFormStep,
17 )
18 
19 from .const import (
20  CONF_INVERT,
21  CONF_MAX_SAMPLES,
22  CONF_MIN_GRADIENT,
23  CONF_MIN_SAMPLES,
24  CONF_SAMPLE_DURATION,
25  DEFAULT_MAX_SAMPLES,
26  DEFAULT_MIN_GRADIENT,
27  DEFAULT_MIN_SAMPLES,
28  DEFAULT_SAMPLE_DURATION,
29  DOMAIN,
30 )
31 
32 
33 async def get_base_options_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
34  """Get base options schema."""
35  return vol.Schema(
36  {
37  vol.Optional(CONF_ATTRIBUTE): selector.AttributeSelector(
38  selector.AttributeSelectorConfig(
39  entity_id=handler.options[CONF_ENTITY_ID]
40  )
41  ),
42  vol.Optional(CONF_INVERT, default=False): selector.BooleanSelector(),
43  }
44  )
45 
46 
47 async def get_extended_options_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
48  """Get extended options schema."""
49  return (await get_base_options_schema(handler)).extend(
50  {
51  vol.Optional(
52  CONF_MAX_SAMPLES, default=DEFAULT_MAX_SAMPLES
53  ): selector.NumberSelector(
54  selector.NumberSelectorConfig(
55  min=2,
56  mode=selector.NumberSelectorMode.BOX,
57  ),
58  ),
59  vol.Optional(
60  CONF_MIN_SAMPLES, default=DEFAULT_MIN_SAMPLES
61  ): selector.NumberSelector(
62  selector.NumberSelectorConfig(
63  min=2,
64  mode=selector.NumberSelectorMode.BOX,
65  ),
66  ),
67  vol.Optional(
68  CONF_MIN_GRADIENT, default=DEFAULT_MIN_GRADIENT
69  ): selector.NumberSelector(
70  selector.NumberSelectorConfig(
71  step="any",
72  mode=selector.NumberSelectorMode.BOX,
73  ),
74  ),
75  vol.Optional(
76  CONF_SAMPLE_DURATION, default=DEFAULT_SAMPLE_DURATION
77  ): selector.NumberSelector(
78  selector.NumberSelectorConfig(
79  min=0,
80  mode=selector.NumberSelectorMode.BOX,
81  unit_of_measurement=UnitOfTime.SECONDS,
82  ),
83  ),
84  }
85  )
86 
87 
88 CONFIG_SCHEMA = vol.Schema(
89  {
90  vol.Required(CONF_NAME): selector.TextSelector(),
91  vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
92  selector.EntitySelectorConfig(domain=SENSOR_DOMAIN, multiple=False),
93  ),
94  }
95 )
96 
97 
99  """Handle a config or options flow for Trend."""
100 
101  config_flow = {
102  "user": SchemaFlowFormStep(schema=CONFIG_SCHEMA, next_step="settings"),
103  "settings": SchemaFlowFormStep(get_base_options_schema),
104  }
105  options_flow = {
106  "init": SchemaFlowFormStep(get_extended_options_schema),
107  }
108 
109  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
110  """Return config entry title."""
111  return cast(str, options[CONF_NAME])
str async_config_entry_title(self, Mapping[str, Any] options)
Definition: config_flow.py:109
vol.Schema get_extended_options_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:47
vol.Schema get_base_options_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:33