2022-03-22 10:32:19 +00:00
|
|
|
"""Config flow for Threshold integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Mapping
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_ENTITY_ID, CONF_NAME
|
|
|
|
from homeassistant.helpers import selector
|
2022-03-30 21:36:47 +00:00
|
|
|
from homeassistant.helpers.schema_config_entry_flow import (
|
|
|
|
SchemaConfigFlowHandler,
|
|
|
|
SchemaFlowError,
|
|
|
|
SchemaFlowFormStep,
|
|
|
|
SchemaFlowMenuStep,
|
2022-03-22 10:32:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
from .const import CONF_HYSTERESIS, CONF_LOWER, CONF_UPPER, DEFAULT_HYSTERESIS, DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
def _validate_mode(data: Any) -> Any:
|
|
|
|
"""Validate the threshold mode, and set limits to None if not set."""
|
|
|
|
if CONF_LOWER not in data and CONF_UPPER not in data:
|
2022-03-30 21:36:47 +00:00
|
|
|
raise SchemaFlowError("need_lower_upper")
|
2022-03-22 10:32:19 +00:00
|
|
|
return {CONF_LOWER: None, CONF_UPPER: None, **data}
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONS_SCHEMA = vol.Schema(
|
|
|
|
{
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(
|
|
|
|
CONF_HYSTERESIS, default=DEFAULT_HYSTERESIS
|
|
|
|
): selector.NumberSelector(
|
2022-09-09 14:05:14 +00:00
|
|
|
selector.NumberSelectorConfig(
|
2022-09-15 06:29:46 +00:00
|
|
|
mode=selector.NumberSelectorMode.BOX, step="any"
|
2022-09-09 14:05:14 +00:00
|
|
|
),
|
2022-04-11 07:20:56 +00:00
|
|
|
),
|
|
|
|
vol.Optional(CONF_LOWER): selector.NumberSelector(
|
2022-09-09 14:05:14 +00:00
|
|
|
selector.NumberSelectorConfig(
|
2022-09-15 06:29:46 +00:00
|
|
|
mode=selector.NumberSelectorMode.BOX, step="any"
|
2022-09-09 14:05:14 +00:00
|
|
|
),
|
2022-04-11 07:20:56 +00:00
|
|
|
),
|
|
|
|
vol.Optional(CONF_UPPER): selector.NumberSelector(
|
2022-09-09 14:05:14 +00:00
|
|
|
selector.NumberSelectorConfig(
|
2022-09-15 06:29:46 +00:00
|
|
|
mode=selector.NumberSelectorMode.BOX, step="any"
|
2022-09-09 14:05:14 +00:00
|
|
|
),
|
2022-03-22 10:32:19 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(CONF_NAME): selector.TextSelector(),
|
|
|
|
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
|
|
|
|
selector.EntitySelectorConfig(domain="sensor")
|
2022-03-22 10:32:19 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
).extend(OPTIONS_SCHEMA.schema)
|
|
|
|
|
2022-03-30 21:36:47 +00:00
|
|
|
CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
|
|
|
|
"user": SchemaFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_mode)
|
2022-03-22 10:32:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 21:36:47 +00:00
|
|
|
OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
|
|
|
|
"init": SchemaFlowFormStep(OPTIONS_SCHEMA, validate_user_input=_validate_mode)
|
2022-03-22 10:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-30 21:36:47 +00:00
|
|
|
class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
|
2022-03-22 10:32:19 +00:00
|
|
|
"""Handle a config or options flow for Threshold."""
|
|
|
|
|
|
|
|
config_flow = CONFIG_FLOW
|
|
|
|
options_flow = OPTIONS_FLOW
|
|
|
|
|
|
|
|
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
|
|
|
|
"""Return config entry title."""
|
|
|
|
return options[CONF_NAME]
|