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.humidifier import HumidifierDeviceClass
12 from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
13 from homeassistant.const import CONF_NAME, PERCENTAGE
14 from homeassistant.helpers import selector
16  SchemaConfigFlowHandler,
17  SchemaFlowFormStep,
18 )
19 
20 from . import (
21  CONF_DEVICE_CLASS,
22  CONF_DRY_TOLERANCE,
23  CONF_HUMIDIFIER,
24  CONF_MIN_DUR,
25  CONF_SENSOR,
26  CONF_WET_TOLERANCE,
27  DEFAULT_TOLERANCE,
28  DOMAIN,
29 )
30 
31 OPTIONS_SCHEMA = {
32  vol.Required(CONF_DEVICE_CLASS): selector.SelectSelector(
33  selector.SelectSelectorConfig(
34  options=[
35  HumidifierDeviceClass.HUMIDIFIER,
36  HumidifierDeviceClass.DEHUMIDIFIER,
37  ],
38  translation_key=CONF_DEVICE_CLASS,
39  mode=selector.SelectSelectorMode.DROPDOWN,
40  ),
41  ),
42  vol.Required(CONF_SENSOR): selector.EntitySelector(
43  selector.EntitySelectorConfig(
44  domain=SENSOR_DOMAIN, device_class=SensorDeviceClass.HUMIDITY
45  )
46  ),
47  vol.Required(CONF_HUMIDIFIER): selector.EntitySelector(
48  selector.EntitySelectorConfig(domain=[switch.DOMAIN, fan.DOMAIN])
49  ),
50  vol.Required(
51  CONF_DRY_TOLERANCE, default=DEFAULT_TOLERANCE
52  ): selector.NumberSelector(
53  selector.NumberSelectorConfig(
54  min=0,
55  max=100,
56  step=0.5,
57  unit_of_measurement=PERCENTAGE,
58  mode=selector.NumberSelectorMode.BOX,
59  )
60  ),
61  vol.Required(
62  CONF_WET_TOLERANCE, default=DEFAULT_TOLERANCE
63  ): selector.NumberSelector(
64  selector.NumberSelectorConfig(
65  min=0,
66  max=100,
67  step=0.5,
68  unit_of_measurement=PERCENTAGE,
69  mode=selector.NumberSelectorMode.BOX,
70  )
71  ),
72  vol.Optional(CONF_MIN_DUR): selector.DurationSelector(
73  selector.DurationSelectorConfig(allow_negative=False)
74  ),
75 }
76 
77 CONFIG_SCHEMA = {
78  vol.Required(CONF_NAME): selector.TextSelector(),
79  **OPTIONS_SCHEMA,
80 }
81 
82 
83 CONFIG_FLOW = {
84  "user": SchemaFlowFormStep(vol.Schema(CONFIG_SCHEMA)),
85 }
86 
87 OPTIONS_FLOW = {
88  "init": SchemaFlowFormStep(vol.Schema(OPTIONS_SCHEMA)),
89 }
90 
91 
93  """Handle a config or options flow."""
94 
95  config_flow = CONFIG_FLOW
96  options_flow = OPTIONS_FLOW
97 
98  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
99  """Return config entry title."""
100  return cast(str, options["name"])