test_smartplugv2_energy.py 6.2 KB

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