test_rgbcw_lightbulb.py 6.0 KB

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