test_rgbcw_lightbulb.py 6.2 KB

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