number.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_MIN_VALUE,
  8. DEFAULT_MAX_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.name} 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. "Unrecognized number device class of %s ignored", dclass
  54. )
  55. @property
  56. def native_min_value(self):
  57. if self._min_dps is not None:
  58. return self._min_dps.get_value(self._device)
  59. r = self._value_dps.range(self._device)
  60. return DEFAULT_MIN_VALUE if r is None else r["min"]
  61. @property
  62. def native_max_value(self):
  63. if self._max_dps is not None:
  64. return self._max_dps.get_value(self._device)
  65. r = self._value_dps.range(self._device)
  66. return DEFAULT_MAX_VALUE if r is None else r["max"]
  67. @property
  68. def native_step(self):
  69. return self._value_dps.step(self._device)
  70. @property
  71. def mode(self):
  72. """Return the mode."""
  73. m = self._config.mode
  74. if m is None:
  75. m = MODE_AUTO
  76. return m
  77. @property
  78. def native_unit_of_measurement(self):
  79. """Return the unit associated with this number."""
  80. if self._unit_dps is None:
  81. unit = self._value_dps.unit
  82. else:
  83. unit = self._unit_dps.get_value(self._device)
  84. return unit_from_ascii(unit)
  85. @property
  86. def native_value(self):
  87. """Return the current value of the number."""
  88. return self._value_dps.get_value(self._device)
  89. async def async_set_native_value(self, value):
  90. """Set the number."""
  91. await self._value_dps.async_set_value(self._device, value)