test_grid_connect_double_power_point.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. )
  10. from ..const import GRIDCONNECT_2SOCKET_PAYLOAD
  11. from ..mixins.lock import BasicLockTests
  12. from ..mixins.select import BasicSelectTests
  13. from ..mixins.sensor import MultiSensorTests
  14. from ..mixins.switch import MultiSwitchTests
  15. from .base_device_tests import TuyaDeviceTestCase
  16. SWITCH1_DPS = "1"
  17. SWITCH2_DPS = "2"
  18. COUNTDOWN1_DPS = "9"
  19. COUNTDOWN2_DPS = "10"
  20. ENERGY_DPS = "17"
  21. CURRENT_DPS = "18"
  22. POWER_DPS = "19"
  23. VOLTAGE_DPS = "20"
  24. TEST_DPS = "21"
  25. CALIBV_DPS = "22"
  26. CALIBA_DPS = "23"
  27. CALIBW_DPS = "24"
  28. CALIBE_DPS = "25"
  29. INITIAL_DPS = "38"
  30. LOCK_DPS = "40"
  31. MASTER_DPS = "101"
  32. class TestGridConnectDoubleSwitch(
  33. BasicLockTests,
  34. BasicSelectTests,
  35. MultiSensorTests,
  36. MultiSwitchTests,
  37. TuyaDeviceTestCase,
  38. ):
  39. __test__ = True
  40. def setUp(self):
  41. self.setUpForConfig(
  42. "grid_connect_usb_double_power_point.yaml",
  43. GRIDCONNECT_2SOCKET_PAYLOAD,
  44. )
  45. self.setUpBasicLock(LOCK_DPS, self.entities.get("lock_child_lock"))
  46. self.setUpBasicSelect(
  47. INITIAL_DPS,
  48. self.entities.get("select_initial_state"),
  49. {
  50. "on": "on",
  51. "off": "off",
  52. "memory": "memory",
  53. },
  54. )
  55. # Master switch must go last, otherwise its tests interfere with
  56. # the tests for the other switches since it overrides them.
  57. # Tests for the specific override behaviour are below.
  58. self.setUpMultiSwitch(
  59. [
  60. {
  61. "name": "switch_outlet_1",
  62. "dps": SWITCH1_DPS,
  63. "device_class": SwitchDeviceClass.OUTLET,
  64. },
  65. {
  66. "name": "switch_outlet_2",
  67. "dps": SWITCH2_DPS,
  68. "device_class": SwitchDeviceClass.OUTLET,
  69. },
  70. {
  71. "name": "switch_master",
  72. "dps": MASTER_DPS,
  73. "device_class": SwitchDeviceClass.OUTLET,
  74. },
  75. ]
  76. )
  77. self.setUpMultiSensors(
  78. [
  79. {
  80. "name": "sensor_energy",
  81. "dps": ENERGY_DPS,
  82. "unit": UnitOfEnergy.WATT_HOUR,
  83. "state_class": "measurement",
  84. },
  85. {
  86. "name": "sensor_current",
  87. "dps": CURRENT_DPS,
  88. "device_class": SensorDeviceClass.CURRENT,
  89. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  90. "state_class": "measurement",
  91. },
  92. {
  93. "name": "sensor_power",
  94. "dps": POWER_DPS,
  95. "device_class": SensorDeviceClass.POWER,
  96. "unit": UnitOfPower.WATT,
  97. "state_class": "measurement",
  98. "testdata": (1234, 123.4),
  99. },
  100. {
  101. "name": "sensor_voltage",
  102. "dps": VOLTAGE_DPS,
  103. "device_class": SensorDeviceClass.VOLTAGE,
  104. "unit": UnitOfElectricPotential.VOLT,
  105. "state_class": "measurement",
  106. "testdata": (2345, 234.5),
  107. },
  108. ]
  109. )
  110. self.mark_secondary(
  111. [
  112. "lock_child_lock",
  113. "select_initial_state",
  114. "switch_master",
  115. "sensor_energy",
  116. "sensor_current",
  117. "sensor_power",
  118. "sensor_voltage",
  119. "time_timer_1",
  120. "time_timer_2",
  121. ],
  122. )
  123. # Since we have attributes, override the default test which expects none.
  124. def test_multi_switch_state_attributes(self):
  125. self.dps[TEST_DPS] = 21
  126. self.assertDictEqual(
  127. self.multiSwitch["switch_master"].extra_state_attributes,
  128. {
  129. "test_bit": 21,
  130. },
  131. )
  132. def test_multi_sensor_extra_state_attributes(self):
  133. self.dps[CALIBA_DPS] = 1
  134. self.dps[CALIBE_DPS] = 2
  135. self.dps[CALIBV_DPS] = 3
  136. self.dps[CALIBW_DPS] = 4
  137. self.assertDictEqual(
  138. self.multiSensor["sensor_current"].extra_state_attributes,
  139. {"calibration": 1},
  140. )
  141. self.assertDictEqual(
  142. self.multiSensor["sensor_energy"].extra_state_attributes,
  143. {"calibration": 2},
  144. )
  145. self.assertDictEqual(
  146. self.multiSensor["sensor_voltage"].extra_state_attributes,
  147. {"calibration": 3},
  148. )
  149. self.assertDictEqual(
  150. self.multiSensor["sensor_power"].extra_state_attributes,
  151. {"calibration": 4},
  152. )