test_rgbcw_lightbulb.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. ]
  34. )
  35. def test_is_on(self):
  36. self.dps[SWITCH_DPS] = True
  37. self.assertTrue(self.subject.is_on)
  38. self.dps[SWITCH_DPS] = False
  39. self.assertFalse(self.subject.is_on)
  40. def test_brightness(self):
  41. self.dps[BRIGHTNESS_DPS] = 500
  42. self.assertAlmostEqual(self.subject.brightness, 126, 0)
  43. def test_color_temp(self):
  44. self.dps[COLORTEMP_DPS] = 500
  45. self.assertEqual(self.subject.color_temp_kelvin, 4600)
  46. self.dps[COLORTEMP_DPS] = 1000
  47. self.assertEqual(self.subject.color_temp_kelvin, 6500)
  48. self.dps[COLORTEMP_DPS] = 0
  49. self.assertEqual(self.subject.color_temp_kelvin, 2700)
  50. self.dps[COLORTEMP_DPS] = None
  51. self.assertEqual(self.subject.color_temp_kelvin, None)
  52. def test_color_temp_range(self):
  53. self.assertEqual(self.subject.min_color_temp_kelvin, 2700)
  54. self.assertEqual(self.subject.max_color_temp_kelvin, 6500)
  55. def test_color_mode(self):
  56. self.dps[MODE_DPS] = "white"
  57. self.assertEqual(self.subject.color_mode, ColorMode.COLOR_TEMP)
  58. self.dps[MODE_DPS] = "colour"
  59. self.assertEqual(self.subject.color_mode, ColorMode.HS)
  60. self.dps[MODE_DPS] = "scene"
  61. self.assertEqual(self.subject.color_mode, ColorMode.HS)
  62. self.dps[MODE_DPS] = "music"
  63. self.assertEqual(self.subject.color_mode, ColorMode.HS)
  64. def test_hs_color(self):
  65. self.dps[HSV_DPS] = "003c03e803e8"
  66. self.dps[BRIGHTNESS_DPS] = 1000
  67. self.assertSequenceEqual(self.subject.hs_color, (60, 100))
  68. # Lights have been observed to return N, O and P mixed in with the hex
  69. # number. Maybe it has some special meaning, but since it is undocumented,
  70. # we just want to reject such values without an exception.
  71. def test_invalid_hs_color(self):
  72. self.dps[HSV_DPS] = "0010001000OP"
  73. self.dps[BRIGHTNESS_DPS] = 1000
  74. self.assertIsNone(self.subject.hs_color)
  75. def test_effect_list(self):
  76. self.assertCountEqual(
  77. self.subject.effect_list,
  78. ["Scene", "Music", EFFECT_OFF],
  79. )
  80. def test_effect(self):
  81. self.dps[MODE_DPS] = "scene"
  82. self.assertEqual(self.subject.effect, "Scene")
  83. self.dps[MODE_DPS] = "music"
  84. self.assertEqual(self.subject.effect, "Music")
  85. self.dps[MODE_DPS] = "white"
  86. self.assertEqual(self.subject.effect, EFFECT_OFF)
  87. self.dps[MODE_DPS] = "colour"
  88. self.assertEqual(self.subject.effect, EFFECT_OFF)
  89. def test_supported_color_modes(self):
  90. self.assertCountEqual(
  91. self.subject.supported_color_modes,
  92. {ColorMode.HS, ColorMode.COLOR_TEMP},
  93. )
  94. def test_supported_features(self):
  95. self.assertEqual(self.subject.supported_features, LightEntityFeature.EFFECT)
  96. async def test_turn_on(self):
  97. self.dps[SWITCH_DPS] = False
  98. async with assert_device_properties_set(
  99. self.subject._device,
  100. {SWITCH_DPS: True},
  101. ):
  102. await self.subject.async_turn_on()
  103. async def test_turn_off(self):
  104. async with assert_device_properties_set(
  105. self.subject._device,
  106. {SWITCH_DPS: False},
  107. ):
  108. await self.subject.async_turn_off()
  109. async def test_set_brightness_white(self):
  110. self.dps[SWITCH_DPS] = True
  111. self.dps[MODE_DPS] = "white"
  112. async with assert_device_properties_set(
  113. self.subject._device,
  114. {
  115. BRIGHTNESS_DPS: 506,
  116. },
  117. ):
  118. await self.subject.async_turn_on(brightness=128)
  119. async def test_set_brightness_color(self):
  120. self.dps[SWITCH_DPS] = True
  121. self.dps[MODE_DPS] = "colour"
  122. self.dps[HSV_DPS] = "000003e803e8"
  123. async with assert_device_properties_set(
  124. self.subject._device,
  125. {
  126. HSV_DPS: "000003e801f6",
  127. },
  128. ):
  129. await self.subject.async_turn_on(brightness=128)
  130. async def test_set_hs_color(self):
  131. self.dps[BRIGHTNESS_DPS] = 1000
  132. self.dps[SWITCH_DPS] = True
  133. self.dps[MODE_DPS] = "colour"
  134. async with assert_device_properties_set(
  135. self.subject._device,
  136. {
  137. HSV_DPS: "000003e803e8",
  138. },
  139. ):
  140. await self.subject.async_turn_on(
  141. hs_color=(0, 100),
  142. )
  143. async def test_set_hs_from_white(self):
  144. self.dps[BRIGHTNESS_DPS] = 1000
  145. self.dps[SWITCH_DPS] = True
  146. self.dps[MODE_DPS] = "white"
  147. async with assert_device_properties_set(
  148. self.subject._device,
  149. {
  150. MODE_DPS: "colour",
  151. HSV_DPS: "000003e803e8",
  152. },
  153. ):
  154. await self.subject.async_turn_on(
  155. hs_color=(0, 100),
  156. )
  157. def test_available(self):
  158. self.assertFalse(self.entities.get("switch_do_not_disturb").available)
  159. self.assertTrue(self.subject.available)
  160. def test_disabled_by_default(self):
  161. self.assertFalse(self.basicText.entity_registry_enabled_default)
  162. self.assertTrue(self.subject.entity_registry_enabled_default)