Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Derivative integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any, cast
7 
8 import voluptuous as vol
9 
10 from homeassistant.components.counter import DOMAIN as COUNTER_DOMAIN
11 from homeassistant.components.input_number import DOMAIN as INPUT_NUMBER_DOMAIN
12 from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
13 from homeassistant.const import (
14  ATTR_UNIT_OF_MEASUREMENT,
15  CONF_NAME,
16  CONF_SOURCE,
17  UnitOfTime,
18 )
19 from homeassistant.core import callback
20 from homeassistant.helpers import selector
22  SchemaCommonFlowHandler,
23  SchemaConfigFlowHandler,
24  SchemaFlowFormStep,
25  SchemaOptionsFlowHandler,
26 )
27 
28 from .const import (
29  CONF_ROUND_DIGITS,
30  CONF_TIME_WINDOW,
31  CONF_UNIT_PREFIX,
32  CONF_UNIT_TIME,
33  DOMAIN,
34 )
35 
36 UNIT_PREFIXES = [
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)"),
45 ]
46 TIME_UNITS = [
47  UnitOfTime.SECONDS,
48  UnitOfTime.MINUTES,
49  UnitOfTime.HOURS,
50  UnitOfTime.DAYS,
51 ]
52 
53 ALLOWED_DOMAINS = [COUNTER_DOMAIN, INPUT_NUMBER_DOMAIN, SENSOR_DOMAIN]
54 
55 
56 @callback
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
64  )
65 
66  entities = [
67  ent.entity_id
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
71  ]
72 
73  return selector.EntitySelector(
74  selector.EntitySelectorConfig(include_entities=entities)
75  )
76 
77 
78 async def _get_options_dict(handler: SchemaCommonFlowHandler | None) -> dict:
79  if handler is None or not isinstance(
80  handler.parent_handler, SchemaOptionsFlowHandler
81  ):
82  entity_selector = selector.EntitySelector(
83  selector.EntitySelectorConfig(domain=ALLOWED_DOMAINS)
84  )
85  else:
86  entity_selector = entity_selector_compatible(handler.parent_handler)
87 
88  return {
89  vol.Required(CONF_SOURCE): entity_selector,
90  vol.Required(CONF_ROUND_DIGITS, default=2): selector.NumberSelector(
91  selector.NumberSelectorConfig(
92  min=0,
93  max=6,
94  mode=selector.NumberSelectorMode.BOX,
95  unit_of_measurement="decimals",
96  ),
97  ),
98  vol.Required(CONF_TIME_WINDOW): selector.DurationSelector(),
99  vol.Optional(CONF_UNIT_PREFIX): selector.SelectSelector(
100  selector.SelectSelectorConfig(options=UNIT_PREFIXES),
101  ),
102  vol.Required(CONF_UNIT_TIME, default=UnitOfTime.HOURS): selector.SelectSelector(
103  selector.SelectSelectorConfig(
104  options=TIME_UNITS, translation_key="time_unit"
105  ),
106  ),
107  }
108 
109 
110 async def _get_options_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
111  return vol.Schema(await _get_options_dict(handler))
112 
113 
114 async def _get_config_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
115  options = await _get_options_dict(handler)
116  return vol.Schema(
117  {
118  vol.Required(CONF_NAME): selector.TextSelector(),
119  **options,
120  }
121  )
122 
123 
124 CONFIG_FLOW = {
125  "user": SchemaFlowFormStep(_get_config_schema),
126 }
127 
128 OPTIONS_FLOW = {
129  "init": SchemaFlowFormStep(_get_options_schema),
130 }
131 
132 
134  """Handle a config or options flow for Derivative."""
135 
136  config_flow = CONFIG_FLOW
137  options_flow = OPTIONS_FLOW
138 
139  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
140  """Return config entry title."""
141  return cast(str, options[CONF_NAME])
str async_config_entry_title(self, Mapping[str, Any] options)
Definition: config_flow.py:139
vol.Schema _get_config_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:114
dict _get_options_dict(SchemaCommonFlowHandler|None handler)
Definition: config_flow.py:78
vol.Schema _get_options_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:110
selector.EntitySelector entity_selector_compatible(SchemaOptionsFlowHandler handler)
Definition: config_flow.py:59