test_rgbcw_lightbulb.py 6.5 KB

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