test_grid_connect_double_power_point.py 5.5 KB

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