Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support to set a numeric value from a slider or text box."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 import logging
7 from typing import Self
8 
9 import voluptuous as vol
10 
11 from homeassistant.const import (
12  ATTR_EDITABLE,
13  ATTR_MODE,
14  CONF_ICON,
15  CONF_ID,
16  CONF_MODE,
17  CONF_NAME,
18  CONF_UNIT_OF_MEASUREMENT,
19  SERVICE_RELOAD,
20 )
21 from homeassistant.core import HomeAssistant, ServiceCall, callback
22 from homeassistant.helpers import collection
24 from homeassistant.helpers.entity_component import EntityComponent
25 from homeassistant.helpers.restore_state import RestoreEntity
27 from homeassistant.helpers.storage import Store
28 from homeassistant.helpers.typing import ConfigType, VolDictType
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 DOMAIN = "input_number"
33 
34 CONF_INITIAL = "initial"
35 CONF_MIN = "min"
36 CONF_MAX = "max"
37 CONF_STEP = "step"
38 
39 MODE_SLIDER = "slider"
40 MODE_BOX = "box"
41 
42 ATTR_INITIAL = "initial"
43 ATTR_VALUE = "value"
44 ATTR_MIN = "min"
45 ATTR_MAX = "max"
46 ATTR_STEP = "step"
47 
48 SERVICE_SET_VALUE = "set_value"
49 SERVICE_INCREMENT = "increment"
50 SERVICE_DECREMENT = "decrement"
51 
52 
54  """Configure validation helper for input number (voluptuous)."""
55  minimum = cfg.get(CONF_MIN)
56  maximum = cfg.get(CONF_MAX)
57  if minimum >= maximum:
58  raise vol.Invalid(
59  f"Maximum ({minimum}) is not greater than minimum ({maximum})"
60  )
61  state = cfg.get(CONF_INITIAL)
62  if state is not None and (state < minimum or state > maximum):
63  raise vol.Invalid(f"Initial value {state} not in range {minimum}-{maximum}")
64  return cfg
65 
66 
67 STORAGE_FIELDS: VolDictType = {
68  vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)),
69  vol.Required(CONF_MIN): vol.Coerce(float),
70  vol.Required(CONF_MAX): vol.Coerce(float),
71  vol.Optional(CONF_INITIAL): vol.Coerce(float),
72  vol.Optional(CONF_STEP, default=1): vol.All(vol.Coerce(float), vol.Range(min=1e-9)),
73  vol.Optional(CONF_ICON): cv.icon,
74  vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
75  vol.Optional(CONF_MODE, default=MODE_SLIDER): vol.In([MODE_BOX, MODE_SLIDER]),
76 }
77 
78 CONFIG_SCHEMA = vol.Schema(
79  {
80  DOMAIN: cv.schema_with_slug_keys(
81  vol.All(
82  {
83  vol.Optional(CONF_NAME): cv.string,
84  vol.Required(CONF_MIN): vol.Coerce(float),
85  vol.Required(CONF_MAX): vol.Coerce(float),
86  vol.Optional(CONF_INITIAL): vol.Coerce(float),
87  vol.Optional(CONF_STEP, default=1): vol.All(
88  vol.Coerce(float), vol.Range(min=1e-9)
89  ),
90  vol.Optional(CONF_ICON): cv.icon,
91  vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
92  vol.Optional(CONF_MODE, default=MODE_SLIDER): vol.In(
93  [MODE_BOX, MODE_SLIDER]
94  ),
95  },
96  _cv_input_number,
97  )
98  )
99  },
100  extra=vol.ALLOW_EXTRA,
101 )
102 RELOAD_SERVICE_SCHEMA = vol.Schema({})
103 STORAGE_KEY = DOMAIN
104 STORAGE_VERSION = 1
105 
106 
107 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
108  """Set up an input slider."""
109  component = EntityComponent[InputNumber](_LOGGER, DOMAIN, hass)
110 
111  id_manager = collection.IDManager()
112 
113  yaml_collection = collection.YamlCollection(
114  logging.getLogger(f"{__name__}.yaml_collection"), id_manager
115  )
116  collection.sync_entity_lifecycle(
117  hass, DOMAIN, DOMAIN, component, yaml_collection, InputNumber
118  )
119 
120  storage_collection = NumberStorageCollection(
121  Store(hass, STORAGE_VERSION, STORAGE_KEY),
122  id_manager,
123  )
124  collection.sync_entity_lifecycle(
125  hass, DOMAIN, DOMAIN, component, storage_collection, InputNumber
126  )
127 
128  await yaml_collection.async_load(
129  [{CONF_ID: id_, **(conf or {})} for id_, conf in config.get(DOMAIN, {}).items()]
130  )
131  await storage_collection.async_load()
132 
133  collection.DictStorageCollectionWebsocket(
134  storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS
135  ).async_setup(hass)
136 
137  async def reload_service_handler(service_call: ServiceCall) -> None:
138  """Reload yaml entities."""
139  conf = await component.async_prepare_reload(skip_reset=True)
140  if conf is None:
141  conf = {DOMAIN: {}}
142  await yaml_collection.async_load(
143  [{CONF_ID: id_, **conf} for id_, conf in conf.get(DOMAIN, {}).items()]
144  )
145 
147  hass,
148  DOMAIN,
149  SERVICE_RELOAD,
150  reload_service_handler,
151  schema=RELOAD_SERVICE_SCHEMA,
152  )
153 
154  component.async_register_entity_service(
155  SERVICE_SET_VALUE,
156  {vol.Required(ATTR_VALUE): vol.Coerce(float)},
157  "async_set_value",
158  )
159 
160  component.async_register_entity_service(SERVICE_INCREMENT, None, "async_increment")
161 
162  component.async_register_entity_service(SERVICE_DECREMENT, None, "async_decrement")
163 
164  return True
165 
166 
167 class NumberStorageCollection(collection.DictStorageCollection):
168  """Input storage based collection."""
169 
170  SCHEMA = vol.Schema(vol.All(STORAGE_FIELDS, _cv_input_number))
171 
172  async def _process_create_data(self, data: dict) -> dict:
173  """Validate the config is valid."""
174  return self.SCHEMASCHEMA(data)
175 
176  @callback
177  def _get_suggested_id(self, info: dict) -> str:
178  """Suggest an ID based on the config."""
179  return info[CONF_NAME]
180 
181  async def _async_load_data(self) -> collection.SerializedStorageCollection | None:
182  """Load the data.
183 
184  A past bug caused frontend to add initial value to all input numbers.
185  This drops that.
186  """
187  data = await super()._async_load_data()
188 
189  if data is None:
190  return data
191 
192  for number in data["items"]:
193  number.pop(CONF_INITIAL, None)
194 
195  return data
196 
197  async def _update_data(self, item: dict, update_data: dict) -> dict:
198  """Return a new updated data object."""
199  update_data = self.SCHEMASCHEMA(update_data)
200  return {CONF_ID: item[CONF_ID]} | update_data
201 
202 
203 class InputNumber(collection.CollectionEntity, RestoreEntity):
204  """Representation of a slider."""
205 
206  _unrecorded_attributes = frozenset(
207  {ATTR_EDITABLE, ATTR_MAX, ATTR_MIN, ATTR_MODE, ATTR_STEP}
208  )
209 
210  _attr_should_poll = False
211  editable: bool
212 
213  def __init__(self, config: ConfigType) -> None:
214  """Initialize an input number."""
215  self._config_config = config
216  self._current_value_current_value: float | None = config.get(CONF_INITIAL)
217 
218  @classmethod
219  def from_storage(cls, config: ConfigType) -> Self:
220  """Return entity instance initialized from storage."""
221  input_num = cls(config)
222  input_num.editable = True
223  return input_num
224 
225  @classmethod
226  def from_yaml(cls, config: ConfigType) -> Self:
227  """Return entity instance initialized from yaml."""
228  input_num = cls(config)
229  input_num.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
230  input_num.editable = False
231  return input_num
232 
233  @property
234  def _minimum(self) -> float:
235  """Return minimum allowed value."""
236  return self._config_config[CONF_MIN]
237 
238  @property
239  def _maximum(self) -> float:
240  """Return maximum allowed value."""
241  return self._config_config[CONF_MAX]
242 
243  @property
244  def name(self):
245  """Return the name of the input slider."""
246  return self._config_config.get(CONF_NAME)
247 
248  @property
249  def icon(self):
250  """Return the icon to be used for this entity."""
251  return self._config_config.get(CONF_ICON)
252 
253  @property
254  def state(self):
255  """Return the state of the component."""
256  return self._current_value_current_value
257 
258  @property
259  def _step(self) -> int:
260  """Return entity's increment/decrement step."""
261  return self._config_config[CONF_STEP]
262 
263  @property
265  """Return the unit the value is expressed in."""
266  return self._config_config.get(CONF_UNIT_OF_MEASUREMENT)
267 
268  @property
269  def unique_id(self) -> str | None:
270  """Return unique id of the entity."""
271  return self._config_config[CONF_ID]
272 
273  @property
275  """Return the state attributes."""
276  return {
277  ATTR_INITIAL: self._config_config.get(CONF_INITIAL),
278  ATTR_EDITABLE: self.editable,
279  ATTR_MIN: self._minimum_minimum,
280  ATTR_MAX: self._maximum_maximum,
281  ATTR_STEP: self._step_step,
282  ATTR_MODE: self._config_config[CONF_MODE],
283  }
284 
285  async def async_added_to_hass(self):
286  """Run when entity about to be added to hass."""
287  await super().async_added_to_hass()
288  if self._current_value_current_value is not None:
289  return
290 
291  value: float | None = None
292  if state := await self.async_get_last_stateasync_get_last_state():
293  with suppress(ValueError):
294  value = float(state.state)
295 
296  # Check against None because value can be 0
297  if value is not None and self._minimum_minimum <= value <= self._maximum_maximum:
298  self._current_value_current_value = value
299  else:
300  self._current_value_current_value = self._minimum_minimum
301 
302  async def async_set_value(self, value):
303  """Set new value."""
304  num_value = float(value)
305 
306  if num_value < self._minimum_minimum or num_value > self._maximum_maximum:
307  raise vol.Invalid(
308  f"Invalid value for {self.entity_id}: {value} (range {self._minimum} -"
309  f" {self._maximum})"
310  )
311 
312  self._current_value_current_value = num_value
313  self.async_write_ha_stateasync_write_ha_state()
314 
315  async def async_increment(self):
316  """Increment value."""
317  await self.async_set_valueasync_set_value(min(self._current_value_current_value + self._step_step, self._maximum_maximum))
318 
319  async def async_decrement(self):
320  """Decrement value."""
321  await self.async_set_valueasync_set_value(max(self._current_value_current_value - self._step_step, self._minimum_minimum))
322 
323  async def async_update_config(self, config: ConfigType) -> None:
324  """Handle when the config is updated."""
325  self._config_config = config
326  # just in case min/max values changed
327  if self._current_value_current_value is None:
328  return
329  self._current_value_current_value = min(self._current_value_current_value, self._maximum_maximum)
330  self._current_value_current_value = max(self._current_value_current_value, self._minimum_minimum)
331  self.async_write_ha_stateasync_write_ha_state()
Self from_storage(cls, ConfigType config)
Definition: __init__.py:219
None __init__(self, ConfigType config)
Definition: __init__.py:213
Self from_yaml(cls, ConfigType config)
Definition: __init__.py:226
None async_update_config(self, ConfigType config)
Definition: __init__.py:323
dict _update_data(self, dict item, dict update_data)
Definition: __init__.py:197
collection.SerializedStorageCollection|None _async_load_data(self)
Definition: __init__.py:181
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:107
None async_register_admin_service(HomeAssistant hass, str domain, str service, Callable[[ServiceCall], Awaitable[None]|None] service_func, VolSchemaType schema=vol.Schema({}, extra=vol.PREVENT_EXTRA))
Definition: service.py:1121