time.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. Setup for Tuya time entities
  3. """
  4. import logging
  5. from datetime import time, timedelta, datetime
  6. from homeassistant.components.time import TimeEntity
  7. from homeassistant.const import UnitOfTime
  8. from .device import TuyaLocalDevice
  9. from .entity import TuyaLocalEntity, unit_from_ascii
  10. from .helpers.config import async_tuya_setup_platform
  11. from .helpers.device_config import TuyaEntityConfig
  12. _LOGGER = logging.getLogger(__name__)
  13. MODE_AUTO = "auto"
  14. MIDNIGHT = datetime.combine(datetime.today(), time(0, 0, 0))
  15. async def async_setup_entry(hass, config_entry, async_add_entities):
  16. config = {**config_entry.data, **config_entry.options}
  17. await async_tuya_setup_platform(
  18. hass,
  19. async_add_entities,
  20. config,
  21. "time",
  22. TuyaLocalTime,
  23. )
  24. class TuyaLocalTime(TuyaLocalEntity, TimeEntity):
  25. """Representation of a Tuya Time"""
  26. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  27. """
  28. Initialise the time entity.
  29. Args:
  30. device (TuyaLocalDevice): the device API instance
  31. config (TuyaEntityConfig): the configuration for this entity
  32. """
  33. super().__init__()
  34. dps_map = self._init_begin(device, config)
  35. self._hour_dps = dps_map.pop("hour", None)
  36. self._minute_dps = dps_map.pop("minute", None)
  37. self._second_dps = dps_map.pop("second", None)
  38. if (
  39. self._hour_dps is None
  40. and self._minute_dps is None
  41. and self._second_dps is None
  42. ):
  43. raise AttributeError(
  44. f"{config.config_id} is missing an hour, minute or second dp"
  45. )
  46. self._init_end(dps_map)
  47. @property
  48. def native_value(self):
  49. """Return the current value of the time."""
  50. hours = minutes = seconds = 0
  51. if self._hour_dps:
  52. hours = self._hour_dps.get_value(self._device)
  53. if self._minute_dps:
  54. minutes = self._minute_dps.get_value(self._device)
  55. if self._second_dps:
  56. seconds = self._second_dps.get_value(self._device)
  57. delta = timedelta(hours=hours, minutes=minutes, seconds=seconds)
  58. return (MIDNIGHT + delta).time()
  59. async def async_set_native_value(self, value: time):
  60. """Set the number."""
  61. settings = {}
  62. hours = value.hour
  63. minutes = value.minute
  64. seconds = value.second
  65. if self._hour_dps:
  66. settings.update(self._hour_dps.get_values_to_set(self._device, hours))
  67. else:
  68. minutes = minutes + hours * 60
  69. if self._minute_dps:
  70. settings.update(self._minute_dps.get_values_to_set(self._device, minutes))
  71. else:
  72. seconds = seconds + minutes * 60
  73. if self._second_dps:
  74. settings.update(self._second_dps.get_values_to_set(self._device, seconds))
  75. else:
  76. _LOGGER.debug(
  77. "%s: Discarding unused precision: %d seconds",
  78. self.name,
  79. seconds,
  80. )
  81. await self._device.async_set_properties(settings)