4
0

test_rgbcw_lightbulb.py 5.8 KB

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