test_moes_rgb_socket.py 7.4 KB

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