test_smartplugv2_energy.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. __test__ = True
  45. def setUp(self):
  46. self.setUpForConfig("smartplugv2_energy.yaml", SMARTSWITCH_ENERGY_PAYLOAD)
  47. self.setUpMultiSwitch(
  48. [
  49. {
  50. "name": "switch_outlet",
  51. "dps": SWITCH_DPS,
  52. "device_class": SwitchDeviceClass.OUTLET,
  53. },
  54. {
  55. "name": "switch_overcharge_cutoff",
  56. "dps": OVERCHARGE_DPS,
  57. },
  58. ]
  59. )
  60. self.setUpBasicBinarySensor(
  61. ERROR_DPS,
  62. self.entities.get("binary_sensor_problem"),
  63. device_class=BinarySensorDeviceClass.PROBLEM,
  64. testdata=(1, 0),
  65. )
  66. self.setUpBasicSelect(
  67. INITIAL_DPS,
  68. self.entities.get("select_initial_state"),
  69. {
  70. "on": "on",
  71. "off": "off",
  72. "memory": "memory",
  73. },
  74. )
  75. self.setUpMultiSensors(
  76. [
  77. {
  78. "name": "sensor_energy",
  79. "dps": ENERGY_DPS,
  80. "unit": UnitOfEnergy.WATT_HOUR,
  81. "state_class": "measurement",
  82. },
  83. {
  84. "name": "sensor_voltage",
  85. "dps": VOLTAGE_DPS,
  86. "unit": UnitOfElectricPotential.VOLT,
  87. "device_class": SensorDeviceClass.VOLTAGE,
  88. "state_class": "measurement",
  89. "testdata": (2300, 230.0),
  90. },
  91. {
  92. "name": "sensor_current",
  93. "dps": CURRENT_DPS,
  94. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  95. "device_class": SensorDeviceClass.CURRENT,
  96. "state_class": "measurement",
  97. },
  98. {
  99. "name": "sensor_power",
  100. "dps": POWER_DPS,
  101. "unit": UnitOfPower.WATT,
  102. "device_class": SensorDeviceClass.POWER,
  103. "state_class": "measurement",
  104. "testdata": (1234, 123.4),
  105. },
  106. ]
  107. )
  108. self.mark_secondary(
  109. [
  110. "binary_sensor_problem",
  111. "lock_child_lock",
  112. "select_initial_state",
  113. "select_light_mode",
  114. "sensor_current",
  115. "sensor_energy",
  116. "sensor_power",
  117. "sensor_voltage",
  118. "switch_overcharge_cutoff",
  119. "time_timer",
  120. ]
  121. )
  122. def test_multi_switch_state_attributes(self):
  123. self.dps[TEST_DPS] = 21
  124. self.assertDictEqual(
  125. self.multiSwitch["switch_outlet"].extra_state_attributes,
  126. {
  127. "test_bit": 21,
  128. },
  129. )
  130. def test_multi_sensor_extra_state_attributes(self):
  131. self.dps[CALIBV_DPS] = 22
  132. self.dps[CALIBI_DPS] = 23
  133. self.dps[CALIBP_DPS] = 24
  134. self.dps[CALIBE_DPS] = 25
  135. self.assertDictEqual(
  136. self.multiSensor["sensor_current"].extra_state_attributes,
  137. {"calibration": 23},
  138. )
  139. self.assertDictEqual(
  140. self.multiSensor["sensor_energy"].extra_state_attributes,
  141. {"calibration": 25},
  142. )
  143. self.assertDictEqual(
  144. self.multiSensor["sensor_power"].extra_state_attributes,
  145. {"calibration": 24},
  146. )
  147. self.assertDictEqual(
  148. self.multiSensor["sensor_voltage"].extra_state_attributes,
  149. {"calibration": 22},
  150. )
  151. def test_basic_bsensor_extra_state_attributes(self):
  152. self.dps[ERROR_DPS] = 2
  153. self.assertDictEqual(
  154. self.basicBSensor.extra_state_attributes,
  155. {"fault_code": 2},
  156. )
  157. async def test_redirected_switch(self):
  158. overcharge_switch = self.multiSwitch["switch_overcharge_cutoff"]
  159. self.dps[OVERCHARGE_DPS] = None
  160. self.dps[ALT_OVERCHARGE_DPS] = False
  161. async with assert_device_properties_set(
  162. overcharge_switch._device,
  163. {ALT_OVERCHARGE_DPS: True},
  164. ):
  165. await overcharge_switch.async_turn_on()
  166. def test_available(self):
  167. self.dps[INITIAL_DPS] = None
  168. self.assertFalse(self.basicSelect.available)
  169. self.dps[INITIAL_DPS] = "on"
  170. self.assertTrue(self.basicSelect.available)
  171. self.dps[OVERCHARGE_DPS] = None
  172. self.dps[ALT_OVERCHARGE_DPS] = None
  173. overcharge_switch = self.multiSwitch["switch_overcharge_cutoff"]
  174. self.assertFalse(overcharge_switch.available)
  175. self.dps[ALT_OVERCHARGE_DPS] = False
  176. self.assertTrue(overcharge_switch.available)
  177. self.dps[ALT_OVERCHARGE_DPS] = None
  178. self.dps[OVERCHARGE_DPS] = True
  179. self.assertTrue(overcharge_switch.available)