test_rgbcw_lightbulb.py 5.9 KB

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