test_rgbcw_lightbulb.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. from homeassistant.components.light import (
  2. EFFECT_OFF,
  3. ColorMode,
  4. LightEntityFeature,
  5. )
  6. from ..const import RGBCW_LIGHTBULB_PAYLOAD
  7. from ..helpers import assert_device_properties_set
  8. from ..mixins.text import TEXT_PATTERN_HEX, BasicTextTests
  9. from .base_device_tests import TuyaDeviceTestCase
  10. SWITCH_DPS = "20"
  11. MODE_DPS = "21"
  12. BRIGHTNESS_DPS = "22"
  13. COLORTEMP_DPS = "23"
  14. HSV_DPS = "24"
  15. SCENE_DPS = "25"
  16. TIMER_DPS = "26"
  17. class TestRGBCWLightbulb(BasicTextTests, TuyaDeviceTestCase):
  18. __test__ = True
  19. def setUp(self):
  20. self.setUpForConfig("rgbcw_lightbulb.yaml", RGBCW_LIGHTBULB_PAYLOAD)
  21. self.subject = self.entities.get("light")
  22. self.setUpBasicText(
  23. SCENE_DPS,
  24. self.entities.get("text_scene"),
  25. pattern=TEXT_PATTERN_HEX,
  26. )
  27. self.mark_secondary(
  28. [
  29. "select_scene",
  30. "text_scene",
  31. "time_timer",
  32. "switch_do_not_disturb",
  33. "text_music",
  34. "text_control",
  35. "text_debug",
  36. "text_rhythm",
  37. "text_sleep",
  38. "text_wakeup",
  39. ]
  40. )
  41. def test_is_on(self):
  42. self.dps[SWITCH_DPS] = True
  43. self.assertTrue(self.subject.is_on)
  44. self.dps[SWITCH_DPS] = False
  45. self.assertFalse(self.subject.is_on)
  46. def test_brightness(self):
  47. self.dps[BRIGHTNESS_DPS] = 500
  48. self.assertAlmostEqual(self.subject.brightness, 126, 0)
  49. def test_color_temp(self):
  50. self.dps[COLORTEMP_DPS] = 500
  51. self.assertEqual(self.subject.color_temp_kelvin, 4600)
  52. self.dps[COLORTEMP_DPS] = 1000
  53. self.assertEqual(self.subject.color_temp_kelvin, 6500)
  54. self.dps[COLORTEMP_DPS] = 0
  55. self.assertEqual(self.subject.color_temp_kelvin, 2700)
  56. self.dps[COLORTEMP_DPS] = None
  57. self.assertEqual(self.subject.color_temp_kelvin, None)
  58. def test_color_temp_range(self):
  59. self.assertEqual(self.subject.min_color_temp_kelvin, 2700)
  60. self.assertEqual(self.subject.max_color_temp_kelvin, 6500)
  61. def test_color_mode(self):
  62. self.dps[MODE_DPS] = "white"
  63. self.assertEqual(self.subject.color_mode, ColorMode.COLOR_TEMP)
  64. self.dps[MODE_DPS] = "colour"
  65. self.assertEqual(self.subject.color_mode, ColorMode.HS)
  66. self.dps[MODE_DPS] = "scene"
  67. self.assertEqual(self.subject.color_mode, ColorMode.HS)
  68. self.dps[MODE_DPS] = "music"
  69. self.assertEqual(self.subject.color_mode, ColorMode.HS)
  70. def test_hs_color(self):
  71. self.dps[HSV_DPS] = "003c03e803e8"
  72. self.dps[BRIGHTNESS_DPS] = 1000
  73. self.assertSequenceEqual(self.subject.hs_color, (60, 100))
  74. # Lights have been observed to return N, O and P mixed in with the hex
  75. # number. Maybe it has some special meaning, but since it is undocumented,
  76. # we just want to reject such values without an exception.
  77. def test_invalid_hs_color(self):
  78. self.dps[HSV_DPS] = "0010001000OP"
  79. self.dps[BRIGHTNESS_DPS] = 1000
  80. self.assertIsNone(self.subject.hs_color)
  81. def test_effect_list(self):
  82. self.assertCountEqual(
  83. self.subject.effect_list,
  84. ["Scene", "Music", EFFECT_OFF],
  85. )
  86. def test_effect(self):
  87. self.dps[MODE_DPS] = "scene"
  88. self.assertEqual(self.subject.effect, "Scene")
  89. self.dps[MODE_DPS] = "music"
  90. self.assertEqual(self.subject.effect, "Music")
  91. self.dps[MODE_DPS] = "white"
  92. self.assertEqual(self.subject.effect, EFFECT_OFF)
  93. self.dps[MODE_DPS] = "colour"
  94. self.assertEqual(self.subject.effect, EFFECT_OFF)
  95. def test_supported_color_modes(self):
  96. self.assertCountEqual(
  97. self.subject.supported_color_modes,
  98. {ColorMode.HS, ColorMode.COLOR_TEMP},
  99. )
  100. def test_supported_features(self):
  101. self.assertEqual(self.subject.supported_features, LightEntityFeature.EFFECT)
  102. async def test_turn_on(self):
  103. self.dps[SWITCH_DPS] = False
  104. async with assert_device_properties_set(
  105. self.subject._device,
  106. {SWITCH_DPS: True},
  107. ):
  108. await self.subject.async_turn_on()
  109. async def test_turn_off(self):
  110. async with assert_device_properties_set(
  111. self.subject._device,
  112. {SWITCH_DPS: False},
  113. ):
  114. await self.subject.async_turn_off()
  115. async def test_set_brightness_white(self):
  116. self.dps[SWITCH_DPS] = True
  117. self.dps[MODE_DPS] = "white"
  118. async with assert_device_properties_set(
  119. self.subject._device,
  120. {
  121. BRIGHTNESS_DPS: 506,
  122. },
  123. ):
  124. await self.subject.async_turn_on(brightness=128)
  125. async def test_set_brightness_color(self):
  126. self.dps[SWITCH_DPS] = True
  127. self.dps[MODE_DPS] = "colour"
  128. self.dps[HSV_DPS] = "000003e803e8"
  129. async with assert_device_properties_set(
  130. self.subject._device,
  131. {
  132. HSV_DPS: "000003e801f6",
  133. },
  134. ):
  135. await self.subject.async_turn_on(brightness=128)
  136. async def test_set_hs_color(self):
  137. self.dps[BRIGHTNESS_DPS] = 1000
  138. self.dps[SWITCH_DPS] = True
  139. self.dps[MODE_DPS] = "colour"
  140. async with assert_device_properties_set(
  141. self.subject._device,
  142. {
  143. HSV_DPS: "000003e803e8",
  144. },
  145. ):
  146. await self.subject.async_turn_on(
  147. hs_color=(0, 100),
  148. )
  149. async def test_set_hs_from_white(self):
  150. self.dps[BRIGHTNESS_DPS] = 1000
  151. self.dps[SWITCH_DPS] = True
  152. self.dps[MODE_DPS] = "white"
  153. async with assert_device_properties_set(
  154. self.subject._device,
  155. {
  156. MODE_DPS: "colour",
  157. HSV_DPS: "000003e803e8",
  158. },
  159. ):
  160. await self.subject.async_turn_on(
  161. hs_color=(0, 100),
  162. )
  163. def test_available(self):
  164. self.assertFalse(self.entities.get("switch_do_not_disturb").available)
  165. self.assertTrue(self.subject.available)
  166. def test_disabled_by_default(self):
  167. self.assertFalse(self.basicText.entity_registry_enabled_default)
  168. self.assertTrue(self.subject.entity_registry_enabled_default)