test_moes_rgb_socket.py 6.9 KB

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