number.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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._decimal_dps = dps_map.pop("decimal", None)
  44. self._init_end(dps_map)
  45. @property
  46. def device_class(self):
  47. """Return the class of this device"""
  48. dclass = self._config.device_class
  49. if dclass:
  50. try:
  51. return NumberDeviceClass(dclass)
  52. except ValueError:
  53. _LOGGER.warning(
  54. "%s/%s: Unrecognized number device class of %s ignored",
  55. self._config._device.config,
  56. self.name or "number",
  57. dclass,
  58. )
  59. @property
  60. def native_min_value(self):
  61. if self._min_dps is not None:
  62. minimum = self._min_dps.get_value(self._device)
  63. if minimum is not None:
  64. return minimum
  65. r = self._value_dps.range(self._device)
  66. return DEFAULT_MIN_VALUE if r is None else r[0]
  67. @property
  68. def native_max_value(self):
  69. if self._max_dps is not None:
  70. maximum = self._max_dps.get_value(self._device)
  71. if maximum is not None:
  72. return maximum
  73. r = self._value_dps.range(self._device)
  74. return DEFAULT_MAX_VALUE if r is None else r[1]
  75. @property
  76. def native_step(self):
  77. return self._value_dps.step(self._device)
  78. @property
  79. def mode(self):
  80. """Return the mode."""
  81. m = self._config.mode
  82. if m is None:
  83. m = MODE_AUTO
  84. return m
  85. @property
  86. def native_unit_of_measurement(self):
  87. """Return the unit associated with this number."""
  88. if self._unit_dps is None:
  89. unit = self._value_dps.unit
  90. else:
  91. unit = self._unit_dps.get_value(self._device)
  92. return unit_from_ascii(unit)
  93. @property
  94. def native_value(self):
  95. """Return the current value of the number."""
  96. val = self._value_dps.get_value(self._device)
  97. if self._decimal_dps is not None:
  98. decimal = self._decimal_dps.get_value(self._device)
  99. if decimal is not None:
  100. val = val + decimal
  101. return val
  102. async def async_set_native_value(self, value):
  103. """Set the number."""
  104. _LOGGER.info("%s setting value to %s", self._config.config_id, value)
  105. settings = {}
  106. if self._decimal_dps is not None:
  107. whole = int(value)
  108. decimal = value - whole
  109. settings = self._decimal_dps.get_values_to_set(self._device, decimal)
  110. value = whole
  111. settings = settings | self._value_dps.get_values_to_set(
  112. self._device, value, settings
  113. )
  114. await self._device.async_set_properties(settings)