test_grid_connect_double_power_point.py 5.1 KB

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