Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for Tuya number."""
2 
3 from __future__ import annotations
4 
5 from tuya_sharing import CustomerDevice, Manager
6 
8  NumberDeviceClass,
9  NumberEntity,
10  NumberEntityDescription,
11 )
12 from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import TuyaConfigEntry
18 from .const import DEVICE_CLASS_UNITS, DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
19 from .entity import IntegerTypeData, TuyaEntity
20 
21 # All descriptions can be found here. Mostly the Integer data types in the
22 # default instructions set of each category end up being a number.
23 # https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
24 NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
25  # Multi-functional Sensor
26  # https://developer.tuya.com/en/docs/iot/categorydgnbj?id=Kaiuz3yorvzg3
27  "dgnbj": (
29  key=DPCode.ALARM_TIME,
30  translation_key="time",
31  entity_category=EntityCategory.CONFIG,
32  ),
33  ),
34  # Smart Kettle
35  # https://developer.tuya.com/en/docs/iot/fbh?id=K9gf484m21yq7
36  "bh": (
38  key=DPCode.TEMP_SET,
39  translation_key="temperature",
40  device_class=NumberDeviceClass.TEMPERATURE,
41  entity_category=EntityCategory.CONFIG,
42  ),
44  key=DPCode.TEMP_SET_F,
45  translation_key="temperature",
46  device_class=NumberDeviceClass.TEMPERATURE,
47  entity_category=EntityCategory.CONFIG,
48  ),
50  key=DPCode.TEMP_BOILING_C,
51  translation_key="temperature_after_boiling",
52  device_class=NumberDeviceClass.TEMPERATURE,
53  entity_category=EntityCategory.CONFIG,
54  ),
56  key=DPCode.TEMP_BOILING_F,
57  translation_key="temperature_after_boiling",
58  device_class=NumberDeviceClass.TEMPERATURE,
59  entity_category=EntityCategory.CONFIG,
60  ),
62  key=DPCode.WARM_TIME,
63  translation_key="heat_preservation_time",
64  entity_category=EntityCategory.CONFIG,
65  ),
66  ),
67  # Smart Pet Feeder
68  # https://developer.tuya.com/en/docs/iot/categorycwwsq?id=Kaiuz2b6vydld
69  "cwwsq": (
71  key=DPCode.MANUAL_FEED,
72  translation_key="feed",
73  ),
75  key=DPCode.VOICE_TIMES,
76  translation_key="voice_times",
77  ),
78  ),
79  # Human Presence Sensor
80  # https://developer.tuya.com/en/docs/iot/categoryhps?id=Kaiuz42yhn1hs
81  "hps": (
83  key=DPCode.SENSITIVITY,
84  translation_key="sensitivity",
85  entity_category=EntityCategory.CONFIG,
86  ),
88  key=DPCode.NEAR_DETECTION,
89  translation_key="near_detection",
90  device_class=NumberDeviceClass.DISTANCE,
91  entity_category=EntityCategory.CONFIG,
92  ),
94  key=DPCode.FAR_DETECTION,
95  translation_key="far_detection",
96  device_class=NumberDeviceClass.DISTANCE,
97  entity_category=EntityCategory.CONFIG,
98  ),
100  key=DPCode.TARGET_DIS_CLOSEST,
101  translation_key="target_dis_closest",
102  device_class=NumberDeviceClass.DISTANCE,
103  ),
104  ),
105  # Coffee maker
106  # https://developer.tuya.com/en/docs/iot/categorykfj?id=Kaiuz2p12pc7f
107  "kfj": (
109  key=DPCode.WATER_SET,
110  translation_key="water_level",
111  entity_category=EntityCategory.CONFIG,
112  ),
114  key=DPCode.TEMP_SET,
115  translation_key="temperature",
116  device_class=NumberDeviceClass.TEMPERATURE,
117  entity_category=EntityCategory.CONFIG,
118  ),
120  key=DPCode.WARM_TIME,
121  translation_key="heat_preservation_time",
122  entity_category=EntityCategory.CONFIG,
123  ),
125  key=DPCode.POWDER_SET,
126  translation_key="powder",
127  entity_category=EntityCategory.CONFIG,
128  ),
129  ),
130  # Sous Vide Cooker
131  # https://developer.tuya.com/en/docs/iot/categorymzj?id=Kaiuz2vy130ux
132  "mzj": (
134  key=DPCode.COOK_TEMPERATURE,
135  translation_key="cook_temperature",
136  entity_category=EntityCategory.CONFIG,
137  ),
139  key=DPCode.COOK_TIME,
140  translation_key="cook_time",
141  native_unit_of_measurement=UnitOfTime.MINUTES,
142  entity_category=EntityCategory.CONFIG,
143  ),
145  key=DPCode.CLOUD_RECIPE_NUMBER,
146  translation_key="cloud_recipe",
147  entity_category=EntityCategory.CONFIG,
148  ),
149  ),
150  # Robot Vacuum
151  # https://developer.tuya.com/en/docs/iot/fsd?id=K9gf487ck1tlo
152  "sd": (
154  key=DPCode.VOLUME_SET,
155  translation_key="volume",
156  entity_category=EntityCategory.CONFIG,
157  ),
158  ),
159  # Siren Alarm
160  # https://developer.tuya.com/en/docs/iot/categorysgbj?id=Kaiuz37tlpbnu
161  "sgbj": (
163  key=DPCode.ALARM_TIME,
164  translation_key="time",
165  entity_category=EntityCategory.CONFIG,
166  ),
167  ),
168  # Smart Camera
169  # https://developer.tuya.com/en/docs/iot/categorysp?id=Kaiuz35leyo12
170  "sp": (
172  key=DPCode.BASIC_DEVICE_VOLUME,
173  translation_key="volume",
174  entity_category=EntityCategory.CONFIG,
175  ),
176  ),
177  # Dimmer Switch
178  # https://developer.tuya.com/en/docs/iot/categorytgkg?id=Kaiuz0ktx7m0o
179  "tgkg": (
181  key=DPCode.BRIGHTNESS_MIN_1,
182  translation_key="minimum_brightness",
183  entity_category=EntityCategory.CONFIG,
184  ),
186  key=DPCode.BRIGHTNESS_MAX_1,
187  translation_key="maximum_brightness",
188  entity_category=EntityCategory.CONFIG,
189  ),
191  key=DPCode.BRIGHTNESS_MIN_2,
192  translation_key="minimum_brightness_2",
193  entity_category=EntityCategory.CONFIG,
194  ),
196  key=DPCode.BRIGHTNESS_MAX_2,
197  translation_key="maximum_brightness_2",
198  entity_category=EntityCategory.CONFIG,
199  ),
201  key=DPCode.BRIGHTNESS_MIN_3,
202  translation_key="minimum_brightness_3",
203  entity_category=EntityCategory.CONFIG,
204  ),
206  key=DPCode.BRIGHTNESS_MAX_3,
207  translation_key="maximum_brightness_3",
208  entity_category=EntityCategory.CONFIG,
209  ),
210  ),
211  # Dimmer Switch
212  # https://developer.tuya.com/en/docs/iot/categorytgkg?id=Kaiuz0ktx7m0o
213  "tgq": (
215  key=DPCode.BRIGHTNESS_MIN_1,
216  translation_key="minimum_brightness",
217  entity_category=EntityCategory.CONFIG,
218  ),
220  key=DPCode.BRIGHTNESS_MAX_1,
221  translation_key="maximum_brightness",
222  entity_category=EntityCategory.CONFIG,
223  ),
225  key=DPCode.BRIGHTNESS_MIN_2,
226  translation_key="minimum_brightness_2",
227  entity_category=EntityCategory.CONFIG,
228  ),
230  key=DPCode.BRIGHTNESS_MAX_2,
231  translation_key="maximum_brightness_2",
232  entity_category=EntityCategory.CONFIG,
233  ),
234  ),
235  # Vibration Sensor
236  # https://developer.tuya.com/en/docs/iot/categoryzd?id=Kaiuz3a5vrzno
237  "zd": (
239  key=DPCode.SENSITIVITY,
240  translation_key="sensitivity",
241  entity_category=EntityCategory.CONFIG,
242  ),
243  ),
244  # Fingerbot
245  "szjqr": (
247  key=DPCode.ARM_DOWN_PERCENT,
248  translation_key="move_down",
249  native_unit_of_measurement=PERCENTAGE,
250  entity_category=EntityCategory.CONFIG,
251  ),
253  key=DPCode.ARM_UP_PERCENT,
254  translation_key="move_up",
255  native_unit_of_measurement=PERCENTAGE,
256  entity_category=EntityCategory.CONFIG,
257  ),
259  key=DPCode.CLICK_SUSTAIN_TIME,
260  translation_key="down_delay",
261  entity_category=EntityCategory.CONFIG,
262  ),
263  ),
264  # Fan
265  # https://developer.tuya.com/en/docs/iot/categoryfs?id=Kaiuz1xweel1c
266  "fs": (
268  key=DPCode.TEMP,
269  translation_key="temperature",
270  device_class=NumberDeviceClass.TEMPERATURE,
271  ),
272  ),
273  # Humidifier
274  # https://developer.tuya.com/en/docs/iot/categoryjsq?id=Kaiuz1smr440b
275  "jsq": (
277  key=DPCode.TEMP_SET,
278  translation_key="temperature",
279  device_class=NumberDeviceClass.TEMPERATURE,
280  ),
282  key=DPCode.TEMP_SET_F,
283  translation_key="temperature",
284  device_class=NumberDeviceClass.TEMPERATURE,
285  ),
286  ),
287  # Pool HeatPump
288  "znrb": (
290  key=DPCode.TEMP_SET,
291  translation_key="temperature",
292  device_class=NumberDeviceClass.TEMPERATURE,
293  ),
294  ),
295  # CO2 Detector
296  # https://developer.tuya.com/en/docs/iot/categoryco2bj?id=Kaiuz3wes7yuy
297  "co2bj": (
299  key=DPCode.ALARM_TIME,
300  translation_key="alarm_duration",
301  native_unit_of_measurement=UnitOfTime.SECONDS,
302  device_class=NumberDeviceClass.DURATION,
303  entity_category=EntityCategory.CONFIG,
304  ),
305  ),
306 }
307 
308 
310  hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
311 ) -> None:
312  """Set up Tuya number dynamically through Tuya discovery."""
313  hass_data = entry.runtime_data
314 
315  @callback
316  def async_discover_device(device_ids: list[str]) -> None:
317  """Discover and add a discovered Tuya number."""
318  entities: list[TuyaNumberEntity] = []
319  for device_id in device_ids:
320  device = hass_data.manager.device_map[device_id]
321  if descriptions := NUMBERS.get(device.category):
322  entities.extend(
323  TuyaNumberEntity(device, hass_data.manager, description)
324  for description in descriptions
325  if description.key in device.status
326  )
327 
328  async_add_entities(entities)
329 
330  async_discover_device([*hass_data.manager.device_map])
331 
332  entry.async_on_unload(
333  async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
334  )
335 
336 
338  """Tuya Number Entity."""
339 
340  _number: IntegerTypeData | None = None
341 
342  def __init__(
343  self,
344  device: CustomerDevice,
345  device_manager: Manager,
346  description: NumberEntityDescription,
347  ) -> None:
348  """Init Tuya sensor."""
349  super().__init__(device, device_manager)
350  self.entity_descriptionentity_description = description
351  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}{description.key}"
352 
353  if int_type := self.find_dpcodefind_dpcodefind_dpcodefind_dpcodefind_dpcode(
354  description.key, dptype=DPType.INTEGER, prefer_function=True
355  ):
356  self._number_number = int_type
357  self._attr_native_max_value_attr_native_max_value = self._number_number.max_scaled
358  self._attr_native_min_value_attr_native_min_value = self._number_number.min_scaled
359  self._attr_native_step_attr_native_step = self._number_number.step_scaled
360 
361  # Logic to ensure the set device class and API received Unit Of Measurement
362  # match Home Assistants requirements.
363  if (
364  self.device_classdevice_classdevice_classdevice_class is not None
365  and not self.device_classdevice_classdevice_classdevice_class.startswith(DOMAIN)
366  and description.native_unit_of_measurement is None
367  ):
368  # We cannot have a device class, if the UOM isn't set or the
369  # device class cannot be found in the validation mapping.
370  if (
371  self.native_unit_of_measurementnative_unit_of_measurement is None
372  or self.device_classdevice_classdevice_classdevice_class not in DEVICE_CLASS_UNITS
373  ):
374  self._attr_device_class_attr_device_class = None
375  return
376 
377  uoms = DEVICE_CLASS_UNITS[self.device_classdevice_classdevice_classdevice_class]
378  self._uom_uom = uoms.get(self.native_unit_of_measurementnative_unit_of_measurement) or uoms.get(
379  self.native_unit_of_measurementnative_unit_of_measurement.lower()
380  )
381 
382  # Unknown unit of measurement, device class should not be used.
383  if self._uom_uom is None:
384  self._attr_device_class_attr_device_class = None
385  return
386 
387  # Found unit of measurement, use the standardized Unit
388  # Use the target conversion unit (if set)
389  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = (
390  self._uom_uom.conversion_unit or self._uom_uom.unit
391  )
392 
393  @property
394  def native_value(self) -> float | None:
395  """Return the entity value to represent the entity state."""
396  # Unknown or unsupported data type
397  if self._number_number is None:
398  return None
399 
400  # Raw value
401  if (value := self.devicedevice.status.get(self.entity_descriptionentity_description.key)) is None:
402  return None
403 
404  return self._number_number.scale_value(value)
405 
406  def set_native_value(self, value: float) -> None:
407  """Set new value."""
408  if self._number_number is None:
409  raise RuntimeError("Cannot set value, device doesn't provide type data")
410 
411  self._send_command_send_command(
412  [
413  {
414  "code": self.entity_descriptionentity_description.key,
415  "value": self._number_number.scale_value_back(value),
416  }
417  ]
418  )
NumberDeviceClass|None device_class(self)
Definition: __init__.py:266
None _send_command(self, list[dict[str, Any]] commands)
Definition: entity.py:295
DPCode|EnumTypeData|IntegerTypeData|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False, DPType|None dptype=None)
Definition: entity.py:206
IntegerTypeData|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False, Literal[DPType.INTEGER] dptype)
Definition: entity.py:190
DPCode|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False)
Definition: entity.py:198
EnumTypeData|None find_dpcode(self, str|DPCode|tuple[DPCode,...]|None dpcodes, *bool prefer_function=False, Literal[DPType.ENUM] dptype)
Definition: entity.py:181
None __init__(self, CustomerDevice device, Manager device_manager, NumberEntityDescription description)
Definition: number.py:347
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, TuyaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:311
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103