test_moes_rgb_socket.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. """Tests for the MoesHouse RGBW smart socket."""
  2. from homeassistant.components.light import (
  3. ColorMode,
  4. EFFECT_COLORLOOP,
  5. LightEntityFeature,
  6. )
  7. from homeassistant.components.sensor import SensorDeviceClass
  8. from homeassistant.components.switch import SwitchDeviceClass
  9. from homeassistant.const import (
  10. ELECTRIC_CURRENT_MILLIAMPERE,
  11. ELECTRIC_POTENTIAL_VOLT,
  12. POWER_WATT,
  13. TIME_MINUTES,
  14. )
  15. from ..const import MOES_RGB_SOCKET_PAYLOAD
  16. from ..helpers import assert_device_properties_set
  17. from ..mixins.number import BasicNumberTests
  18. from ..mixins.sensor import MultiSensorTests
  19. from ..mixins.switch import BasicSwitchTests
  20. from .base_device_tests import TuyaDeviceTestCase
  21. LIGHT_DPS = "1"
  22. MODE_DPS = "2"
  23. BRIGHTNESS_DPS = "3"
  24. UNKNOWN4_DPS = "4"
  25. RGBW_DPS = "5"
  26. SCENE_DPS = "6"
  27. SCENE1_DPS = "7"
  28. SCENE2_DPS = "8"
  29. SCENE3_DPS = "9"
  30. SCENE4_DPS = "10"
  31. SWITCH_DPS = "101"
  32. TIMER_DPS = "102"
  33. CURRENT_DPS = "104"
  34. POWER_DPS = "105"
  35. VOLTAGE_DPS = "106"
  36. class TestMoesRGBWSocket(
  37. BasicNumberTests,
  38. MultiSensorTests,
  39. BasicSwitchTests,
  40. TuyaDeviceTestCase,
  41. ):
  42. __test__ = True
  43. def setUp(self):
  44. self.setUpForConfig("moes_rgb_socket.yaml", MOES_RGB_SOCKET_PAYLOAD)
  45. self.light = self.entities.get("light_night_light")
  46. self.setUpBasicSwitch(
  47. SWITCH_DPS,
  48. self.entities.get("switch"),
  49. device_class=SwitchDeviceClass.OUTLET,
  50. power_dps=POWER_DPS,
  51. power_scale=10,
  52. )
  53. self.setUpBasicNumber(
  54. TIMER_DPS,
  55. self.entities.get("number_timer"),
  56. max=1440.0,
  57. unit=TIME_MINUTES,
  58. scale=60,
  59. )
  60. self.setUpMultiSensors(
  61. [
  62. {
  63. "name": "sensor_voltage",
  64. "dps": VOLTAGE_DPS,
  65. "unit": ELECTRIC_POTENTIAL_VOLT,
  66. "device_class": SensorDeviceClass.VOLTAGE,
  67. "state_class": "measurement",
  68. "testdata": (2300, 230.0),
  69. },
  70. {
  71. "name": "sensor_current",
  72. "dps": CURRENT_DPS,
  73. "unit": ELECTRIC_CURRENT_MILLIAMPERE,
  74. "device_class": SensorDeviceClass.CURRENT,
  75. "state_class": "measurement",
  76. },
  77. {
  78. "name": "sensor_power",
  79. "dps": POWER_DPS,
  80. "unit": POWER_WATT,
  81. "device_class": SensorDeviceClass.POWER,
  82. "state_class": "measurement",
  83. "testdata": (1234, 123.4),
  84. },
  85. ]
  86. )
  87. self.mark_secondary(
  88. [
  89. "number_timer",
  90. "sensor_current",
  91. "sensor_power",
  92. "sensor_voltage",
  93. ]
  94. )
  95. def test_light_is_on(self):
  96. self.dps[LIGHT_DPS] = True
  97. self.assertTrue(self.light.is_on)
  98. self.dps[LIGHT_DPS] = False
  99. self.assertFalse(self.light.is_on)
  100. def test_light_brightness(self):
  101. self.dps[BRIGHTNESS_DPS] = 45
  102. self.assertEqual(self.light.brightness, 45)
  103. def test_light_color_mode(self):
  104. self.dps[MODE_DPS] = "colour"
  105. self.assertEqual(self.light.color_mode, ColorMode.RGBW)
  106. self.dps[MODE_DPS] = "white"
  107. self.assertEqual(self.light.color_mode, ColorMode.WHITE)
  108. self.dps[MODE_DPS] = "scene"
  109. self.assertEqual(self.light.color_mode, ColorMode.RGBW)
  110. self.dps[MODE_DPS] = "scene_1"
  111. self.assertEqual(self.light.color_mode, ColorMode.RGBW)
  112. self.dps[MODE_DPS] = "scene_2"
  113. self.assertEqual(self.light.color_mode, ColorMode.RGBW)
  114. self.dps[MODE_DPS] = "scene_3"
  115. self.assertEqual(self.light.color_mode, ColorMode.RGBW)
  116. self.dps[MODE_DPS] = "scene_4"
  117. self.assertEqual(self.light.color_mode, ColorMode.RGBW)
  118. def test_light_rgbw_color(self):
  119. self.dps[RGBW_DPS] = "ffff00003cffff"
  120. self.assertSequenceEqual(
  121. self.light.rgbw_color,
  122. (255, 255, 0, 255),
  123. )
  124. def test_light_effect_list(self):
  125. self.assertCountEqual(
  126. self.light.effect_list,
  127. [
  128. EFFECT_COLORLOOP,
  129. "Scene 1",
  130. "Scene 2",
  131. "Scene 3",
  132. "Scene 4",
  133. ],
  134. )
  135. def test_light_effect(self):
  136. self.dps[MODE_DPS] = "scene"
  137. self.assertEqual(self.light.effect, EFFECT_COLORLOOP)
  138. self.dps[MODE_DPS] = "scene_1"
  139. self.assertEqual(self.light.effect, "Scene 1")
  140. self.dps[MODE_DPS] = "scene_2"
  141. self.assertEqual(self.light.effect, "Scene 2")
  142. self.dps[MODE_DPS] = "scene_3"
  143. self.assertEqual(self.light.effect, "Scene 3")
  144. self.dps[MODE_DPS] = "scene_4"
  145. self.assertEqual(self.light.effect, "Scene 4")
  146. def test_light_supported_color_modes(self):
  147. self.assertCountEqual(
  148. self.light.supported_color_modes,
  149. {ColorMode.RGBW, ColorMode.WHITE},
  150. )
  151. def test_light_supported_features(self):
  152. self.assertEqual(self.light.supported_features, LightEntityFeature.EFFECT)
  153. async def test_turn_on(self):
  154. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  155. await self.light.async_turn_on()
  156. async def test_turn_off(self):
  157. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  158. await self.light.async_turn_off()
  159. async def test_set_brightness(self):
  160. async with assert_device_properties_set(
  161. self.light._device,
  162. {
  163. LIGHT_DPS: True,
  164. MODE_DPS: "white",
  165. BRIGHTNESS_DPS: 128,
  166. },
  167. ):
  168. await self.light.async_turn_on(color_mode=ColorMode.WHITE, brightness=128)
  169. async def test_set_rgbw(self):
  170. async with assert_device_properties_set(
  171. self.light._device,
  172. {
  173. LIGHT_DPS: True,
  174. MODE_DPS: "colour",
  175. RGBW_DPS: "ff00000000ffff",
  176. },
  177. ):
  178. await self.light.async_turn_on(
  179. color_mode=ColorMode.RGBW, rgbw_color=(255, 0, 0, 255)
  180. )
  181. def test_extra_state_attributes_set(self):
  182. self.dps[UNKNOWN4_DPS] = 4
  183. self.dps[SCENE_DPS] = "scene"
  184. self.dps[SCENE1_DPS] = "scene1"
  185. self.dps[SCENE2_DPS] = "scene2"
  186. self.dps[SCENE3_DPS] = "scene3"
  187. self.dps[SCENE4_DPS] = "scene4"
  188. self.assertDictEqual(
  189. self.light.extra_state_attributes,
  190. {
  191. "unknown_4": 4,
  192. "scene_data": "scene",
  193. "flash_scene_1": "scene1",
  194. "flash_scene_2": "scene2",
  195. "flash_scene_3": "scene3",
  196. "flash_scene_4": "scene4",
  197. },
  198. )