light.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """
  2. Platform to control Tuya lights.
  3. Initially based on the secondary panel lighting control on some climate
  4. devices, so only providing simple on/off control.
  5. """
  6. from homeassistant.components.light import (
  7. LightEntity,
  8. ATTR_BRIGHTNESS,
  9. COLOR_MODE_BRIGHTNESS,
  10. COLOR_MODE_ONOFF,
  11. COLOR_MODE_UNKNOWN,
  12. )
  13. from ..device import TuyaLocalDevice
  14. from ..helpers.device_config import TuyaEntityConfig
  15. class TuyaLocalLight(LightEntity):
  16. """Representation of a Tuya WiFi-connected light."""
  17. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  18. """
  19. Initialize the light.
  20. Args:
  21. device (TuyaLocalDevice): The device API instance.
  22. config (TuyaEntityConfig): The configuration for this entity.
  23. """
  24. self._device = device
  25. self._config = config
  26. self._attr_dps = []
  27. dps_map = {c.name: c for c in config.dps()}
  28. self._switch_dps = dps_map.pop("switch", None)
  29. self._brightness_dps = dps_map.pop("brightness", None)
  30. for d in dps_map.values():
  31. if not d.hidden:
  32. self._attr_dps.append(d)
  33. @property
  34. def should_poll(self):
  35. """Return the polling state."""
  36. return True
  37. @property
  38. def name(self):
  39. """Return the name of the light."""
  40. return self._device.name
  41. @property
  42. def friendly_name(self):
  43. """Return the friendly name for this entity."""
  44. return self._config.name
  45. @property
  46. def unique_id(self):
  47. """Return the unique id for this heater LED display."""
  48. return self._device.unique_id
  49. @property
  50. def device_info(self):
  51. """Return device information about this heater LED display."""
  52. return self._device.device_info
  53. @property
  54. def icon(self):
  55. """Return the icon to use in the frontend for this device."""
  56. icon = self._config.icon(self._device)
  57. if icon:
  58. return icon
  59. else:
  60. return super().icon
  61. @property
  62. def color_mode(self):
  63. """Return the color mode of the light"""
  64. if self._brightness_dps:
  65. return COLOR_MODE_BRIGHTNESS
  66. elif self._switch_dps:
  67. return COLOR_MODE_ONOFF
  68. else:
  69. return COLOR_MODE_UNKNOWN
  70. @property
  71. def is_on(self):
  72. """Return the current state."""
  73. if self._switch_dps:
  74. return self._switch_dps.get_value(self._device)
  75. elif self._brightness_dps:
  76. b = self.brightness
  77. return isinstance(b, int) and b > 0
  78. else:
  79. # There shouldn't be lights without control, but if there are, assume always on
  80. return True
  81. @property
  82. def brightness(self):
  83. """Get the current brightness of the light"""
  84. if self._brightness_dps is None:
  85. return None
  86. return self._brightness_dps.get_value(self._device)
  87. @property
  88. def device_state_attributes(self):
  89. """Get additional attributes that the integration itself does not support."""
  90. attr = {}
  91. for a in self._attr_dps:
  92. attr[a.name] = a.get_value(self._device)
  93. return attr
  94. async def async_turn_on(self, **params):
  95. settings = {}
  96. if self._switch_dps:
  97. settings = {
  98. **settings,
  99. **self._switch_dps.get_values_to_set(self._device, True),
  100. }
  101. if self._brightness_dps:
  102. bright = params.get(ATTR_BRIGHTNESS, 255)
  103. bright_values = self._brightness_dps.get_values_to_set(self._device, bright)
  104. settings = {
  105. **settings,
  106. **bright_values,
  107. }
  108. await self._device.async_set_properties(settings)
  109. async def async_turn_off(self):
  110. if self._switch_dps:
  111. await self._switch_dps.async_set_value(self._device, False)
  112. elif self._brightness_dps:
  113. await self._brightness_dps.async_set_value(self._device, 0)
  114. async def async_toggle(self):
  115. dps_display_on = self.is_on
  116. await (self.async_turn_on() if not dps_display_on else self.async_turn_off())
  117. async def async_update(self):
  118. await self._device.async_refresh()