test_grid_connect_double_power_point.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """Tests for the switch entity."""
  2. from homeassistant.components.switch import DEVICE_CLASS_OUTLET
  3. from homeassistant.const import (
  4. DEVICE_CLASS_CURRENT,
  5. DEVICE_CLASS_ENERGY,
  6. DEVICE_CLASS_POWER,
  7. DEVICE_CLASS_VOLTAGE,
  8. ELECTRIC_CURRENT_MILLIAMPERE,
  9. ELECTRIC_POTENTIAL_VOLT,
  10. ENERGY_WATT_HOUR,
  11. POWER_WATT,
  12. TIME_SECONDS,
  13. )
  14. from ..const import GRIDCONNECT_2SOCKET_PAYLOAD
  15. from ..mixins.lock import BasicLockTests
  16. from ..mixins.number import MultiNumberTests
  17. from ..mixins.select import BasicSelectTests
  18. from ..mixins.sensor import MultiSensorTests
  19. from ..mixins.switch import MultiSwitchTests
  20. from .base_device_tests import TuyaDeviceTestCase
  21. SWITCH1_DPS = "1"
  22. SWITCH2_DPS = "2"
  23. COUNTDOWN1_DPS = "9"
  24. COUNTDOWN2_DPS = "10"
  25. ENERGY_DPS = "17"
  26. CURRENT_DPS = "18"
  27. POWER_DPS = "19"
  28. VOLTAGE_DPS = "20"
  29. TEST_DPS = "21"
  30. CALIBV_DPS = "22"
  31. CALIBA_DPS = "23"
  32. CALIBW_DPS = "24"
  33. CALIBE_DPS = "25"
  34. INITIAL_DPS = "38"
  35. LOCK_DPS = "40"
  36. MASTER_DPS = "101"
  37. class TestGridConnectDoubleSwitch(
  38. BasicLockTests,
  39. BasicSelectTests,
  40. MultiNumberTests,
  41. MultiSensorTests,
  42. MultiSwitchTests,
  43. TuyaDeviceTestCase,
  44. ):
  45. __test__ = True
  46. def setUp(self):
  47. self.setUpForConfig(
  48. "grid_connect_usb_double_power_point.yaml",
  49. GRIDCONNECT_2SOCKET_PAYLOAD,
  50. )
  51. self.setUpBasicLock(LOCK_DPS, self.entities.get("lock_child_lock"))
  52. self.setUpBasicSelect(
  53. INITIAL_DPS,
  54. self.entities.get("select_initial_state"),
  55. {
  56. "on": "On",
  57. "off": "Off",
  58. "memory": "Last State",
  59. },
  60. )
  61. # Master switch must go last, otherwise its tests interfere with
  62. # the tests for the other switches since it overrides them.
  63. # Tests for the specific override behaviour are below.
  64. self.setUpMultiSwitch(
  65. [
  66. {
  67. "name": "switch_outlet_1",
  68. "dps": SWITCH1_DPS,
  69. "device_class": DEVICE_CLASS_OUTLET,
  70. },
  71. {
  72. "name": "switch_outlet_2",
  73. "dps": SWITCH2_DPS,
  74. "device_class": DEVICE_CLASS_OUTLET,
  75. },
  76. {
  77. "name": "switch_master",
  78. "dps": MASTER_DPS,
  79. "device_class": DEVICE_CLASS_OUTLET,
  80. "power_dps": POWER_DPS,
  81. "power_scale": 10,
  82. },
  83. ]
  84. )
  85. self.setUpMultiSensors(
  86. [
  87. {
  88. "name": "sensor_energy",
  89. "dps": ENERGY_DPS,
  90. "device_class": DEVICE_CLASS_ENERGY,
  91. "unit": ENERGY_WATT_HOUR,
  92. "state_class": "total_increasing",
  93. },
  94. {
  95. "name": "sensor_current",
  96. "dps": CURRENT_DPS,
  97. "device_class": DEVICE_CLASS_CURRENT,
  98. "unit": ELECTRIC_CURRENT_MILLIAMPERE,
  99. "state_class": "measurement",
  100. },
  101. {
  102. "name": "sensor_power",
  103. "dps": POWER_DPS,
  104. "device_class": DEVICE_CLASS_POWER,
  105. "unit": POWER_WATT,
  106. "state_class": "measurement",
  107. "testdata": (1234, 123.4),
  108. },
  109. {
  110. "name": "sensor_voltage",
  111. "dps": VOLTAGE_DPS,
  112. "device_class": DEVICE_CLASS_VOLTAGE,
  113. "unit": ELECTRIC_POTENTIAL_VOLT,
  114. "state_class": "measurement",
  115. "testdata": (2345, 234.5),
  116. },
  117. ]
  118. )
  119. self.setUpMultiNumber(
  120. [
  121. {
  122. "name": "number_timer_1",
  123. "dps": COUNTDOWN1_DPS,
  124. "max": 86400,
  125. "unit": TIME_SECONDS,
  126. },
  127. {
  128. "name": "number_timer_2",
  129. "dps": COUNTDOWN2_DPS,
  130. "max": 86400,
  131. "unit": TIME_SECONDS,
  132. },
  133. ]
  134. )
  135. self.mark_secondary(
  136. [
  137. "lock_child_lock",
  138. "number_timer_1",
  139. "number_timer_2",
  140. "select_initial_state",
  141. "switch_master",
  142. "sensor_energy",
  143. "sensor_current",
  144. "sensor_power",
  145. "sensor_voltage",
  146. ],
  147. )
  148. async def test_turn_on_fails_when_master_is_off(self):
  149. self.dps[MASTER_DPS] = False
  150. self.dps[SWITCH1_DPS] = False
  151. self.dps[SWITCH2_DPS] = False
  152. with self.assertRaises(AttributeError):
  153. await self.multiSwitch["switch_outlet_1"].async_turn_on()
  154. with self.assertRaises(AttributeError):
  155. await self.multiSwitch["switch_outlet_2"].async_turn_on()
  156. # Since we have attributes, override the default test which expects none.
  157. def test_multi_switch_state_attributes(self):
  158. self.dps[COUNTDOWN1_DPS] = 9
  159. self.dps[COUNTDOWN2_DPS] = 10
  160. self.dps[VOLTAGE_DPS] = 2350
  161. self.dps[CURRENT_DPS] = 1234
  162. self.dps[POWER_DPS] = 5678
  163. self.dps[TEST_DPS] = 21
  164. self.dps[CALIBV_DPS] = 22
  165. self.dps[CALIBA_DPS] = 23
  166. self.dps[CALIBW_DPS] = 24
  167. self.dps[CALIBE_DPS] = 25
  168. self.assertDictEqual(
  169. self.multiSwitch["switch_master"].extra_state_attributes,
  170. {
  171. "current_a": 1.234,
  172. "voltage_v": 235.0,
  173. "current_power_w": 567.8,
  174. "test_bit": 21,
  175. "voltage_calibration": 22,
  176. "current_calibration": 23,
  177. "power_calibration": 24,
  178. "energy_calibration": 25,
  179. },
  180. )
  181. self.assertDictEqual(
  182. self.multiSwitch["switch_outlet_1"].extra_state_attributes,
  183. {
  184. "countdown": 9,
  185. },
  186. )
  187. self.assertDictEqual(
  188. self.multiSwitch["switch_outlet_2"].extra_state_attributes,
  189. {
  190. "countdown": 10,
  191. },
  192. )