test_rgbcw_lightbulb.py 5.7 KB

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