test_rgbcw_lightbulb.py 6.5 KB

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