Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Utility Meter 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.sensor import DOMAIN as SENSOR_DOMAIN
11 from homeassistant.const import CONF_NAME
12 from homeassistant.helpers import selector
14  SchemaCommonFlowHandler,
15  SchemaConfigFlowHandler,
16  SchemaFlowError,
17  SchemaFlowFormStep,
18 )
19 
20 from .const import (
21  BIMONTHLY,
22  CONF_METER_DELTA_VALUES,
23  CONF_METER_NET_CONSUMPTION,
24  CONF_METER_OFFSET,
25  CONF_METER_PERIODICALLY_RESETTING,
26  CONF_METER_TYPE,
27  CONF_SENSOR_ALWAYS_AVAILABLE,
28  CONF_SOURCE_SENSOR,
29  CONF_TARIFFS,
30  DAILY,
31  DOMAIN,
32  HOURLY,
33  MONTHLY,
34  QUARTER_HOURLY,
35  QUARTERLY,
36  WEEKLY,
37  YEARLY,
38 )
39 
40 METER_TYPES = [
41  "none",
42  QUARTER_HOURLY,
43  HOURLY,
44  DAILY,
45  WEEKLY,
46  MONTHLY,
47  BIMONTHLY,
48  QUARTERLY,
49  YEARLY,
50 ]
51 
52 
53 async def _validate_config(
54  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
55 ) -> dict[str, Any]:
56  """Validate config."""
57  try:
58  vol.Unique()(user_input[CONF_TARIFFS])
59  except vol.Invalid as exc:
60  raise SchemaFlowError("tariffs_not_unique") from exc
61 
62  return user_input
63 
64 
65 OPTIONS_SCHEMA = vol.Schema(
66  {
67  vol.Required(CONF_SOURCE_SENSOR): selector.EntitySelector(
68  selector.EntitySelectorConfig(domain=SENSOR_DOMAIN),
69  ),
70  vol.Required(
71  CONF_METER_PERIODICALLY_RESETTING,
72  ): selector.BooleanSelector(),
73  vol.Optional(
74  CONF_SENSOR_ALWAYS_AVAILABLE,
75  default=False,
76  ): selector.BooleanSelector(),
77  }
78 )
79 
80 CONFIG_SCHEMA = vol.Schema(
81  {
82  vol.Required(CONF_NAME): selector.TextSelector(),
83  vol.Required(CONF_SOURCE_SENSOR): selector.EntitySelector(
84  selector.EntitySelectorConfig(domain=SENSOR_DOMAIN),
85  ),
86  vol.Required(CONF_METER_TYPE): selector.SelectSelector(
87  selector.SelectSelectorConfig(
88  options=METER_TYPES, translation_key=CONF_METER_TYPE
89  ),
90  ),
91  vol.Required(CONF_METER_OFFSET, default=0): selector.NumberSelector(
92  selector.NumberSelectorConfig(
93  min=0,
94  max=28,
95  mode=selector.NumberSelectorMode.BOX,
96  unit_of_measurement="days",
97  ),
98  ),
99  vol.Required(CONF_TARIFFS, default=[]): selector.SelectSelector(
100  selector.SelectSelectorConfig(options=[], custom_value=True, multiple=True),
101  ),
102  vol.Required(
103  CONF_METER_NET_CONSUMPTION, default=False
104  ): selector.BooleanSelector(),
105  vol.Required(
106  CONF_METER_DELTA_VALUES, default=False
107  ): selector.BooleanSelector(),
108  vol.Required(
109  CONF_METER_PERIODICALLY_RESETTING,
110  default=True,
111  ): selector.BooleanSelector(),
112  vol.Optional(
113  CONF_SENSOR_ALWAYS_AVAILABLE,
114  default=False,
115  ): selector.BooleanSelector(),
116  }
117 )
118 
119 CONFIG_FLOW = {
120  "user": SchemaFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_config)
121 }
122 
123 OPTIONS_FLOW = {
124  "init": SchemaFlowFormStep(OPTIONS_SCHEMA),
125 }
126 
127 
129  """Handle a config or options flow for Utility Meter."""
130 
131  VERSION = 2
132 
133  config_flow = CONFIG_FLOW
134  options_flow = OPTIONS_FLOW
135 
136  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
137  """Return config entry title."""
138 
139  return cast(str, options[CONF_NAME])
dict[str, Any] _validate_config(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:55