test_grid_connect_double_power_point.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. UnitOfElectricCurrent,
  6. UnitOfElectricPotential,
  7. UnitOfTime,
  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. "unit": UnitOfEnergy.WATT_HOUR,
  88. },
  89. {
  90. "name": "sensor_current",
  91. "dps": CURRENT_DPS,
  92. "device_class": SensorDeviceClass.CURRENT,
  93. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  94. "state_class": "measurement",
  95. },
  96. {
  97. "name": "sensor_power",
  98. "dps": POWER_DPS,
  99. "device_class": SensorDeviceClass.POWER,
  100. "unit": UnitOfPower.WATT,
  101. "state_class": "measurement",
  102. "testdata": (1234, 123.4),
  103. },
  104. {
  105. "name": "sensor_voltage",
  106. "dps": VOLTAGE_DPS,
  107. "device_class": SensorDeviceClass.VOLTAGE,
  108. "unit": UnitOfElectricPotential.VOLT,
  109. "state_class": "measurement",
  110. "testdata": (2345, 234.5),
  111. },
  112. ]
  113. )
  114. self.setUpMultiNumber(
  115. [
  116. {
  117. "name": "number_timer_1",
  118. "dps": COUNTDOWN1_DPS,
  119. "max": 86400,
  120. "unit": UnitOfTime.SECONDS,
  121. },
  122. {
  123. "name": "number_timer_2",
  124. "dps": COUNTDOWN2_DPS,
  125. "max": 86400,
  126. "unit": UnitOfTime.SECONDS,
  127. },
  128. ]
  129. )
  130. self.mark_secondary(
  131. [
  132. "lock_child_lock",
  133. "number_timer_1",
  134. "number_timer_2",
  135. "select_initial_state",
  136. "switch_master",
  137. "sensor_energy",
  138. "sensor_current",
  139. "sensor_power",
  140. "sensor_voltage",
  141. ],
  142. )
  143. # Since we have attributes, override the default test which expects none.
  144. def test_multi_switch_state_attributes(self):
  145. self.dps[COUNTDOWN1_DPS] = 9
  146. self.dps[COUNTDOWN2_DPS] = 10
  147. self.dps[VOLTAGE_DPS] = 2350
  148. self.dps[CURRENT_DPS] = 1234
  149. self.dps[POWER_DPS] = 5678
  150. self.dps[TEST_DPS] = 21
  151. self.dps[CALIBV_DPS] = 22
  152. self.dps[CALIBA_DPS] = 23
  153. self.dps[CALIBW_DPS] = 24
  154. self.dps[CALIBE_DPS] = 25
  155. self.assertDictEqual(
  156. self.multiSwitch["switch_master"].extra_state_attributes,
  157. {
  158. "current_a": 1.234,
  159. "voltage_v": 235.0,
  160. "current_power_w": 567.8,
  161. "test_bit": 21,
  162. "voltage_calibration": 22,
  163. "current_calibration": 23,
  164. "power_calibration": 24,
  165. "energy_calibration": 25,
  166. },
  167. )
  168. self.assertDictEqual(
  169. self.multiSwitch["switch_outlet_1"].extra_state_attributes,
  170. {
  171. "countdown": 9,
  172. },
  173. )
  174. self.assertDictEqual(
  175. self.multiSwitch["switch_outlet_2"].extra_state_attributes,
  176. {
  177. "countdown": 10,
  178. },
  179. )