1 """Config flow for Random helper."""
3 from collections.abc
import Callable, Coroutine, Mapping
4 from enum
import StrEnum
5 from typing
import Any, cast
7 import voluptuous
as vol
16 CONF_UNIT_OF_MEASUREMENT,
22 SchemaCommonFlowHandler,
23 SchemaConfigFlowHandler,
34 from .const
import DOMAIN
35 from .sensor
import DEFAULT_MAX, DEFAULT_MIN
44 """Generate schema."""
45 schema: dict[vol.Marker, Any] = {}
47 if flow_type == _FlowType.CONFIG:
50 if domain == Platform.BINARY_SENSOR:
53 options=[cls.value
for cls
in BinarySensorDeviceClass],
55 mode=SelectSelectorMode.DROPDOWN,
56 translation_key=
"binary_sensor_device_class",
60 if domain == Platform.SENSOR:
63 vol.Optional(CONF_MINIMUM, default=DEFAULT_MIN): cv.positive_int,
64 vol.Optional(CONF_MAXIMUM, default=DEFAULT_MAX): cv.positive_int,
69 for cls
in SensorDeviceClass
70 if cls != SensorDeviceClass.ENUM
73 mode=SelectSelectorMode.DROPDOWN,
74 translation_key=
"sensor_device_class",
81 for units
in DEVICE_CLASS_UNITS.values()
86 mode=SelectSelectorMode.DROPDOWN,
87 translation_key=
"sensor_unit_of_measurement",
94 return vol.Schema(schema)
98 """Return next step_id for options flow according to entity_type."""
99 return cast(str, options[
"entity_type"])
103 """Validate unit of measurement."""
105 (device_class := options.get(CONF_DEVICE_CLASS))
106 and (units := DEVICE_CLASS_UNITS.get(device_class))
107 and (unit := options.get(CONF_UNIT_OF_MEASUREMENT))
not in units
109 sorted_units = sorted(
110 [f
"'{unit!s}'" if unit
else "no unit of measurement" for unit
in units],
113 if len(sorted_units) == 1:
114 units_string = sorted_units[0]
116 units_string = f
"one of {', '.join(sorted_units)}"
119 f
"'{unit}' is not a valid unit for device class '{device_class}'; "
120 f
"expected {units_string}"
127 [SchemaCommonFlowHandler, dict[str, Any]],
128 Coroutine[Any, Any, dict[str, Any]],
130 """Do post validation of user input.
132 For sensors: Validate unit of measurement.
135 async
def _validate_user_input(
136 _: SchemaCommonFlowHandler,
137 user_input: dict[str, Any],
139 """Add entity type to user input."""
140 if entity_type == Platform.SENSOR:
142 return {
"entity_type": entity_type} | user_input
144 return _validate_user_input
148 Platform.BINARY_SENSOR.value,
149 Platform.SENSOR.value,
179 """Handle config flow for random helper."""
181 config_flow = CONFIG_FLOW
182 options_flow = OPTIONS_FLOW
186 """Return config entry title."""
187 return cast(str, options[
"name"])
str async_config_entry_title(self, Mapping[str, Any] options)
Callable[[SchemaCommonFlowHandler, dict[str, Any]], Coroutine[Any, Any, dict[str, Any]],] validate_user_input(str entity_type)
None _validate_unit(dict[str, Any] options)
str choose_options_step(dict[str, Any] options)
vol.Schema _generate_schema(str domain, _FlowType flow_type)