test_grid_connect_double_power_point.py 6.0 KB

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