test_grid_connect_double_power_point.py 5.8 KB

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