test_rgbcw_lightbulb.py 6.5 KB

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