test_smartplugv2_energy.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """Tests for the switch entity."""
  2. from homeassistant.components.binary_sensor import BinarySensorDeviceClass
  3. from homeassistant.components.number import NumberDeviceClass
  4. from homeassistant.components.sensor import SensorDeviceClass
  5. from homeassistant.components.switch import SwitchDeviceClass
  6. from homeassistant.const import (
  7. UnitOfElectricCurrent,
  8. UnitOfElectricPotential,
  9. UnitOfEnergy,
  10. UnitOfPower,
  11. UnitOfTime,
  12. )
  13. from ..const import SMARTSWITCH_ENERGY_PAYLOAD
  14. from ..mixins.binary_sensor import BasicBinarySensorTests
  15. from ..mixins.number import BasicNumberTests
  16. from ..mixins.select import BasicSelectTests
  17. from ..mixins.sensor import MultiSensorTests
  18. from ..mixins.switch import MultiSwitchTests
  19. from .base_device_tests import TuyaDeviceTestCase
  20. SWITCH_DPS = "1"
  21. TIMER_DPS = "9"
  22. ENERGY_DPS = "17"
  23. CURRENT_DPS = "18"
  24. POWER_DPS = "19"
  25. VOLTAGE_DPS = "20"
  26. TEST_DPS = "21"
  27. CALIBV_DPS = "22"
  28. CALIBI_DPS = "23"
  29. CALIBP_DPS = "24"
  30. CALIBE_DPS = "25"
  31. ERROR_DPS = "26"
  32. INITIAL_DPS = "38"
  33. LIGHT_DPS = "39"
  34. LOCK_DPS = "40"
  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_outlet",
  53. "dps": SWITCH_DPS,
  54. "device_class": SwitchDeviceClass.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_problem"),
  65. device_class=BinarySensorDeviceClass.PROBLEM,
  66. testdata=(1, 0),
  67. )
  68. self.setUpBasicNumber(
  69. TIMER_DPS,
  70. self.entities.get("number_timer"),
  71. max=1440.0,
  72. unit=UnitOfTime.MINUTES,
  73. device_class=NumberDeviceClass.DURATION,
  74. scale=60,
  75. )
  76. self.setUpBasicSelect(
  77. INITIAL_DPS,
  78. self.entities.get("select_initial_state"),
  79. {
  80. "on": "on",
  81. "off": "off",
  82. "memory": "memory",
  83. },
  84. )
  85. self.setUpMultiSensors(
  86. [
  87. {
  88. "name": "sensor_energy",
  89. "dps": ENERGY_DPS,
  90. "unit": UnitOfEnergy.WATT_HOUR,
  91. "state_class": "measurement",
  92. },
  93. {
  94. "name": "sensor_voltage",
  95. "dps": VOLTAGE_DPS,
  96. "unit": UnitOfElectricPotential.VOLT,
  97. "device_class": SensorDeviceClass.VOLTAGE,
  98. "state_class": "measurement",
  99. "testdata": (2300, 230.0),
  100. },
  101. {
  102. "name": "sensor_current",
  103. "dps": CURRENT_DPS,
  104. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  105. "device_class": SensorDeviceClass.CURRENT,
  106. "state_class": "measurement",
  107. },
  108. {
  109. "name": "sensor_power",
  110. "dps": POWER_DPS,
  111. "unit": UnitOfPower.WATT,
  112. "device_class": SensorDeviceClass.POWER,
  113. "state_class": "measurement",
  114. "testdata": (1234, 123.4),
  115. },
  116. ]
  117. )
  118. self.mark_secondary(
  119. [
  120. "binary_sensor_problem",
  121. "lock_child_lock",
  122. "number_timer",
  123. "select_initial_state",
  124. "select_light_mode",
  125. "sensor_current",
  126. "sensor_energy",
  127. "sensor_power",
  128. "sensor_voltage",
  129. "switch_overcharge_cutoff",
  130. ]
  131. )
  132. def test_multi_switch_state_attributes(self):
  133. self.dps[TEST_DPS] = 21
  134. self.dps[CYCLE_DPS] = "1A2B"
  135. self.dps[RANDOM_DPS] = "3C4D"
  136. self.assertDictEqual(
  137. self.multiSwitch["switch_outlet"].extra_state_attributes,
  138. {
  139. "test_bit": 21,
  140. "cycle_timer": "1A2B",
  141. "random_timer": "3C4D",
  142. },
  143. )
  144. def test_multi_sensor_extra_state_attributes(self):
  145. self.dps[CALIBV_DPS] = 22
  146. self.dps[CALIBI_DPS] = 23
  147. self.dps[CALIBP_DPS] = 24
  148. self.dps[CALIBE_DPS] = 25
  149. self.assertDictEqual(
  150. self.multiSensor["sensor_current"].extra_state_attributes,
  151. {"calibration": 23},
  152. )
  153. self.assertDictEqual(
  154. self.multiSensor["sensor_energy"].extra_state_attributes,
  155. {"calibration": 25},
  156. )
  157. self.assertDictEqual(
  158. self.multiSensor["sensor_power"].extra_state_attributes,
  159. {"calibration": 24},
  160. )
  161. self.assertDictEqual(
  162. self.multiSensor["sensor_voltage"].extra_state_attributes,
  163. {"calibration": 22},
  164. )
  165. def test_basic_bsensor_extra_state_attributes(self):
  166. self.dps[ERROR_DPS] = 2
  167. self.assertDictEqual(
  168. self.basicBSensor.extra_state_attributes,
  169. {"fault_code": 2},
  170. )
  171. def test_available(self):
  172. self.dps[INITIAL_DPS] = None
  173. self.assertFalse(self.basicSelect.available)
  174. self.dps[INITIAL_DPS] = "on"
  175. self.assertTrue(self.basicSelect.available)