1 """Config flow for Derivative integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from typing
import Any, cast
8 import voluptuous
as vol
14 ATTR_UNIT_OF_MEASUREMENT,
22 SchemaCommonFlowHandler,
23 SchemaConfigFlowHandler,
25 SchemaOptionsFlowHandler,
37 selector.SelectOptionDict(value=
"n", label=
"n (nano)"),
38 selector.SelectOptionDict(value=
"µ", label=
"µ (micro)"),
39 selector.SelectOptionDict(value=
"m", label=
"m (milli)"),
40 selector.SelectOptionDict(value=
"k", label=
"k (kilo)"),
41 selector.SelectOptionDict(value=
"M", label=
"M (mega)"),
42 selector.SelectOptionDict(value=
"G", label=
"G (giga)"),
43 selector.SelectOptionDict(value=
"T", label=
"T (tera)"),
44 selector.SelectOptionDict(value=
"P", label=
"P (peta)"),
53 ALLOWED_DOMAINS = [COUNTER_DOMAIN, INPUT_NUMBER_DOMAIN, SENSOR_DOMAIN]
58 handler: SchemaOptionsFlowHandler,
59 ) -> selector.EntitySelector:
60 """Return an entity selector which compatible entities."""
61 current = handler.hass.states.get(handler.options[CONF_SOURCE])
62 unit_of_measurement = (
63 current.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
if current
else None
68 for ent
in handler.hass.states.async_all(ALLOWED_DOMAINS)
69 if ent.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == unit_of_measurement
70 and ent.domain
in ALLOWED_DOMAINS
73 return selector.EntitySelector(
74 selector.EntitySelectorConfig(include_entities=entities)
79 if handler
is None or not isinstance(
80 handler.parent_handler, SchemaOptionsFlowHandler
82 entity_selector = selector.EntitySelector(
83 selector.EntitySelectorConfig(domain=ALLOWED_DOMAINS)
89 vol.Required(CONF_SOURCE): entity_selector,
90 vol.Required(CONF_ROUND_DIGITS, default=2): selector.NumberSelector(
91 selector.NumberSelectorConfig(
94 mode=selector.NumberSelectorMode.BOX,
95 unit_of_measurement=
"decimals",
98 vol.Required(CONF_TIME_WINDOW): selector.DurationSelector(),
99 vol.Optional(CONF_UNIT_PREFIX): selector.SelectSelector(
100 selector.SelectSelectorConfig(options=UNIT_PREFIXES),
102 vol.Required(CONF_UNIT_TIME, default=UnitOfTime.HOURS): selector.SelectSelector(
103 selector.SelectSelectorConfig(
104 options=TIME_UNITS, translation_key=
"time_unit"
118 vol.Required(CONF_NAME): selector.TextSelector(),
134 """Handle a config or options flow for Derivative."""
136 config_flow = CONFIG_FLOW
137 options_flow = OPTIONS_FLOW
140 """Return config entry title."""
141 return cast(str, options[CONF_NAME])
str async_config_entry_title(self, Mapping[str, Any] options)
vol.Schema _get_config_schema(SchemaCommonFlowHandler handler)
dict _get_options_dict(SchemaCommonFlowHandler|None handler)
vol.Schema _get_options_schema(SchemaCommonFlowHandler handler)
selector.EntitySelector entity_selector_compatible(SchemaOptionsFlowHandler handler)