hass-core/homeassistant/components/threshold/config_flow.py
Raman Gupta b325c112b4
Add SelectorType enum and TypedDicts for each selector's data (#68399)
* rebase off current

* rearrange

* Overload selector function

* Update/fix all selector references

* better typing?

* remove extra option

* move things around

* Switch to Sequence type to avoid ignoring mypy error

* Get rid of ...'s

* Improve typing to reduce number of ignores

* Remove all typing ignores

* Make config optional for selectors that don't need a config

* add missing unit prefixes

* Rename TypedDicts

* Update homeassistant/helpers/selector.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* review feedback

* remove peta from integration integration

* Fix min_max

* Revert change to selector function

* Fix logic

* Add typing for selector classes

* Update selector.py

* Fix indent

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2022-04-11 09:20:56 +02:00

70 lines
2.2 KiB
Python

"""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
from homeassistant.helpers.schema_config_entry_flow import (
SchemaConfigFlowHandler,
SchemaFlowError,
SchemaFlowFormStep,
SchemaFlowMenuStep,
)
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:
raise SchemaFlowError("need_lower_upper")
return {CONF_LOWER: None, CONF_UPPER: None, **data}
OPTIONS_SCHEMA = vol.Schema(
{
vol.Required(
CONF_HYSTERESIS, default=DEFAULT_HYSTERESIS
): selector.NumberSelector(
selector.NumberSelectorConfig(mode=selector.NumberSelectorMode.BOX),
),
vol.Optional(CONF_LOWER): selector.NumberSelector(
selector.NumberSelectorConfig(mode=selector.NumberSelectorMode.BOX),
),
vol.Optional(CONF_UPPER): selector.NumberSelector(
selector.NumberSelectorConfig(mode=selector.NumberSelectorMode.BOX),
),
}
)
CONFIG_SCHEMA = vol.Schema(
{
vol.Required(CONF_NAME): selector.TextSelector(),
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
selector.EntitySelectorConfig(domain="sensor")
),
}
).extend(OPTIONS_SCHEMA.schema)
CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
"user": SchemaFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_mode)
}
OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
"init": SchemaFlowFormStep(OPTIONS_SCHEMA, validate_user_input=_validate_mode)
}
class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
"""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]