number.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. )
  9. from .device import TuyaLocalDevice
  10. from .helpers.config import async_tuya_setup_platform
  11. from .helpers.device_config import TuyaEntityConfig
  12. from .helpers.mixin import TuyaLocalEntity, unit_from_ascii
  13. MODE_AUTO = "auto"
  14. async def async_setup_entry(hass, config_entry, async_add_entities):
  15. config = {**config_entry.data, **config_entry.options}
  16. await async_tuya_setup_platform(
  17. hass,
  18. async_add_entities,
  19. config,
  20. "number",
  21. TuyaLocalNumber,
  22. )
  23. class TuyaLocalNumber(TuyaLocalEntity, NumberEntity):
  24. """Representation of a Tuya Number"""
  25. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  26. """
  27. Initialise the sensor.
  28. Args:
  29. device (TuyaLocalDevice): the device API instance
  30. config (TuyaEntityConfig): the configuration for this entity
  31. """
  32. dps_map = self._init_begin(device, config)
  33. self._value_dps = dps_map.pop("value")
  34. if self._value_dps is None:
  35. raise AttributeError(f"{config.name} is missing a value dps")
  36. self._unit_dps = dps_map.pop("unit", None)
  37. self._min_dps = dps_map.pop("minimum", None)
  38. self._max_dps = dps_map.pop("maximum", None)
  39. self._init_end(dps_map)
  40. @property
  41. def native_min_value(self):
  42. if self._min_dps is not None:
  43. return self._min_dps.get_value(self._device)
  44. r = self._value_dps.range(self._device)
  45. return DEFAULT_MIN_VALUE if r is None else r["min"]
  46. @property
  47. def native_max_value(self):
  48. if self._max_dps is not None:
  49. return self._max_dps.get_value(self._device)
  50. r = self._value_dps.range(self._device)
  51. return DEFAULT_MAX_VALUE if r is None else r["max"]
  52. @property
  53. def native_step(self):
  54. return self._value_dps.step(self._device)
  55. @property
  56. def mode(self):
  57. """Return the mode."""
  58. m = self._config.mode
  59. if m is None:
  60. m = MODE_AUTO
  61. return m
  62. @property
  63. def native_unit_of_measurement(self):
  64. """Return the unit associated with this number."""
  65. if self._unit_dps is None:
  66. unit = self._value_dps.unit
  67. else:
  68. unit = self._unit_dps.get_value(self._device)
  69. return unit_from_ascii(unit)
  70. @property
  71. def native_value(self):
  72. """Return the current value of the number."""
  73. return self._value_dps.get_value(self._device)
  74. async def async_set_native_value(self, value):
  75. """Set the number."""
  76. await self._value_dps.async_set_value(self._device, value)