Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Generic hygrostat."""
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 import fan, switch
11 from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
12 from homeassistant.const import CONF_NAME, DEGREE
13 from homeassistant.helpers import selector
15  SchemaConfigFlowHandler,
16  SchemaFlowFormStep,
17 )
18 
19 from .const import (
20  CONF_AC_MODE,
21  CONF_COLD_TOLERANCE,
22  CONF_HEATER,
23  CONF_HOT_TOLERANCE,
24  CONF_MIN_DUR,
25  CONF_PRESETS,
26  CONF_SENSOR,
27  DEFAULT_TOLERANCE,
28  DOMAIN,
29 )
30 
31 OPTIONS_SCHEMA = {
32  vol.Required(CONF_AC_MODE): selector.BooleanSelector(
33  selector.BooleanSelectorConfig(),
34  ),
35  vol.Required(CONF_SENSOR): selector.EntitySelector(
36  selector.EntitySelectorConfig(
37  domain=SENSOR_DOMAIN, device_class=SensorDeviceClass.TEMPERATURE
38  )
39  ),
40  vol.Required(CONF_HEATER): selector.EntitySelector(
41  selector.EntitySelectorConfig(domain=[fan.DOMAIN, switch.DOMAIN])
42  ),
43  vol.Required(
44  CONF_COLD_TOLERANCE, default=DEFAULT_TOLERANCE
45  ): selector.NumberSelector(
46  selector.NumberSelectorConfig(
47  mode=selector.NumberSelectorMode.BOX, unit_of_measurement=DEGREE, step=0.1
48  )
49  ),
50  vol.Required(
51  CONF_HOT_TOLERANCE, default=DEFAULT_TOLERANCE
52  ): selector.NumberSelector(
53  selector.NumberSelectorConfig(
54  mode=selector.NumberSelectorMode.BOX, unit_of_measurement=DEGREE, step=0.1
55  )
56  ),
57  vol.Optional(CONF_MIN_DUR): selector.DurationSelector(
58  selector.DurationSelectorConfig(allow_negative=False)
59  ),
60 }
61 
62 PRESETS_SCHEMA = {
63  vol.Optional(v): selector.NumberSelector(
64  selector.NumberSelectorConfig(
65  mode=selector.NumberSelectorMode.BOX, unit_of_measurement=DEGREE, step=0.1
66  )
67  )
68  for v in CONF_PRESETS.values()
69 }
70 
71 CONFIG_SCHEMA = {
72  vol.Required(CONF_NAME): selector.TextSelector(),
73  **OPTIONS_SCHEMA,
74 }
75 
76 
77 CONFIG_FLOW = {
78  "user": SchemaFlowFormStep(vol.Schema(CONFIG_SCHEMA), next_step="presets"),
79  "presets": SchemaFlowFormStep(vol.Schema(PRESETS_SCHEMA)),
80 }
81 
82 OPTIONS_FLOW = {
83  "init": SchemaFlowFormStep(vol.Schema(OPTIONS_SCHEMA), next_step="presets"),
84  "presets": SchemaFlowFormStep(vol.Schema(PRESETS_SCHEMA)),
85 }
86 
87 
89  """Handle a config or options flow."""
90 
91  config_flow = CONFIG_FLOW
92  options_flow = OPTIONS_FLOW
93 
94  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
95  """Return config entry title."""
96  return cast(str, options["name"])