test_moes_rgb_socket.py 7.0 KB

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