Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for Homekit number ranges.
2 
3 These are mostly used where a HomeKit accessory exposes additional non-standard
4 characteristics that don't map to a Home Assistant feature.
5 """
6 
7 from __future__ import annotations
8 
9 from aiohomekit.model.characteristics import Characteristic, CharacteristicsTypes
10 
12  DEFAULT_MAX_VALUE,
13  DEFAULT_MIN_VALUE,
14  DEFAULT_STEP,
15  NumberEntity,
16  NumberEntityDescription,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import EntityCategory, Platform
20 from homeassistant.core import HomeAssistant, callback
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType
23 
24 from . import KNOWN_DEVICES
25 from .connection import HKDevice
26 from .entity import CharacteristicEntity
27 
28 NUMBER_ENTITIES: dict[str, NumberEntityDescription] = {
29  CharacteristicsTypes.VENDOR_VOCOLINC_HUMIDIFIER_SPRAY_LEVEL: NumberEntityDescription(
30  key=CharacteristicsTypes.VENDOR_VOCOLINC_HUMIDIFIER_SPRAY_LEVEL,
31  name="Spray Quantity",
32  translation_key="spray_quantity",
33  entity_category=EntityCategory.CONFIG,
34  ),
35  CharacteristicsTypes.VENDOR_EVE_DEGREE_ELEVATION: NumberEntityDescription(
36  key=CharacteristicsTypes.VENDOR_EVE_DEGREE_ELEVATION,
37  name="Elevation",
38  translation_key="elevation",
39  entity_category=EntityCategory.CONFIG,
40  ),
41  CharacteristicsTypes.VENDOR_AQARA_GATEWAY_VOLUME: NumberEntityDescription(
42  key=CharacteristicsTypes.VENDOR_AQARA_GATEWAY_VOLUME,
43  name="Volume",
44  translation_key="volume",
45  entity_category=EntityCategory.CONFIG,
46  ),
47  CharacteristicsTypes.VENDOR_AQARA_E1_GATEWAY_VOLUME: NumberEntityDescription(
48  key=CharacteristicsTypes.VENDOR_AQARA_E1_GATEWAY_VOLUME,
49  name="Volume",
50  translation_key="volume",
51  entity_category=EntityCategory.CONFIG,
52  ),
53  CharacteristicsTypes.VENDOR_EVE_MOTION_DURATION: NumberEntityDescription(
54  key=CharacteristicsTypes.VENDOR_EVE_MOTION_DURATION,
55  name="Duration",
56  translation_key="duration",
57  entity_category=EntityCategory.CONFIG,
58  ),
59  CharacteristicsTypes.VENDOR_EVE_MOTION_SENSITIVITY: NumberEntityDescription(
60  key=CharacteristicsTypes.VENDOR_EVE_MOTION_SENSITIVITY,
61  name="Sensitivity",
62  translation_key="sensitivity",
63  entity_category=EntityCategory.CONFIG,
64  ),
65 }
66 
67 
69  hass: HomeAssistant,
70  config_entry: ConfigEntry,
71  async_add_entities: AddEntitiesCallback,
72 ) -> None:
73  """Set up Homekit numbers."""
74  hkid: str = config_entry.data["AccessoryPairingID"]
75  conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
76 
77  @callback
78  def async_add_characteristic(char: Characteristic) -> bool:
79  entities: list[HomeKitNumber] = []
80  info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
81 
82  if description := NUMBER_ENTITIES.get(char.type):
83  entities.append(HomeKitNumber(conn, info, char, description))
84  else:
85  return False
86 
87  for entity in entities:
88  conn.async_migrate_unique_id(
89  entity.old_unique_id, entity.unique_id, Platform.NUMBER
90  )
91 
92  async_add_entities(entities)
93  return True
94 
95  conn.add_char_factory(async_add_characteristic)
96 
97 
99  """Representation of a Number control on a homekit accessory."""
100 
101  def __init__(
102  self,
103  conn: HKDevice,
104  info: ConfigType,
105  char: Characteristic,
106  description: NumberEntityDescription,
107  ) -> None:
108  """Initialise a HomeKit number control."""
109  self.entity_descriptionentity_description = description
110  super().__init__(conn, info, char)
111 
112  @property
113  def name(self) -> str:
114  """Return the name of the device if any."""
115  if name := self.accessoryaccessory.name:
116  return f"{name} {self.entity_description.name}"
117  return f"{self.entity_description.name}"
118 
119  def get_characteristic_types(self) -> list[str]:
120  """Define the homekit characteristics the entity is tracking."""
121  return [self._char_char.type]
122 
123  @property
124  def native_min_value(self) -> float:
125  """Return the minimum value."""
126  return self._char_char.minValue or DEFAULT_MIN_VALUE
127 
128  @property
129  def native_max_value(self) -> float:
130  """Return the maximum value."""
131  return self._char_char.maxValue or DEFAULT_MAX_VALUE
132 
133  @property
134  def native_step(self) -> float:
135  """Return the increment/decrement step."""
136  return self._char_char.minStep or DEFAULT_STEP
137 
138  @property
139  def native_value(self) -> float:
140  """Return the current characteristic value."""
141  return self._char_char.value
142 
143  async def async_set_native_value(self, value: float) -> None:
144  """Set the characteristic to this value."""
145  await self.async_put_characteristicsasync_put_characteristics(
146  {
147  self._char_char.type: value,
148  }
149  )
None async_put_characteristics(self, dict[str, Any] characteristics)
Definition: entity.py:125
None __init__(self, HKDevice conn, ConfigType info, Characteristic char, NumberEntityDescription description)
Definition: number.py:107
bool async_add_characteristic(Characteristic char)
Definition: number.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: number.py:72