Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Integration - Riemann sum integral 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_METHOD,
16  CONF_NAME,
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_MAX_SUB_INTERVAL,
30  CONF_ROUND_DIGITS,
31  CONF_SOURCE_SENSOR,
32  CONF_UNIT_PREFIX,
33  CONF_UNIT_TIME,
34  DOMAIN,
35  METHOD_LEFT,
36  METHOD_RIGHT,
37  METHOD_TRAPEZOIDAL,
38 )
39 
40 UNIT_PREFIXES = [
41  selector.SelectOptionDict(value="k", label="k (kilo)"),
42  selector.SelectOptionDict(value="M", label="M (mega)"),
43  selector.SelectOptionDict(value="G", label="G (giga)"),
44  selector.SelectOptionDict(value="T", label="T (tera)"),
45 ]
46 TIME_UNITS = [
47  UnitOfTime.SECONDS,
48  UnitOfTime.MINUTES,
49  UnitOfTime.HOURS,
50  UnitOfTime.DAYS,
51 ]
52 INTEGRATION_METHODS = [
53  METHOD_TRAPEZOIDAL,
54  METHOD_LEFT,
55  METHOD_RIGHT,
56 ]
57 ALLOWED_DOMAINS = [COUNTER_DOMAIN, INPUT_NUMBER_DOMAIN, SENSOR_DOMAIN]
58 
59 
60 @callback
62  handler: SchemaOptionsFlowHandler,
63 ) -> selector.EntitySelector:
64  """Return an entity selector which compatible entities."""
65  current = handler.hass.states.get(handler.options[CONF_SOURCE_SENSOR])
66  unit_of_measurement = (
67  current.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if current else None
68  )
69 
70  entities = [
71  ent.entity_id
72  for ent in handler.hass.states.async_all(ALLOWED_DOMAINS)
73  if ent.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == unit_of_measurement
74  and ent.domain in ALLOWED_DOMAINS
75  ]
76 
77  return selector.EntitySelector(
78  selector.EntitySelectorConfig(include_entities=entities)
79  )
80 
81 
82 async def _get_options_dict(handler: SchemaCommonFlowHandler | None) -> dict:
83  if handler is None or not isinstance(
84  handler.parent_handler, SchemaOptionsFlowHandler
85  ):
86  entity_selector = selector.EntitySelector(
87  selector.EntitySelectorConfig(domain=ALLOWED_DOMAINS)
88  )
89  else:
90  entity_selector = entity_selector_compatible(handler.parent_handler)
91 
92  return {
93  vol.Required(CONF_SOURCE_SENSOR): entity_selector,
94  vol.Required(CONF_METHOD, default=METHOD_TRAPEZOIDAL): selector.SelectSelector(
95  selector.SelectSelectorConfig(
96  options=INTEGRATION_METHODS, translation_key=CONF_METHOD
97  ),
98  ),
99  vol.Optional(CONF_ROUND_DIGITS): selector.NumberSelector(
100  selector.NumberSelectorConfig(
101  min=0, max=6, mode=selector.NumberSelectorMode.BOX
102  ),
103  ),
104  vol.Optional(CONF_MAX_SUB_INTERVAL): selector.DurationSelector(
105  selector.DurationSelectorConfig(allow_negative=False)
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  vol.Optional(CONF_UNIT_PREFIX): selector.SelectSelector(
120  selector.SelectSelectorConfig(
121  options=UNIT_PREFIXES, mode=selector.SelectSelectorMode.DROPDOWN
122  )
123  ),
124  vol.Required(
125  CONF_UNIT_TIME, default=UnitOfTime.HOURS
126  ): selector.SelectSelector(
127  selector.SelectSelectorConfig(
128  options=TIME_UNITS,
129  mode=selector.SelectSelectorMode.DROPDOWN,
130  translation_key=CONF_UNIT_TIME,
131  ),
132  ),
133  **options,
134  }
135  )
136 
137 
138 CONFIG_FLOW = {
139  "user": SchemaFlowFormStep(_get_config_schema),
140 }
141 
142 OPTIONS_FLOW = {
143  "init": SchemaFlowFormStep(_get_options_schema),
144 }
145 
146 
148  """Handle a config or options flow for Integration."""
149 
150  config_flow = CONFIG_FLOW
151  options_flow = OPTIONS_FLOW
152 
153  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
154  """Return config entry title."""
155  return cast(str, options["name"])
str async_config_entry_title(self, Mapping[str, Any] options)
Definition: config_flow.py:153
vol.Schema _get_config_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:114
dict _get_options_dict(SchemaCommonFlowHandler|None handler)
Definition: config_flow.py:82
selector.EntitySelector entity_selector_compatible(SchemaOptionsFlowHandler handler)
Definition: config_flow.py:63
vol.Schema _get_options_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:110