number.py 3.5 KB

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