number.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Setup for different kinds of Tuya numbers
  3. """
  4. from homeassistant.components.number import NumberEntity
  5. from homeassistant.components.number.const import (
  6. DEFAULT_MIN_VALUE,
  7. DEFAULT_MAX_VALUE,
  8. NumberDeviceClass,
  9. )
  10. from .device import TuyaLocalDevice
  11. from .helpers.config import async_tuya_setup_platform
  12. from .helpers.device_config import TuyaEntityConfig
  13. from .helpers.mixin import TuyaLocalEntity, unit_from_ascii
  14. MODE_AUTO = "auto"
  15. async def async_setup_entry(hass, config_entry, async_add_entities):
  16. config = {**config_entry.data, **config_entry.options}
  17. await async_tuya_setup_platform(
  18. hass,
  19. async_add_entities,
  20. config,
  21. "number",
  22. TuyaLocalNumber,
  23. )
  24. class TuyaLocalNumber(TuyaLocalEntity, NumberEntity):
  25. """Representation of a Tuya Number"""
  26. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  27. """
  28. Initialise the sensor.
  29. Args:
  30. device (TuyaLocalDevice): the device API instance
  31. config (TuyaEntityConfig): the configuration for this entity
  32. """
  33. dps_map = self._init_begin(device, config)
  34. self._value_dps = dps_map.pop("value")
  35. if self._value_dps is None:
  36. raise AttributeError(f"{config.name} is missing a value dps")
  37. self._unit_dps = dps_map.pop("unit", None)
  38. self._min_dps = dps_map.pop("minimum", None)
  39. self._max_dps = dps_map.pop("maximum", None)
  40. self._init_end(dps_map)
  41. @property
  42. def device_class(self):
  43. """Return the class of this device"""
  44. dclass = self._config.device_class
  45. if dclass:
  46. try:
  47. return NumberDeviceClass(dclass)
  48. except ValueError:
  49. _LOGGER.warning(
  50. "Unrecognized number device class of %s ignored", dclass
  51. )
  52. @property
  53. def native_min_value(self):
  54. if self._min_dps is not None:
  55. return self._min_dps.get_value(self._device)
  56. r = self._value_dps.range(self._device)
  57. return DEFAULT_MIN_VALUE if r is None else r["min"]
  58. @property
  59. def native_max_value(self):
  60. if self._max_dps is not None:
  61. return self._max_dps.get_value(self._device)
  62. r = self._value_dps.range(self._device)
  63. return DEFAULT_MAX_VALUE if r is None else r["max"]
  64. @property
  65. def native_step(self):
  66. return self._value_dps.step(self._device)
  67. @property
  68. def mode(self):
  69. """Return the mode."""
  70. m = self._config.mode
  71. if m is None:
  72. m = MODE_AUTO
  73. return m
  74. @property
  75. def native_unit_of_measurement(self):
  76. """Return the unit associated with this number."""
  77. if self._unit_dps is None:
  78. unit = self._value_dps.unit
  79. else:
  80. unit = self._unit_dps.get_value(self._device)
  81. return unit_from_ascii(unit)
  82. @property
  83. def native_value(self):
  84. """Return the current value of the number."""
  85. return self._value_dps.get_value(self._device)
  86. async def async_set_native_value(self, value):
  87. """Set the number."""
  88. await self._value_dps.async_set_value(self._device, value)