number.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 .entity import TuyaLocalEntity, unit_from_ascii
  13. from .helpers.config import async_tuya_setup_platform
  14. from .helpers.device_config import TuyaEntityConfig
  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. minimum = self._min_dps.get_value(self._device)
  62. if minimum is not None:
  63. return minimum
  64. r = self._value_dps.range(self._device)
  65. return DEFAULT_MIN_VALUE if r is None else r[0]
  66. @property
  67. def native_max_value(self):
  68. if self._max_dps is not None:
  69. maximum = self._max_dps.get_value(self._device)
  70. if maximum is not None:
  71. return maximum
  72. r = self._value_dps.range(self._device)
  73. return DEFAULT_MAX_VALUE if r is None else r[1]
  74. @property
  75. def native_step(self):
  76. return self._value_dps.step(self._device)
  77. @property
  78. def mode(self):
  79. """Return the mode."""
  80. m = self._config.mode
  81. if m is None:
  82. m = MODE_AUTO
  83. return m
  84. @property
  85. def native_unit_of_measurement(self):
  86. """Return the unit associated with this number."""
  87. if self._unit_dps is None:
  88. unit = self._value_dps.unit
  89. else:
  90. unit = self._unit_dps.get_value(self._device)
  91. return unit_from_ascii(unit)
  92. @property
  93. def native_value(self):
  94. """Return the current value of the number."""
  95. return self._value_dps.get_value(self._device)
  96. async def async_set_native_value(self, value):
  97. """Set the number."""
  98. await self._value_dps.async_set_value(self._device, value)