test_rgbcw_lightbulb.py 6.2 KB

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