test_grid_connect_double_power_point.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """Tests for the switch entity."""
  2. from homeassistant.components.number import NumberDeviceClass
  3. from homeassistant.components.sensor import SensorDeviceClass
  4. from homeassistant.components.switch import SwitchDeviceClass
  5. from homeassistant.const import (
  6. UnitOfElectricCurrent,
  7. UnitOfElectricPotential,
  8. UnitOfEnergy,
  9. UnitOfPower,
  10. UnitOfTime,
  11. )
  12. from ..const import GRIDCONNECT_2SOCKET_PAYLOAD
  13. from ..mixins.lock import BasicLockTests
  14. from ..mixins.number import MultiNumberTests
  15. from ..mixins.select import BasicSelectTests
  16. from ..mixins.sensor import MultiSensorTests
  17. from ..mixins.switch import MultiSwitchTests
  18. from .base_device_tests import TuyaDeviceTestCase
  19. SWITCH1_DPS = "1"
  20. SWITCH2_DPS = "2"
  21. COUNTDOWN1_DPS = "9"
  22. COUNTDOWN2_DPS = "10"
  23. ENERGY_DPS = "17"
  24. CURRENT_DPS = "18"
  25. POWER_DPS = "19"
  26. VOLTAGE_DPS = "20"
  27. TEST_DPS = "21"
  28. CALIBV_DPS = "22"
  29. CALIBA_DPS = "23"
  30. CALIBW_DPS = "24"
  31. CALIBE_DPS = "25"
  32. INITIAL_DPS = "38"
  33. LOCK_DPS = "40"
  34. MASTER_DPS = "101"
  35. class TestGridConnectDoubleSwitch(
  36. BasicLockTests,
  37. BasicSelectTests,
  38. MultiNumberTests,
  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": "memory",
  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": SwitchDeviceClass.OUTLET,
  68. },
  69. {
  70. "name": "switch_outlet_2",
  71. "dps": SWITCH2_DPS,
  72. "device_class": SwitchDeviceClass.OUTLET,
  73. },
  74. {
  75. "name": "switch_master",
  76. "dps": MASTER_DPS,
  77. "device_class": SwitchDeviceClass.OUTLET,
  78. },
  79. ]
  80. )
  81. self.setUpMultiSensors(
  82. [
  83. {
  84. "name": "sensor_energy",
  85. "dps": ENERGY_DPS,
  86. "unit": UnitOfEnergy.WATT_HOUR,
  87. "state_class": "measurement",
  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. "device_class": NumberDeviceClass.DURATION,
  121. "unit": UnitOfTime.SECONDS,
  122. },
  123. {
  124. "name": "number_timer_2",
  125. "dps": COUNTDOWN2_DPS,
  126. "max": 86400,
  127. "device_class": NumberDeviceClass.DURATION,
  128. "unit": UnitOfTime.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. "time_timer_1",
  144. "time_timer_2",
  145. ],
  146. )
  147. # Since we have attributes, override the default test which expects none.
  148. def test_multi_switch_state_attributes(self):
  149. self.dps[TEST_DPS] = 21
  150. self.assertDictEqual(
  151. self.multiSwitch["switch_master"].extra_state_attributes,
  152. {
  153. "test_bit": 21,
  154. },
  155. )
  156. def test_multi_sensor_extra_state_attributes(self):
  157. self.dps[CALIBA_DPS] = 1
  158. self.dps[CALIBE_DPS] = 2
  159. self.dps[CALIBV_DPS] = 3
  160. self.dps[CALIBW_DPS] = 4
  161. self.assertDictEqual(
  162. self.multiSensor["sensor_current"].extra_state_attributes,
  163. {"calibration": 1},
  164. )
  165. self.assertDictEqual(
  166. self.multiSensor["sensor_energy"].extra_state_attributes,
  167. {"calibration": 2},
  168. )
  169. self.assertDictEqual(
  170. self.multiSensor["sensor_voltage"].extra_state_attributes,
  171. {"calibration": 3},
  172. )
  173. self.assertDictEqual(
  174. self.multiSensor["sensor_power"].extra_state_attributes,
  175. {"calibration": 4},
  176. )