test_moes_rgb_socket.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. """Tests for the MoesHouse RGB smart socket."""
  2. from homeassistant.components.light import ColorMode, LightEntityFeature
  3. from homeassistant.components.sensor import SensorDeviceClass
  4. from homeassistant.components.switch import SwitchDeviceClass
  5. from homeassistant.const import (
  6. UnitOfElectricCurrent,
  7. UnitOfElectricPotential,
  8. UnitOfPower,
  9. UnitOfTime,
  10. )
  11. from ..const import MOES_RGB_SOCKET_PAYLOAD
  12. from ..helpers import assert_device_properties_set
  13. from ..mixins.number import BasicNumberTests
  14. from ..mixins.sensor import MultiSensorTests
  15. from ..mixins.switch import BasicSwitchTests
  16. from .base_device_tests import TuyaDeviceTestCase
  17. LIGHT_DPS = "1"
  18. MODE_DPS = "2"
  19. BRIGHTNESS_DPS = "3"
  20. UNKNOWN4_DPS = "4"
  21. RGB_DPS = "5"
  22. SCENE_DPS = "6"
  23. SCENE1_DPS = "7"
  24. SCENE2_DPS = "8"
  25. SCENE3_DPS = "9"
  26. SCENE4_DPS = "10"
  27. SWITCH_DPS = "101"
  28. TIMER_DPS = "102"
  29. CURRENT_DPS = "104"
  30. POWER_DPS = "105"
  31. VOLTAGE_DPS = "106"
  32. class TestMoesRGBSocket(
  33. BasicNumberTests,
  34. MultiSensorTests,
  35. BasicSwitchTests,
  36. TuyaDeviceTestCase,
  37. ):
  38. __test__ = True
  39. def setUp(self):
  40. self.setUpForConfig("moes_rgb_socket.yaml", MOES_RGB_SOCKET_PAYLOAD)
  41. self.light = self.entities.get("light_night_light")
  42. self.setUpBasicSwitch(
  43. SWITCH_DPS,
  44. self.entities.get("switch"),
  45. device_class=SwitchDeviceClass.OUTLET,
  46. power_dps=POWER_DPS,
  47. power_scale=10,
  48. )
  49. self.setUpBasicNumber(
  50. TIMER_DPS,
  51. self.entities.get("number_timer"),
  52. max=1440.0,
  53. unit=UnitOfTime.MINUTES,
  54. scale=60,
  55. )
  56. self.setUpMultiSensors(
  57. [
  58. {
  59. "name": "sensor_voltage",
  60. "dps": VOLTAGE_DPS,
  61. "unit": UnitOfElectricPotential.VOLT,
  62. "device_class": SensorDeviceClass.VOLTAGE,
  63. "state_class": "measurement",
  64. "testdata": (2300, 230.0),
  65. },
  66. {
  67. "name": "sensor_current",
  68. "dps": CURRENT_DPS,
  69. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  70. "device_class": SensorDeviceClass.CURRENT,
  71. "state_class": "measurement",
  72. },
  73. {
  74. "name": "sensor_power",
  75. "dps": POWER_DPS,
  76. "unit": UnitOfPower.WATT,
  77. "device_class": SensorDeviceClass.POWER,
  78. "state_class": "measurement",
  79. "testdata": (1234, 123.4),
  80. },
  81. ]
  82. )
  83. self.mark_secondary(
  84. [
  85. "number_timer",
  86. "sensor_current",
  87. "sensor_power",
  88. "sensor_voltage",
  89. ]
  90. )
  91. def test_light_is_on(self):
  92. self.dps[LIGHT_DPS] = True
  93. self.assertTrue(self.light.is_on)
  94. self.dps[LIGHT_DPS] = False
  95. self.assertFalse(self.light.is_on)
  96. def test_light_brightness(self):
  97. self.dps[BRIGHTNESS_DPS] = 45
  98. self.dps[MODE_DPS] = "white"
  99. self.assertEqual(self.light.brightness, 45)
  100. self.dps[RGB_DPS] = "808000003cff80"
  101. self.dps[MODE_DPS] = "colour"
  102. self.assertEqual(self.light.brightness, 128)
  103. def test_light_color_mode(self):
  104. self.dps[MODE_DPS] = "colour"
  105. self.assertEqual(self.light.color_mode, ColorMode.HS)
  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.HS)
  110. self.dps[MODE_DPS] = "scene_1"
  111. self.assertEqual(self.light.color_mode, ColorMode.HS)
  112. self.dps[MODE_DPS] = "scene_2"
  113. self.assertEqual(self.light.color_mode, ColorMode.HS)
  114. self.dps[MODE_DPS] = "scene_3"
  115. self.assertEqual(self.light.color_mode, ColorMode.HS)
  116. self.dps[MODE_DPS] = "scene_4"
  117. self.assertEqual(self.light.color_mode, ColorMode.HS)
  118. def test_light_hs_color(self):
  119. self.dps[RGB_DPS] = "ffff00003cffff"
  120. self.dps[BRIGHTNESS_DPS] = 255
  121. self.assertSequenceEqual(
  122. self.light.hs_color,
  123. (60, 100),
  124. )
  125. def test_light_effect_list(self):
  126. self.assertCountEqual(
  127. self.light.effect_list,
  128. [
  129. "Scene",
  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, "Scene")
  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.HS, 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. self.dps[MODE_DPS] = "white"
  164. async with assert_device_properties_set(
  165. self.light._device,
  166. {
  167. BRIGHTNESS_DPS: 128,
  168. },
  169. ):
  170. await self.light.async_turn_on(brightness=128)
  171. async def test_set_hs_color(self):
  172. self.dps[BRIGHTNESS_DPS] = 255
  173. self.dps[LIGHT_DPS] = True
  174. self.dps[MODE_DPS] = "colour"
  175. async with assert_device_properties_set(
  176. self.light._device,
  177. {
  178. RGB_DPS: "ff00000000ffff",
  179. },
  180. ):
  181. await self.light.async_turn_on(hs_color=(0, 100))
  182. def test_extra_state_attributes_set(self):
  183. self.dps[UNKNOWN4_DPS] = 4
  184. self.dps[SCENE_DPS] = "scene"
  185. self.dps[SCENE1_DPS] = "scene1"
  186. self.dps[SCENE2_DPS] = "scene2"
  187. self.dps[SCENE3_DPS] = "scene3"
  188. self.dps[SCENE4_DPS] = "scene4"
  189. self.assertDictEqual(
  190. self.light.extra_state_attributes,
  191. {
  192. "unknown_4": 4,
  193. "scene_data": "scene",
  194. "flash_scene_1": "scene1",
  195. "flash_scene_2": "scene2",
  196. "flash_scene_3": "scene3",
  197. "flash_scene_4": "scene4",
  198. },
  199. )