light.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 supported_color_modes(self):
  63. """Return the supported color modes for this 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 []
  70. @property
  71. def color_mode(self):
  72. """Return the color mode of the light"""
  73. if self._brightness_dps:
  74. return COLOR_MODE_BRIGHTNESS
  75. elif self._switch_dps:
  76. return COLOR_MODE_ONOFF
  77. else:
  78. return COLOR_MODE_UNKNOWN
  79. @property
  80. def is_on(self):
  81. """Return the current state."""
  82. if self._switch_dps:
  83. return self._switch_dps.get_value(self._device)
  84. elif self._brightness_dps:
  85. b = self.brightness
  86. return isinstance(b, int) and b > 0
  87. else:
  88. # There shouldn't be lights without control, but if there are, assume always on
  89. return True
  90. @property
  91. def brightness(self):
  92. """Get the current brightness of the light"""
  93. if self._brightness_dps is None:
  94. return None
  95. return self._brightness_dps.get_value(self._device)
  96. @property
  97. def device_state_attributes(self):
  98. """Get additional attributes that the integration itself does not support."""
  99. attr = {}
  100. for a in self._attr_dps:
  101. attr[a.name] = a.get_value(self._device)
  102. return attr
  103. async def async_turn_on(self, **params):
  104. settings = {}
  105. if self._switch_dps:
  106. settings = {
  107. **settings,
  108. **self._switch_dps.get_values_to_set(self._device, True),
  109. }
  110. if self._brightness_dps:
  111. bright = params.get(ATTR_BRIGHTNESS, 255)
  112. bright_values = self._brightness_dps.get_values_to_set(self._device, bright)
  113. settings = {
  114. **settings,
  115. **bright_values,
  116. }
  117. await self._device.async_set_properties(settings)
  118. async def async_turn_off(self):
  119. if self._switch_dps:
  120. await self._switch_dps.async_set_value(self._device, False)
  121. elif self._brightness_dps:
  122. await self._brightness_dps.async_set_value(self._device, 0)
  123. async def async_toggle(self):
  124. dps_display_on = self.is_on
  125. await (self.async_turn_on() if not dps_display_on else self.async_turn_off())
  126. async def async_update(self):
  127. await self._device.async_refresh()