test_smartplugv2_energy.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """Tests for the switch entity."""
  2. from homeassistant.components.binary_sensor import DEVICE_CLASS_PROBLEM
  3. from homeassistant.components.switch import DEVICE_CLASS_OUTLET
  4. from homeassistant.const import (
  5. DEVICE_CLASS_CURRENT,
  6. DEVICE_CLASS_ENERGY,
  7. DEVICE_CLASS_POWER,
  8. DEVICE_CLASS_VOLTAGE,
  9. ELECTRIC_CURRENT_MILLIAMPERE,
  10. ELECTRIC_POTENTIAL_VOLT,
  11. ENERGY_WATT_HOUR,
  12. POWER_WATT,
  13. TIME_MINUTES,
  14. )
  15. from ..const import SMARTSWITCH_ENERGY_PAYLOAD
  16. from ..mixins.binary_sensor import BasicBinarySensorTests
  17. from ..mixins.number import BasicNumberTests
  18. from ..mixins.select import BasicSelectTests
  19. from ..mixins.sensor import MultiSensorTests
  20. from ..mixins.switch import MultiSwitchTests
  21. from .base_device_tests import TuyaDeviceTestCase
  22. SWITCH_DPS = "1"
  23. TIMER_DPS = "9"
  24. ENERGY_DPS = "17"
  25. CURRENT_DPS = "18"
  26. POWER_DPS = "19"
  27. VOLTAGE_DPS = "20"
  28. TEST_DPS = "21"
  29. CALIBV_DPS = "22"
  30. CALIBI_DPS = "23"
  31. CALIBP_DPS = "24"
  32. CALIBE_DPS = "25"
  33. ERROR_DPS = "26"
  34. INITIAL_DPS = "38"
  35. CYCLE_DPS = "41"
  36. RANDOM_DPS = "42"
  37. OVERCHARGE_DPS = "46"
  38. class TestSwitchV2Energy(
  39. BasicBinarySensorTests,
  40. BasicNumberTests,
  41. BasicSelectTests,
  42. MultiSensorTests,
  43. MultiSwitchTests,
  44. TuyaDeviceTestCase,
  45. ):
  46. __test__ = True
  47. def setUp(self):
  48. self.setUpForConfig("smartplugv2_energy.yaml", SMARTSWITCH_ENERGY_PAYLOAD)
  49. self.setUpMultiSwitch(
  50. [
  51. {
  52. "name": "switch",
  53. "dps": SWITCH_DPS,
  54. "device_class": DEVICE_CLASS_OUTLET,
  55. },
  56. {
  57. "name": "switch_overcharge_cutoff",
  58. "dps": OVERCHARGE_DPS,
  59. },
  60. ]
  61. )
  62. self.setUpBasicBinarySensor(
  63. ERROR_DPS,
  64. self.entities.get("binary_sensor_error"),
  65. device_class=DEVICE_CLASS_PROBLEM,
  66. testdata=(1, 0),
  67. )
  68. self.setUpBasicNumber(
  69. TIMER_DPS,
  70. self.entities.get("number_timer"),
  71. max=1440,
  72. unit=TIME_MINUTES,
  73. )
  74. self.setUpBasicSelect(
  75. INITIAL_DPS,
  76. self.entities.get("select_initial_state"),
  77. {
  78. "on": "On",
  79. "off": "Off",
  80. "memory": "Last State",
  81. },
  82. )
  83. self.setUpMultiSensors(
  84. [
  85. {
  86. "name": "sensor_energy",
  87. "dps": ENERGY_DPS,
  88. "unit": ENERGY_WATT_HOUR,
  89. "device_class": DEVICE_CLASS_ENERGY,
  90. "state_class": "total_increasing",
  91. },
  92. {
  93. "name": "sensor_voltage",
  94. "dps": VOLTAGE_DPS,
  95. "unit": ELECTRIC_POTENTIAL_VOLT,
  96. "device_class": DEVICE_CLASS_VOLTAGE,
  97. "state_class": "measurement",
  98. "testdata": (2300, 230.0),
  99. },
  100. {
  101. "name": "sensor_current",
  102. "dps": CURRENT_DPS,
  103. "unit": ELECTRIC_CURRENT_MILLIAMPERE,
  104. "device_class": DEVICE_CLASS_CURRENT,
  105. "state_class": "measurement",
  106. },
  107. {
  108. "name": "sensor_power",
  109. "dps": POWER_DPS,
  110. "unit": POWER_WATT,
  111. "device_class": DEVICE_CLASS_POWER,
  112. "state_class": "measurement",
  113. "testdata": (1234, 123.4),
  114. },
  115. ]
  116. )
  117. self.mark_secondary(
  118. [
  119. "binary_sensor_error",
  120. "number_timer",
  121. "select_initial_state",
  122. "sensor_current",
  123. "sensor_energy",
  124. "sensor_power",
  125. "sensor_voltage",
  126. "switch_overcharge_cutoff",
  127. ]
  128. )
  129. def test_multi_switch_state_attributes(self):
  130. self.dps[TEST_DPS] = 21
  131. self.dps[CALIBV_DPS] = 22
  132. self.dps[CALIBI_DPS] = 23
  133. self.dps[CALIBP_DPS] = 24
  134. self.dps[CALIBE_DPS] = 25
  135. self.dps[ERROR_DPS] = 26
  136. self.dps[CYCLE_DPS] = "1A2B"
  137. self.dps[RANDOM_DPS] = "3C4D"
  138. self.assertDictEqual(
  139. self.multiSwitch["switch"].extra_state_attributes,
  140. {
  141. "test_bit": 21,
  142. "voltage_calibration": 22,
  143. "current_calibration": 23,
  144. "power_calibration": 24,
  145. "energy_calibration": 25,
  146. "fault_code": 26,
  147. "cycle_timer": "1A2B",
  148. "random_timer": "3C4D",
  149. },
  150. )