test_smartplugv2_energy.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. LIGHT_DPS = "39"
  33. LOCK_DPS = "40"
  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_outlet",
  52. "dps": SWITCH_DPS,
  53. "device_class": SwitchDeviceClass.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_problem"),
  64. device_class=BinarySensorDeviceClass.PROBLEM,
  65. testdata=(1, 0),
  66. )
  67. self.setUpBasicNumber(
  68. TIMER_DPS,
  69. self.entities.get("number_timer"),
  70. max=1440.0,
  71. unit=UnitOfTime.MINUTES,
  72. scale=60,
  73. )
  74. self.setUpBasicSelect(
  75. INITIAL_DPS,
  76. self.entities.get("select_initial_state"),
  77. {
  78. "on": "on",
  79. "off": "off",
  80. "memory": "memory",
  81. },
  82. )
  83. self.setUpMultiSensors(
  84. [
  85. {
  86. "name": "sensor_energy",
  87. "dps": ENERGY_DPS,
  88. "unit": UnitOfEnergy.WATT_HOUR,
  89. },
  90. {
  91. "name": "sensor_voltage",
  92. "dps": VOLTAGE_DPS,
  93. "unit": UnitOfElectricPotential.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": UnitOfElectricCurrent.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_problem",
  118. "lock_child_lock",
  119. "number_timer",
  120. "select_initial_state",
  121. "select_light",
  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[ERROR_DPS] = 26
  132. self.dps[CYCLE_DPS] = "1A2B"
  133. self.dps[RANDOM_DPS] = "3C4D"
  134. self.assertDictEqual(
  135. self.multiSwitch["switch_outlet"].extra_state_attributes,
  136. {
  137. "test_bit": 21,
  138. "fault_code": 26,
  139. "cycle_timer": "1A2B",
  140. "random_timer": "3C4D",
  141. },
  142. )
  143. def test_multi_sensor_extra_state_attributes(self):
  144. self.dps[CALIBV_DPS] = 22
  145. self.dps[CALIBI_DPS] = 23
  146. self.dps[CALIBP_DPS] = 24
  147. self.dps[CALIBE_DPS] = 25
  148. self.assertDictEqual(
  149. self.multiSensor["sensor_current"].extra_state_attributes,
  150. {"calibration": 23},
  151. )
  152. self.assertDictEqual(
  153. self.multiSensor["sensor_energy"].extra_state_attributes,
  154. {"calibration": 25},
  155. )
  156. self.assertDictEqual(
  157. self.multiSensor["sensor_power"].extra_state_attributes,
  158. {"calibration": 24},
  159. )
  160. self.assertDictEqual(
  161. self.multiSensor["sensor_voltage"].extra_state_attributes,
  162. {"calibration": 22},
  163. )