test_moes_rgb_socket.py 7.0 KB

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