test_smartplugv2_energy.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. UnitOfElectricCurrent,
  7. UnitOfElectricPotential,
  8. UnitOfEnergy,
  9. UnitOfPower,
  10. UnitOfTime,
  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_outlet",
  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=UnitOfTime.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. },
  88. {
  89. "name": "sensor_voltage",
  90. "dps": VOLTAGE_DPS,
  91. "unit": UnitOfElectricPotential.VOLT,
  92. "device_class": SensorDeviceClass.VOLTAGE,
  93. "state_class": "measurement",
  94. "testdata": (2300, 230.0),
  95. },
  96. {
  97. "name": "sensor_current",
  98. "dps": CURRENT_DPS,
  99. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  100. "device_class": SensorDeviceClass.CURRENT,
  101. "state_class": "measurement",
  102. },
  103. {
  104. "name": "sensor_power",
  105. "dps": POWER_DPS,
  106. "unit": UnitOfPower.WATT,
  107. "device_class": SensorDeviceClass.POWER,
  108. "state_class": "measurement",
  109. "testdata": (1234, 123.4),
  110. },
  111. ]
  112. )
  113. self.mark_secondary(
  114. [
  115. "binary_sensor_error",
  116. "number_timer",
  117. "select_initial_state",
  118. "sensor_current",
  119. "sensor_energy",
  120. "sensor_power",
  121. "sensor_voltage",
  122. "switch_overcharge_cutoff",
  123. ]
  124. )
  125. def test_multi_switch_state_attributes(self):
  126. self.dps[TEST_DPS] = 21
  127. self.dps[ERROR_DPS] = 26
  128. self.dps[CYCLE_DPS] = "1A2B"
  129. self.dps[RANDOM_DPS] = "3C4D"
  130. self.assertDictEqual(
  131. self.multiSwitch["switch_outlet"].extra_state_attributes,
  132. {
  133. "test_bit": 21,
  134. "fault_code": 26,
  135. "cycle_timer": "1A2B",
  136. "random_timer": "3C4D",
  137. },
  138. )
  139. def test_multi_sensor_extra_state_attributes(self):
  140. self.dps[CALIBV_DPS] = 22
  141. self.dps[CALIBI_DPS] = 23
  142. self.dps[CALIBP_DPS] = 24
  143. self.dps[CALIBE_DPS] = 25
  144. self.assertDictEqual(
  145. self.multiSensor["sensor_current"].extra_state_attributes,
  146. {"calibration": 23},
  147. )
  148. self.assertDictEqual(
  149. self.multiSensor["sensor_energy"].extra_state_attributes,
  150. {"calibration": 25},
  151. )
  152. self.assertDictEqual(
  153. self.multiSensor["sensor_power"].extra_state_attributes,
  154. {"calibration": 24},
  155. )
  156. self.assertDictEqual(
  157. self.multiSensor["sensor_voltage"].extra_state_attributes,
  158. {"calibration": 22},
  159. )