test_smartplugv2_energy.py 4.3 KB

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