4
0

test_moes_rgb_socket.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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=86400,
  58. unit=UnitOfTime.SECONDS,
  59. device_class=NumberDeviceClass.DURATION,
  60. )
  61. self.setUpMultiSensors(
  62. [
  63. {
  64. "name": "sensor_voltage",
  65. "dps": VOLTAGE_DPS,
  66. "unit": UnitOfElectricPotential.VOLT,
  67. "device_class": SensorDeviceClass.VOLTAGE,
  68. "state_class": "measurement",
  69. "testdata": (2300, 230.0),
  70. },
  71. {
  72. "name": "sensor_current",
  73. "dps": CURRENT_DPS,
  74. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  75. "device_class": SensorDeviceClass.CURRENT,
  76. "state_class": "measurement",
  77. },
  78. {
  79. "name": "sensor_power",
  80. "dps": POWER_DPS,
  81. "unit": UnitOfPower.WATT,
  82. "device_class": SensorDeviceClass.POWER,
  83. "state_class": "measurement",
  84. "testdata": (1234, 123.4),
  85. },
  86. ]
  87. )
  88. self.mark_secondary(
  89. [
  90. "number_timer",
  91. "sensor_current",
  92. "sensor_power",
  93. "sensor_voltage",
  94. ]
  95. )
  96. def test_light_is_on(self):
  97. self.dps[LIGHT_DPS] = True
  98. self.assertTrue(self.light.is_on)
  99. self.dps[LIGHT_DPS] = False
  100. self.assertFalse(self.light.is_on)
  101. def test_light_brightness(self):
  102. self.dps[BRIGHTNESS_DPS] = 45
  103. self.dps[MODE_DPS] = "white"
  104. self.assertEqual(self.light.brightness, 23)
  105. self.dps[RGB_DPS] = "808000003cff80"
  106. self.dps[MODE_DPS] = "colour"
  107. self.assertEqual(self.light.brightness, 128)
  108. def test_light_color_mode(self):
  109. self.dps[MODE_DPS] = "colour"
  110. self.assertEqual(self.light.color_mode, ColorMode.HS)
  111. self.dps[MODE_DPS] = "white"
  112. self.assertEqual(self.light.color_mode, ColorMode.WHITE)
  113. self.dps[MODE_DPS] = "scene"
  114. self.assertEqual(self.light.color_mode, ColorMode.HS)
  115. self.dps[MODE_DPS] = "scene_1"
  116. self.assertEqual(self.light.color_mode, ColorMode.HS)
  117. self.dps[MODE_DPS] = "scene_2"
  118. self.assertEqual(self.light.color_mode, ColorMode.HS)
  119. self.dps[MODE_DPS] = "scene_3"
  120. self.assertEqual(self.light.color_mode, ColorMode.HS)
  121. self.dps[MODE_DPS] = "scene_4"
  122. self.assertEqual(self.light.color_mode, ColorMode.HS)
  123. def test_light_hs_color(self):
  124. self.dps[RGB_DPS] = "ffff00003cffff"
  125. self.dps[BRIGHTNESS_DPS] = 255
  126. self.assertSequenceEqual(
  127. self.light.hs_color,
  128. (60, 100),
  129. )
  130. def test_light_effect_list(self):
  131. self.assertCountEqual(
  132. self.light.effect_list,
  133. [
  134. "Scene",
  135. "Scene 1",
  136. "Scene 2",
  137. "Scene 3",
  138. "Scene 4",
  139. EFFECT_OFF,
  140. ],
  141. )
  142. def test_light_effect(self):
  143. self.dps[MODE_DPS] = "scene"
  144. self.assertEqual(self.light.effect, "Scene")
  145. self.dps[MODE_DPS] = "scene_1"
  146. self.assertEqual(self.light.effect, "Scene 1")
  147. self.dps[MODE_DPS] = "scene_2"
  148. self.assertEqual(self.light.effect, "Scene 2")
  149. self.dps[MODE_DPS] = "scene_3"
  150. self.assertEqual(self.light.effect, "Scene 3")
  151. self.dps[MODE_DPS] = "scene_4"
  152. self.assertEqual(self.light.effect, "Scene 4")
  153. def test_light_supported_color_modes(self):
  154. self.assertCountEqual(
  155. self.light.supported_color_modes,
  156. {ColorMode.HS, ColorMode.WHITE},
  157. )
  158. def test_light_supported_features(self):
  159. self.assertEqual(self.light.supported_features, LightEntityFeature.EFFECT)
  160. async def test_turn_on(self):
  161. self.dps[LIGHT_DPS] = False
  162. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  163. await self.light.async_turn_on()
  164. async def test_turn_off(self):
  165. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  166. await self.light.async_turn_off()
  167. async def test_set_brightness(self):
  168. self.dps[LIGHT_DPS] = True
  169. self.dps[MODE_DPS] = "white"
  170. async with assert_device_properties_set(
  171. self.light._device,
  172. {
  173. BRIGHTNESS_DPS: 140,
  174. },
  175. ):
  176. await self.light.async_turn_on(brightness=128)
  177. async def test_set_hs_color(self):
  178. self.dps[BRIGHTNESS_DPS] = 255
  179. self.dps[LIGHT_DPS] = True
  180. self.dps[MODE_DPS] = "colour"
  181. async with assert_device_properties_set(
  182. self.light._device,
  183. {
  184. RGB_DPS: "ff00000000ffff",
  185. },
  186. ):
  187. await self.light.async_turn_on(hs_color=(0, 100))
  188. def test_extra_state_attributes_set(self):
  189. self.dps[UNKNOWN4_DPS] = 4
  190. self.dps[SCENE_DPS] = "scene"
  191. self.dps[SCENE1_DPS] = "scene1"
  192. self.dps[SCENE2_DPS] = "scene2"
  193. self.dps[SCENE3_DPS] = "scene3"
  194. self.dps[SCENE4_DPS] = "scene4"
  195. self.assertDictEqual(
  196. self.light.extra_state_attributes,
  197. {
  198. "unknown_4": 4,
  199. "scene_data": "scene",
  200. "flash_scene_1": "scene1",
  201. "flash_scene_2": "scene2",
  202. "flash_scene_3": "scene3",
  203. "flash_scene_4": "scene4",
  204. },
  205. )