test_rgbcw_lightbulb.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from homeassistant.components.light import (
  2. ColorMode,
  3. LightEntityFeature,
  4. EFFECT_COLORLOOP,
  5. EFFECT_RANDOM,
  6. )
  7. from homeassistant.const import TIME_MINUTES
  8. from ..const import RGBCW_LIGHTBULB_PAYLOAD
  9. from ..helpers import assert_device_properties_set
  10. from ..mixins.number import BasicNumberTests
  11. from .base_device_tests import TuyaDeviceTestCase
  12. SWITCH_DPS = "20"
  13. MODE_DPS = "21"
  14. BRIGHTNESS_DPS = "22"
  15. COLORTEMP_DPS = "23"
  16. HSV_DPS = "24"
  17. SCENE_DPS = "25"
  18. TIMER_DPS = "26"
  19. class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
  20. __test__ = True
  21. def setUp(self):
  22. self.setUpForConfig("rgbcw_lightbulb.yaml", RGBCW_LIGHTBULB_PAYLOAD)
  23. self.subject = self.entities.get("light")
  24. self.setUpBasicNumber(
  25. TIMER_DPS,
  26. self.entities.get("number_timer"),
  27. max=1440.0,
  28. unit=TIME_MINUTES,
  29. scale=60,
  30. )
  31. self.mark_secondary(["number_timer"])
  32. def test_is_on(self):
  33. self.dps[SWITCH_DPS] = True
  34. self.assertTrue(self.subject.is_on)
  35. self.dps[SWITCH_DPS] = False
  36. self.assertFalse(self.subject.is_on)
  37. def test_brightness(self):
  38. self.dps[BRIGHTNESS_DPS] = 500
  39. self.assertAlmostEqual(self.subject.brightness, 128, 0)
  40. def test_color_temp(self):
  41. self.dps[COLORTEMP_DPS] = 500
  42. self.assertAlmostEqual(self.subject.color_temp, 326, 0)
  43. self.dps[COLORTEMP_DPS] = 1000
  44. self.assertAlmostEqual(self.subject.color_temp, 153, 0)
  45. self.dps[COLORTEMP_DPS] = 0
  46. self.assertAlmostEqual(self.subject.color_temp, 500, 0)
  47. self.dps[COLORTEMP_DPS] = None
  48. self.assertEqual(self.subject.color_temp, None)
  49. def test_color_mode(self):
  50. self.dps[MODE_DPS] = "white"
  51. self.assertEqual(self.subject.color_mode, ColorMode.COLOR_TEMP)
  52. self.dps[MODE_DPS] = "colour"
  53. self.assertEqual(self.subject.color_mode, ColorMode.RGBW)
  54. self.dps[MODE_DPS] = "scene"
  55. self.assertEqual(self.subject.color_mode, ColorMode.RGBW)
  56. self.dps[MODE_DPS] = "music"
  57. self.assertEqual(self.subject.color_mode, ColorMode.RGBW)
  58. def test_rgbw_color(self):
  59. self.dps[HSV_DPS] = "003c03e803e8"
  60. self.dps[BRIGHTNESS_DPS] = 1000
  61. self.assertSequenceEqual(
  62. self.subject.rgbw_color,
  63. (255, 255, 0, 255),
  64. )
  65. def test_effect_list(self):
  66. self.assertCountEqual(
  67. self.subject.effect_list,
  68. [EFFECT_COLORLOOP, EFFECT_RANDOM],
  69. )
  70. def test_effect(self):
  71. self.dps[MODE_DPS] = "scene"
  72. self.assertEqual(self.subject.effect, EFFECT_COLORLOOP)
  73. self.dps[MODE_DPS] = "music"
  74. self.assertEqual(self.subject.effect, EFFECT_RANDOM)
  75. self.dps[MODE_DPS] = "white"
  76. self.assertIsNone(self.subject.effect)
  77. self.dps[MODE_DPS] = "colour"
  78. self.assertIsNone(self.subject.effect)
  79. def test_supported_color_modes(self):
  80. self.assertCountEqual(
  81. self.subject.supported_color_modes,
  82. {ColorMode.RGBW, ColorMode.COLOR_TEMP},
  83. )
  84. def test_supported_features(self):
  85. self.assertEqual(self.subject.supported_features, LightEntityFeature.EFFECT)
  86. async def test_turn_on(self):
  87. self.dps[SWITCH_DPS] = False
  88. async with assert_device_properties_set(
  89. self.subject._device,
  90. {SWITCH_DPS: True},
  91. ):
  92. await self.subject.async_turn_on()
  93. async def test_turn_off(self):
  94. async with assert_device_properties_set(
  95. self.subject._device,
  96. {SWITCH_DPS: False},
  97. ):
  98. await self.subject.async_turn_off()
  99. async def test_set_brightness(self):
  100. self.dps[SWITCH_DPS] = True
  101. async with assert_device_properties_set(
  102. self.subject._device,
  103. {
  104. MODE_DPS: "white",
  105. BRIGHTNESS_DPS: 502,
  106. },
  107. ):
  108. await self.subject.async_turn_on(color_mode=ColorMode.WHITE, brightness=128)
  109. async def test_set_rgbw(self):
  110. self.dps[BRIGHTNESS_DPS] = 1000
  111. self.dps[SWITCH_DPS] = True
  112. async with assert_device_properties_set(
  113. self.subject._device,
  114. {
  115. MODE_DPS: "colour",
  116. HSV_DPS: "000003e803e8",
  117. },
  118. ):
  119. await self.subject.async_turn_on(
  120. color_mode=ColorMode.RGBW,
  121. rgbw_color=(255, 0, 0, 255),
  122. )
  123. def test_extra_state_attributes(self):
  124. self.dps[SCENE_DPS] = "test"
  125. self.assertDictEqual(
  126. self.subject.extra_state_attributes,
  127. {
  128. "scene_data": "test",
  129. },
  130. )