test_moes_rgb_socket.py 7.2 KB

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