test_grid_connect_double_power_point.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. UnitOfEnergy,
  8. UnitOfPower,
  9. UnitOfTime,
  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": "memory",
  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. },
  78. ]
  79. )
  80. self.setUpMultiSensors(
  81. [
  82. {
  83. "name": "sensor_energy",
  84. "dps": ENERGY_DPS,
  85. "unit": UnitOfEnergy.WATT_HOUR,
  86. },
  87. {
  88. "name": "sensor_current",
  89. "dps": CURRENT_DPS,
  90. "device_class": SensorDeviceClass.CURRENT,
  91. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  92. "state_class": "measurement",
  93. },
  94. {
  95. "name": "sensor_power",
  96. "dps": POWER_DPS,
  97. "device_class": SensorDeviceClass.POWER,
  98. "unit": UnitOfPower.WATT,
  99. "state_class": "measurement",
  100. "testdata": (1234, 123.4),
  101. },
  102. {
  103. "name": "sensor_voltage",
  104. "dps": VOLTAGE_DPS,
  105. "device_class": SensorDeviceClass.VOLTAGE,
  106. "unit": UnitOfElectricPotential.VOLT,
  107. "state_class": "measurement",
  108. "testdata": (2345, 234.5),
  109. },
  110. ]
  111. )
  112. self.setUpMultiNumber(
  113. [
  114. {
  115. "name": "number_timer_1",
  116. "dps": COUNTDOWN1_DPS,
  117. "max": 86400,
  118. "unit": UnitOfTime.SECONDS,
  119. },
  120. {
  121. "name": "number_timer_2",
  122. "dps": COUNTDOWN2_DPS,
  123. "max": 86400,
  124. "unit": UnitOfTime.SECONDS,
  125. },
  126. ]
  127. )
  128. self.mark_secondary(
  129. [
  130. "lock_child_lock",
  131. "number_timer_1",
  132. "number_timer_2",
  133. "select_initial_state",
  134. "switch_master",
  135. "sensor_energy",
  136. "sensor_current",
  137. "sensor_power",
  138. "sensor_voltage",
  139. ],
  140. )
  141. # Since we have attributes, override the default test which expects none.
  142. def test_multi_switch_state_attributes(self):
  143. self.dps[TEST_DPS] = 21
  144. self.assertDictEqual(
  145. self.multiSwitch["switch_master"].extra_state_attributes,
  146. {
  147. "test_bit": 21,
  148. },
  149. )
  150. def test_multi_sensor_extra_state_attributes(self):
  151. self.dps[CALIBA_DPS] = 1
  152. self.dps[CALIBE_DPS] = 2
  153. self.dps[CALIBV_DPS] = 3
  154. self.dps[CALIBW_DPS] = 4
  155. self.assertDictEqual(
  156. self.multiSensor["sensor_current"].extra_state_attributes,
  157. {"calibration": 1},
  158. )
  159. self.assertDictEqual(
  160. self.multiSensor["sensor_energy"].extra_state_attributes,
  161. {"calibration": 2},
  162. )
  163. self.assertDictEqual(
  164. self.multiSensor["sensor_voltage"].extra_state_attributes,
  165. {"calibration": 3},
  166. )
  167. self.assertDictEqual(
  168. self.multiSensor["sensor_power"].extra_state_attributes,
  169. {"calibration": 4},
  170. )