test_smartplugv2_energy.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 ..helpers import assert_device_properties_set
  15. from ..mixins.binary_sensor import BasicBinarySensorTests
  16. from ..mixins.number import BasicNumberTests
  17. from ..mixins.select import BasicSelectTests
  18. from ..mixins.sensor import MultiSensorTests
  19. from ..mixins.switch import MultiSwitchTests
  20. from .base_device_tests import TuyaDeviceTestCase
  21. SWITCH_DPS = "1"
  22. TIMER_DPS = "9"
  23. ENERGY_DPS = "17"
  24. CURRENT_DPS = "18"
  25. POWER_DPS = "19"
  26. VOLTAGE_DPS = "20"
  27. TEST_DPS = "21"
  28. CALIBV_DPS = "22"
  29. CALIBI_DPS = "23"
  30. CALIBP_DPS = "24"
  31. CALIBE_DPS = "25"
  32. ERROR_DPS = "26"
  33. INITIAL_DPS = "38"
  34. LIGHT_DPS = "39"
  35. LOCK_DPS = "40"
  36. CYCLE_DPS = "41"
  37. RANDOM_DPS = "42"
  38. OVERCHARGE_DPS = "46"
  39. ALT_OVERCHARGE_DPS = "51"
  40. class TestSwitchV2Energy(
  41. BasicBinarySensorTests,
  42. BasicNumberTests,
  43. BasicSelectTests,
  44. MultiSensorTests,
  45. MultiSwitchTests,
  46. TuyaDeviceTestCase,
  47. ):
  48. __test__ = True
  49. def setUp(self):
  50. self.setUpForConfig("smartplugv2_energy.yaml", SMARTSWITCH_ENERGY_PAYLOAD)
  51. self.setUpMultiSwitch(
  52. [
  53. {
  54. "name": "switch_outlet",
  55. "dps": SWITCH_DPS,
  56. "device_class": SwitchDeviceClass.OUTLET,
  57. },
  58. {
  59. "name": "switch_overcharge_cutoff",
  60. "dps": OVERCHARGE_DPS,
  61. },
  62. ]
  63. )
  64. self.setUpBasicBinarySensor(
  65. ERROR_DPS,
  66. self.entities.get("binary_sensor_problem"),
  67. device_class=BinarySensorDeviceClass.PROBLEM,
  68. testdata=(1, 0),
  69. )
  70. self.setUpBasicNumber(
  71. TIMER_DPS,
  72. self.entities.get("number_timer"),
  73. max=1440.0,
  74. unit=UnitOfTime.MINUTES,
  75. device_class=NumberDeviceClass.DURATION,
  76. scale=60,
  77. )
  78. self.setUpBasicSelect(
  79. INITIAL_DPS,
  80. self.entities.get("select_initial_state"),
  81. {
  82. "on": "on",
  83. "off": "off",
  84. "memory": "memory",
  85. },
  86. )
  87. self.setUpMultiSensors(
  88. [
  89. {
  90. "name": "sensor_energy",
  91. "dps": ENERGY_DPS,
  92. "unit": UnitOfEnergy.WATT_HOUR,
  93. "state_class": "measurement",
  94. },
  95. {
  96. "name": "sensor_voltage",
  97. "dps": VOLTAGE_DPS,
  98. "unit": UnitOfElectricPotential.VOLT,
  99. "device_class": SensorDeviceClass.VOLTAGE,
  100. "state_class": "measurement",
  101. "testdata": (2300, 230.0),
  102. },
  103. {
  104. "name": "sensor_current",
  105. "dps": CURRENT_DPS,
  106. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  107. "device_class": SensorDeviceClass.CURRENT,
  108. "state_class": "measurement",
  109. },
  110. {
  111. "name": "sensor_power",
  112. "dps": POWER_DPS,
  113. "unit": UnitOfPower.WATT,
  114. "device_class": SensorDeviceClass.POWER,
  115. "state_class": "measurement",
  116. "testdata": (1234, 123.4),
  117. },
  118. ]
  119. )
  120. self.mark_secondary(
  121. [
  122. "binary_sensor_problem",
  123. "lock_child_lock",
  124. "number_timer",
  125. "select_initial_state",
  126. "select_light_mode",
  127. "sensor_current",
  128. "sensor_energy",
  129. "sensor_power",
  130. "sensor_voltage",
  131. "switch_overcharge_cutoff",
  132. "time_timer",
  133. ]
  134. )
  135. def test_multi_switch_state_attributes(self):
  136. self.dps[TEST_DPS] = 21
  137. self.assertDictEqual(
  138. self.multiSwitch["switch_outlet"].extra_state_attributes,
  139. {
  140. "test_bit": 21,
  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. )
  164. def test_basic_bsensor_extra_state_attributes(self):
  165. self.dps[ERROR_DPS] = 2
  166. self.assertDictEqual(
  167. self.basicBSensor.extra_state_attributes,
  168. {"fault_code": 2},
  169. )
  170. async def test_redirected_switch(self):
  171. overcharge_switch = self.multiSwitch["switch_overcharge_cutoff"]
  172. self.dps[OVERCHARGE_DPS] = None
  173. self.dps[ALT_OVERCHARGE_DPS] = False
  174. async with assert_device_properties_set(
  175. overcharge_switch._device,
  176. {ALT_OVERCHARGE_DPS: True},
  177. ):
  178. await overcharge_switch.async_turn_on()
  179. def test_available(self):
  180. self.dps[INITIAL_DPS] = None
  181. self.assertFalse(self.basicSelect.available)
  182. self.dps[INITIAL_DPS] = "on"
  183. self.assertTrue(self.basicSelect.available)
  184. self.dps[OVERCHARGE_DPS] = None
  185. self.dps[ALT_OVERCHARGE_DPS] = None
  186. overcharge_switch = self.multiSwitch["switch_overcharge_cutoff"]
  187. self.assertFalse(overcharge_switch.available)
  188. self.dps[ALT_OVERCHARGE_DPS] = False
  189. self.assertTrue(overcharge_switch.available)
  190. self.dps[ALT_OVERCHARGE_DPS] = None
  191. self.dps[OVERCHARGE_DPS] = True
  192. self.assertTrue(overcharge_switch.available)