test_moes_rgb_socket.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. TIME_MINUTES,
  13. UnitOfPower,
  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": UnitOfPower.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.dps[BRIGHTNESS_DPS] = 255
  121. self.assertSequenceEqual(
  122. self.light.rgbw_color,
  123. (255, 255, 0, 255),
  124. )
  125. def test_light_effect_list(self):
  126. self.assertCountEqual(
  127. self.light.effect_list,
  128. [
  129. EFFECT_COLORLOOP,
  130. "Scene 1",
  131. "Scene 2",
  132. "Scene 3",
  133. "Scene 4",
  134. ],
  135. )
  136. def test_light_effect(self):
  137. self.dps[MODE_DPS] = "scene"
  138. self.assertEqual(self.light.effect, EFFECT_COLORLOOP)
  139. self.dps[MODE_DPS] = "scene_1"
  140. self.assertEqual(self.light.effect, "Scene 1")
  141. self.dps[MODE_DPS] = "scene_2"
  142. self.assertEqual(self.light.effect, "Scene 2")
  143. self.dps[MODE_DPS] = "scene_3"
  144. self.assertEqual(self.light.effect, "Scene 3")
  145. self.dps[MODE_DPS] = "scene_4"
  146. self.assertEqual(self.light.effect, "Scene 4")
  147. def test_light_supported_color_modes(self):
  148. self.assertCountEqual(
  149. self.light.supported_color_modes,
  150. {ColorMode.RGBW, ColorMode.WHITE},
  151. )
  152. def test_light_supported_features(self):
  153. self.assertEqual(self.light.supported_features, LightEntityFeature.EFFECT)
  154. async def test_turn_on(self):
  155. self.dps[LIGHT_DPS] = False
  156. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  157. await self.light.async_turn_on()
  158. async def test_turn_off(self):
  159. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  160. await self.light.async_turn_off()
  161. async def test_set_brightness(self):
  162. self.dps[LIGHT_DPS] = True
  163. async with assert_device_properties_set(
  164. self.light._device,
  165. {
  166. MODE_DPS: "white",
  167. BRIGHTNESS_DPS: 128,
  168. },
  169. ):
  170. await self.light.async_turn_on(color_mode=ColorMode.WHITE, brightness=128)
  171. async def test_set_rgbw(self):
  172. self.dps[BRIGHTNESS_DPS] = 255
  173. self.dps[LIGHT_DPS] = True
  174. async with assert_device_properties_set(
  175. self.light._device,
  176. {
  177. MODE_DPS: "colour",
  178. RGBW_DPS: "ff00000000ffff",
  179. },
  180. ):
  181. await self.light.async_turn_on(
  182. color_mode=ColorMode.RGBW, rgbw_color=(255, 0, 0, 255)
  183. )
  184. def test_extra_state_attributes_set(self):
  185. self.dps[UNKNOWN4_DPS] = 4
  186. self.dps[SCENE_DPS] = "scene"
  187. self.dps[SCENE1_DPS] = "scene1"
  188. self.dps[SCENE2_DPS] = "scene2"
  189. self.dps[SCENE3_DPS] = "scene3"
  190. self.dps[SCENE4_DPS] = "scene4"
  191. self.assertDictEqual(
  192. self.light.extra_state_attributes,
  193. {
  194. "unknown_4": 4,
  195. "scene_data": "scene",
  196. "flash_scene_1": "scene1",
  197. "flash_scene_2": "scene2",
  198. "flash_scene_3": "scene3",
  199. "flash_scene_4": "scene4",
  200. },
  201. )