number.py 2.6 KB

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