test_rgbcw_lightbulb.py 5.9 KB

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