light.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. ATTR_EFFECT,
  10. COLOR_MODE_BRIGHTNESS,
  11. COLOR_MODE_ONOFF,
  12. COLOR_MODE_UNKNOWN,
  13. SUPPORT_EFFECT,
  14. )
  15. from ..device import TuyaLocalDevice
  16. from ..helpers.device_config import TuyaEntityConfig
  17. from ..helpers.mixin import TuyaLocalEntity
  18. class TuyaLocalLight(TuyaLocalEntity, LightEntity):
  19. """Representation of a Tuya WiFi-connected light."""
  20. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  21. """
  22. Initialize the light.
  23. Args:
  24. device (TuyaLocalDevice): The device API instance.
  25. config (TuyaEntityConfig): The configuration for this entity.
  26. """
  27. dps_map = self._init_begin(device, config)
  28. self._switch_dps = dps_map.pop("switch", None)
  29. self._brightness_dps = dps_map.pop("brightness", None)
  30. self._effect_dps = dps_map.pop("effect", None)
  31. self._init_end(dps_map)
  32. @property
  33. def supported_color_modes(self):
  34. """Return the supported color modes for this light."""
  35. if self._brightness_dps:
  36. return [COLOR_MODE_BRIGHTNESS]
  37. elif self._switch_dps:
  38. return [COLOR_MODE_ONOFF]
  39. else:
  40. return []
  41. @property
  42. def supported_features(self):
  43. """Return the supported features for this light."""
  44. if self._effect_dps:
  45. return SUPPORT_EFFECT
  46. else:
  47. return 0
  48. @property
  49. def color_mode(self):
  50. """Return the color mode of the light"""
  51. if self._brightness_dps:
  52. return COLOR_MODE_BRIGHTNESS
  53. elif self._switch_dps:
  54. return COLOR_MODE_ONOFF
  55. else:
  56. return COLOR_MODE_UNKNOWN
  57. @property
  58. def is_on(self):
  59. """Return the current state."""
  60. if self._switch_dps:
  61. return self._switch_dps.get_value(self._device)
  62. elif self._brightness_dps:
  63. b = self.brightness
  64. return isinstance(b, int) and b > 0
  65. else:
  66. # There shouldn't be lights without control, but if there are, assume always on if they are responding
  67. return self.available
  68. @property
  69. def brightness(self):
  70. """Get the current brightness of the light"""
  71. if self._brightness_dps is None:
  72. return None
  73. return self._brightness_dps.get_value(self._device)
  74. @property
  75. def effect_list(self):
  76. """Return the list of valid effects for the light"""
  77. if self._effect_dps is None:
  78. return None
  79. return self._effect_dps.values(self._device)
  80. @property
  81. def effect(self):
  82. """Return the current effect setting of this light"""
  83. if self._effect_dps is None:
  84. return None
  85. return self._effect_dps.get_value(self._device)
  86. async def async_turn_on(self, **params):
  87. settings = {}
  88. if self._switch_dps:
  89. settings = {
  90. **settings,
  91. **self._switch_dps.get_values_to_set(self._device, True),
  92. }
  93. if self._brightness_dps:
  94. bright = params.get(ATTR_BRIGHTNESS, 255)
  95. bright_values = self._brightness_dps.get_values_to_set(self._device, bright)
  96. settings = {
  97. **settings,
  98. **bright_values,
  99. }
  100. if self._effect_dps:
  101. effect = params.get(ATTR_EFFECT, None)
  102. if effect:
  103. effect_values = self._effect_dps.get_values_to_set(self._device, effect)
  104. settings = {
  105. **settings,
  106. **effect_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. else:
  115. raise NotImplementedError()
  116. async def async_toggle(self):
  117. dps_display_on = self.is_on
  118. await (self.async_turn_on() if not dps_display_on else self.async_turn_off())