Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for KNX/IP numeric values."""
2 
3 from __future__ import annotations
4 
5 from typing import cast
6 
7 from xknx import XKNX
8 from xknx.devices import NumericValue
9 
10 from homeassistant import config_entries
11 from homeassistant.components.number import RestoreNumber
12 from homeassistant.const import (
13  CONF_ENTITY_CATEGORY,
14  CONF_MODE,
15  CONF_NAME,
16  CONF_TYPE,
17  STATE_UNAVAILABLE,
18  STATE_UNKNOWN,
19  Platform,
20 )
21 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 from homeassistant.helpers.typing import ConfigType
24 
25 from . import KNXModule
26 from .const import CONF_RESPOND_TO_READ, CONF_STATE_ADDRESS, KNX_ADDRESS, KNX_MODULE_KEY
27 from .entity import KnxYamlEntity
28 from .schema import NumberSchema
29 
30 
32  hass: HomeAssistant,
33  config_entry: config_entries.ConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up number(s) for KNX platform."""
37  knx_module = hass.data[KNX_MODULE_KEY]
38  config: list[ConfigType] = knx_module.config_yaml[Platform.NUMBER]
39 
40  async_add_entities(KNXNumber(knx_module, entity_config) for entity_config in config)
41 
42 
43 def _create_numeric_value(xknx: XKNX, config: ConfigType) -> NumericValue:
44  """Return a KNX NumericValue to be used within XKNX."""
45  return NumericValue(
46  xknx,
47  name=config[CONF_NAME],
48  group_address=config[KNX_ADDRESS],
49  group_address_state=config.get(CONF_STATE_ADDRESS),
50  respond_to_read=config[CONF_RESPOND_TO_READ],
51  value_type=config[CONF_TYPE],
52  )
53 
54 
56  """Representation of a KNX number."""
57 
58  _device: NumericValue
59 
60  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
61  """Initialize a KNX number."""
62  super().__init__(
63  knx_module=knx_module,
64  device=_create_numeric_value(knx_module.xknx, config),
65  )
66  self._attr_native_max_value_attr_native_max_value = config.get(
67  NumberSchema.CONF_MAX,
68  self._device_device.sensor_value.dpt_class.value_max,
69  )
70  self._attr_native_min_value_attr_native_min_value = config.get(
71  NumberSchema.CONF_MIN,
72  self._device_device.sensor_value.dpt_class.value_min,
73  )
74  self._attr_mode_attr_mode = config[CONF_MODE]
75  self._attr_native_step_attr_native_step = config.get(
76  NumberSchema.CONF_STEP,
77  self._device_device.sensor_value.dpt_class.resolution,
78  )
79  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
80  self._attr_unique_id_attr_unique_id = str(self._device_device.sensor_value.group_address)
81  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = self._device_device.unit_of_measurement()
82  self._device_device.sensor_value.value = max(0, self._attr_native_min_value_attr_native_min_value)
83 
84  async def async_added_to_hass(self) -> None:
85  """Restore last state."""
86  await super().async_added_to_hass()
87  if (
88  not self._device_device.sensor_value.readable
89  and (last_state := await self.async_get_last_stateasync_get_last_state())
90  and (last_number_data := await self.async_get_last_number_dataasync_get_last_number_data())
91  ):
92  if last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
93  self._device_device.sensor_value.value = last_number_data.native_value
94 
95  @property
96  def native_value(self) -> float:
97  """Return the entity value to represent the entity state."""
98  # self._device.sensor_value.value is set in __init__ so it is never None
99  return cast(float, self._device_device.resolve_state())
100 
101  async def async_set_native_value(self, value: float) -> None:
102  """Set new value."""
103  await self._device_device.set(value)
None __init__(self, KNXModule knx_module, ConfigType config)
Definition: number.py:60
None async_set_native_value(self, float value)
Definition: number.py:101
NumberExtraStoredData|None async_get_last_number_data(self)
Definition: __init__.py:549
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: number.py:35
NumericValue _create_numeric_value(XKNX xknx, ConfigType config)
Definition: number.py:43