test_moes_rgb_socket.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.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. RGB_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 TestMoesRGBSocket(
  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_nightlight")
  46. self.setUpBasicSwitch(
  47. SWITCH_DPS,
  48. self.entities.get("switch_outlet"),
  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=UnitOfTime.MINUTES,
  58. scale=60,
  59. )
  60. self.setUpMultiSensors(
  61. [
  62. {
  63. "name": "sensor_voltage",
  64. "dps": VOLTAGE_DPS,
  65. "unit": UnitOfElectricPotential.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": UnitOfElectricCurrent.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.dps[MODE_DPS] = "white"
  103. self.assertEqual(self.light.brightness, 23)
  104. self.dps[RGB_DPS] = "808000003cff80"
  105. self.dps[MODE_DPS] = "colour"
  106. self.assertEqual(self.light.brightness, 128)
  107. def test_light_color_mode(self):
  108. self.dps[MODE_DPS] = "colour"
  109. self.assertEqual(self.light.color_mode, ColorMode.HS)
  110. self.dps[MODE_DPS] = "white"
  111. self.assertEqual(self.light.color_mode, ColorMode.WHITE)
  112. self.dps[MODE_DPS] = "scene"
  113. self.assertEqual(self.light.color_mode, ColorMode.HS)
  114. self.dps[MODE_DPS] = "scene_1"
  115. self.assertEqual(self.light.color_mode, ColorMode.HS)
  116. self.dps[MODE_DPS] = "scene_2"
  117. self.assertEqual(self.light.color_mode, ColorMode.HS)
  118. self.dps[MODE_DPS] = "scene_3"
  119. self.assertEqual(self.light.color_mode, ColorMode.HS)
  120. self.dps[MODE_DPS] = "scene_4"
  121. self.assertEqual(self.light.color_mode, ColorMode.HS)
  122. def test_light_hs_color(self):
  123. self.dps[RGB_DPS] = "ffff00003cffff"
  124. self.dps[BRIGHTNESS_DPS] = 255
  125. self.assertSequenceEqual(
  126. self.light.hs_color,
  127. (60, 100),
  128. )
  129. def test_light_effect_list(self):
  130. self.assertCountEqual(
  131. self.light.effect_list,
  132. [
  133. "Scene",
  134. "Scene 1",
  135. "Scene 2",
  136. "Scene 3",
  137. "Scene 4",
  138. EFFECT_OFF,
  139. ],
  140. )
  141. def test_light_effect(self):
  142. self.dps[MODE_DPS] = "scene"
  143. self.assertEqual(self.light.effect, "Scene")
  144. self.dps[MODE_DPS] = "scene_1"
  145. self.assertEqual(self.light.effect, "Scene 1")
  146. self.dps[MODE_DPS] = "scene_2"
  147. self.assertEqual(self.light.effect, "Scene 2")
  148. self.dps[MODE_DPS] = "scene_3"
  149. self.assertEqual(self.light.effect, "Scene 3")
  150. self.dps[MODE_DPS] = "scene_4"
  151. self.assertEqual(self.light.effect, "Scene 4")
  152. def test_light_supported_color_modes(self):
  153. self.assertCountEqual(
  154. self.light.supported_color_modes,
  155. {ColorMode.HS, ColorMode.WHITE},
  156. )
  157. def test_light_supported_features(self):
  158. self.assertEqual(self.light.supported_features, LightEntityFeature.EFFECT)
  159. async def test_turn_on(self):
  160. self.dps[LIGHT_DPS] = False
  161. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  162. await self.light.async_turn_on()
  163. async def test_turn_off(self):
  164. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  165. await self.light.async_turn_off()
  166. async def test_set_brightness(self):
  167. self.dps[LIGHT_DPS] = True
  168. self.dps[MODE_DPS] = "white"
  169. async with assert_device_properties_set(
  170. self.light._device,
  171. {
  172. BRIGHTNESS_DPS: 140,
  173. },
  174. ):
  175. await self.light.async_turn_on(brightness=128)
  176. async def test_set_hs_color(self):
  177. self.dps[BRIGHTNESS_DPS] = 255
  178. self.dps[LIGHT_DPS] = True
  179. self.dps[MODE_DPS] = "colour"
  180. async with assert_device_properties_set(
  181. self.light._device,
  182. {
  183. RGB_DPS: "ff00000000ffff",
  184. },
  185. ):
  186. await self.light.async_turn_on(hs_color=(0, 100))
  187. def test_extra_state_attributes_set(self):
  188. self.dps[UNKNOWN4_DPS] = 4
  189. self.dps[SCENE_DPS] = "scene"
  190. self.dps[SCENE1_DPS] = "scene1"
  191. self.dps[SCENE2_DPS] = "scene2"
  192. self.dps[SCENE3_DPS] = "scene3"
  193. self.dps[SCENE4_DPS] = "scene4"
  194. self.assertDictEqual(
  195. self.light.extra_state_attributes,
  196. {
  197. "unknown_4": 4,
  198. "scene_data": "scene",
  199. "flash_scene_1": "scene1",
  200. "flash_scene_2": "scene2",
  201. "flash_scene_3": "scene3",
  202. "flash_scene_4": "scene4",
  203. },
  204. )