test_rgbcw_lightbulb.py 6.1 KB

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