test_smartplugv2_energy.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.0,
  72. unit=TIME_MINUTES,
  73. scale=60,
  74. )
  75. self.setUpBasicSelect(
  76. INITIAL_DPS,
  77. self.entities.get("select_initial_state"),
  78. {
  79. "on": "On",
  80. "off": "Off",
  81. "memory": "Last State",
  82. },
  83. )
  84. self.setUpMultiSensors(
  85. [
  86. {
  87. "name": "sensor_energy",
  88. "dps": ENERGY_DPS,
  89. "unit": ENERGY_WATT_HOUR,
  90. "device_class": DEVICE_CLASS_ENERGY,
  91. "state_class": "total_increasing",
  92. },
  93. {
  94. "name": "sensor_voltage",
  95. "dps": VOLTAGE_DPS,
  96. "unit": ELECTRIC_POTENTIAL_VOLT,
  97. "device_class": DEVICE_CLASS_VOLTAGE,
  98. "state_class": "measurement",
  99. "testdata": (2300, 230.0),
  100. },
  101. {
  102. "name": "sensor_current",
  103. "dps": CURRENT_DPS,
  104. "unit": ELECTRIC_CURRENT_MILLIAMPERE,
  105. "device_class": DEVICE_CLASS_CURRENT,
  106. "state_class": "measurement",
  107. },
  108. {
  109. "name": "sensor_power",
  110. "dps": POWER_DPS,
  111. "unit": POWER_WATT,
  112. "device_class": DEVICE_CLASS_POWER,
  113. "state_class": "measurement",
  114. "testdata": (1234, 123.4),
  115. },
  116. ]
  117. )
  118. self.mark_secondary(
  119. [
  120. "binary_sensor_error",
  121. "number_timer",
  122. "select_initial_state",
  123. "sensor_current",
  124. "sensor_energy",
  125. "sensor_power",
  126. "sensor_voltage",
  127. "switch_overcharge_cutoff",
  128. ]
  129. )
  130. def test_multi_switch_state_attributes(self):
  131. self.dps[TEST_DPS] = 21
  132. self.dps[CALIBV_DPS] = 22
  133. self.dps[CALIBI_DPS] = 23
  134. self.dps[CALIBP_DPS] = 24
  135. self.dps[CALIBE_DPS] = 25
  136. self.dps[ERROR_DPS] = 26
  137. self.dps[CYCLE_DPS] = "1A2B"
  138. self.dps[RANDOM_DPS] = "3C4D"
  139. self.assertDictEqual(
  140. self.multiSwitch["switch"].extra_state_attributes,
  141. {
  142. "test_bit": 21,
  143. "voltage_calibration": 22,
  144. "current_calibration": 23,
  145. "power_calibration": 24,
  146. "energy_calibration": 25,
  147. "fault_code": 26,
  148. "cycle_timer": "1A2B",
  149. "random_timer": "3C4D",
  150. },
  151. )