Files
core/homeassistant/components/generic_hygrostat/config_flow.py
Joakim Plate 84d1d11138 Add config flow to generic hygrostat (#119017)
* Add config flow to hygrostat

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2024-06-23 12:56:41 +02:00

101 lines
2.8 KiB
Python

"""Config flow for Generic hygrostat."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any, cast
import voluptuous as vol
from homeassistant.components.humidifier import HumidifierDeviceClass
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import CONF_NAME, PERCENTAGE
from homeassistant.helpers import selector
from homeassistant.helpers.schema_config_entry_flow import (
SchemaConfigFlowHandler,
SchemaFlowFormStep,
)
from . import (
CONF_DEVICE_CLASS,
CONF_DRY_TOLERANCE,
CONF_HUMIDIFIER,
CONF_MIN_DUR,
CONF_SENSOR,
CONF_WET_TOLERANCE,
DEFAULT_TOLERANCE,
DOMAIN,
)
OPTIONS_SCHEMA = {
vol.Required(CONF_DEVICE_CLASS): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
HumidifierDeviceClass.HUMIDIFIER,
HumidifierDeviceClass.DEHUMIDIFIER,
],
translation_key=CONF_DEVICE_CLASS,
mode=selector.SelectSelectorMode.DROPDOWN,
),
),
vol.Required(CONF_SENSOR): selector.EntitySelector(
selector.EntitySelectorConfig(
domain=SENSOR_DOMAIN, device_class=SensorDeviceClass.HUMIDITY
)
),
vol.Required(CONF_HUMIDIFIER): selector.EntitySelector(
selector.EntitySelectorConfig(domain=SWITCH_DOMAIN)
),
vol.Required(
CONF_DRY_TOLERANCE, default=DEFAULT_TOLERANCE
): selector.NumberSelector(
selector.NumberSelectorConfig(
min=0,
max=100,
step=0.5,
unit_of_measurement=PERCENTAGE,
mode=selector.NumberSelectorMode.BOX,
)
),
vol.Required(
CONF_WET_TOLERANCE, default=DEFAULT_TOLERANCE
): selector.NumberSelector(
selector.NumberSelectorConfig(
min=0,
max=100,
step=0.5,
unit_of_measurement=PERCENTAGE,
mode=selector.NumberSelectorMode.BOX,
)
),
vol.Optional(CONF_MIN_DUR): selector.DurationSelector(
selector.DurationSelectorConfig(allow_negative=False)
),
}
CONFIG_SCHEMA = {
vol.Required(CONF_NAME): selector.TextSelector(),
**OPTIONS_SCHEMA,
}
CONFIG_FLOW = {
"user": SchemaFlowFormStep(vol.Schema(CONFIG_SCHEMA)),
}
OPTIONS_FLOW = {
"init": SchemaFlowFormStep(vol.Schema(OPTIONS_SCHEMA)),
}
class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
"""Handle a config or options flow."""
config_flow = CONFIG_FLOW
options_flow = OPTIONS_FLOW
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
"""Return config entry title."""
return cast(str, options["name"])