test_moes_rgb_socket.py 7.4 KB

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