number.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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
  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._init_end(dps_map)
  27. @property
  28. def min_value(self):
  29. r = self._value_dps.range(self._device)
  30. return DEFAULT_MIN_VALUE if r is None else r["min"]
  31. @property
  32. def max_value(self):
  33. r = self._value_dps.range(self._device)
  34. return DEFAULT_MAX_VALUE if r is None else r["max"]
  35. @property
  36. def step(self):
  37. return self._value_dps.step(self._device)
  38. @property
  39. def mode(self):
  40. """Return the mode."""
  41. m = self._config.mode
  42. if m is None:
  43. m = MODE_AUTO
  44. return m
  45. @property
  46. def value(self):
  47. """Return the current value of the number."""
  48. return self._value_dps.get_value(self._device)
  49. async def async_set_value(self, value):
  50. """Set the number."""
  51. await self._value_dps.async_set_value(self._device, value)