test_smartplugv2_energy.py 4.8 KB

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