test_smartplugv2_energy.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. },
  92. {
  93. "name": "sensor_voltage",
  94. "dps": VOLTAGE_DPS,
  95. "unit": UnitOfElectricPotential.VOLT,
  96. "device_class": SensorDeviceClass.VOLTAGE,
  97. "state_class": "measurement",
  98. "testdata": (2300, 230.0),
  99. },
  100. {
  101. "name": "sensor_current",
  102. "dps": CURRENT_DPS,
  103. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  104. "device_class": SensorDeviceClass.CURRENT,
  105. "state_class": "measurement",
  106. },
  107. {
  108. "name": "sensor_power",
  109. "dps": POWER_DPS,
  110. "unit": UnitOfPower.WATT,
  111. "device_class": SensorDeviceClass.POWER,
  112. "state_class": "measurement",
  113. "testdata": (1234, 123.4),
  114. },
  115. ]
  116. )
  117. self.mark_secondary(
  118. [
  119. "binary_sensor_problem",
  120. "lock_child_lock",
  121. "number_timer",
  122. "select_initial_state",
  123. "select_light",
  124. "sensor_current",
  125. "sensor_energy",
  126. "sensor_power",
  127. "sensor_voltage",
  128. "switch_overcharge_cutoff",
  129. ]
  130. )
  131. def test_multi_switch_state_attributes(self):
  132. self.dps[TEST_DPS] = 21
  133. self.dps[CYCLE_DPS] = "1A2B"
  134. self.dps[RANDOM_DPS] = "3C4D"
  135. self.assertDictEqual(
  136. self.multiSwitch["switch_outlet"].extra_state_attributes,
  137. {
  138. "test_bit": 21,
  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. )
  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. def test_available(self):
  171. self.dps[INITIAL_DPS] = None
  172. self.assertFalse(self.basicSelect.available)
  173. self.dps[INITIAL_DPS] = "on"
  174. self.assertTrue(self.basicSelect.available)