4
0

test_smartmcb_smt006_energymeter.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. """Tests for the SmartMCB SMT006 Energy Meter"""
  2. from homeassistant.components.binary_sensor import BinarySensorDeviceClass
  3. from homeassistant.components.button import ButtonDeviceClass
  4. from homeassistant.components.sensor import (
  5. SensorDeviceClass,
  6. STATE_CLASS_TOTAL,
  7. STATE_CLASS_TOTAL_INCREASING,
  8. )
  9. from homeassistant.const import UnitOfEnergy
  10. from ..const import SMARTMCB_SMT006_METER_PAYLOAD
  11. from ..mixins.binary_sensor import MultiBinarySensorTests
  12. from ..mixins.button import BasicButtonTests
  13. from ..mixins.sensor import MultiSensorTests
  14. from ..mixins.switch import MultiSwitchTests
  15. from .base_device_tests import TuyaDeviceTestCase
  16. TOTALENERGY_DPS = "1"
  17. ERROR_DPS = "9"
  18. PREPAY_DPS = "11"
  19. RESET_DPS = "12"
  20. BALANCE_DPS = "13"
  21. SWITCH_DPS = "16"
  22. SERIAL_DPS = "19"
  23. UNKNOWN101_DPS = "101"
  24. UNKNOWN102_DPS = "102"
  25. UNKNOWN103_DPS = "103"
  26. UNKNOWN104_DPS = "104"
  27. UNKNOWN105_DPS = "105"
  28. UNKNOWN106_DPS = "106"
  29. class TestSmartMcbSMT006EnergyMeter(
  30. BasicButtonTests,
  31. MultiBinarySensorTests,
  32. MultiSensorTests,
  33. MultiSwitchTests,
  34. TuyaDeviceTestCase,
  35. ):
  36. __test__ = True
  37. def setUp(self):
  38. self.setUpForConfig(
  39. "smartmcb_smt006_energymeter.yaml",
  40. SMARTMCB_SMT006_METER_PAYLOAD,
  41. )
  42. self.setUpBasicButton(
  43. RESET_DPS,
  44. self.entities.get("button_energy_reset"),
  45. ButtonDeviceClass.RESTART,
  46. )
  47. self.setUpMultiSwitch(
  48. [
  49. {
  50. "name": "switch",
  51. "dps": SWITCH_DPS,
  52. },
  53. {
  54. "name": "switch_prepay",
  55. "dps": PREPAY_DPS,
  56. },
  57. ],
  58. )
  59. self.setUpMultiSensors(
  60. [
  61. {
  62. "name": "sensor_energy",
  63. "dps": TOTALENERGY_DPS,
  64. "device_class": SensorDeviceClass.ENERGY,
  65. "unit": UnitOfEnergy.KILO_WATT_HOUR,
  66. "state_class": STATE_CLASS_TOTAL_INCREASING,
  67. "testdata": (123456, 1234.56),
  68. },
  69. {
  70. "name": "sensor_balance_energy",
  71. "dps": BALANCE_DPS,
  72. "unit": UnitOfEnergy.KILO_WATT_HOUR,
  73. "testdata": (123456, 1234.56),
  74. },
  75. ],
  76. )
  77. self.setUpMultiBinarySensors(
  78. [
  79. {
  80. "name": "binary_sensor_short_circuit",
  81. "dps": ERROR_DPS,
  82. "device_class": BinarySensorDeviceClass.PROBLEM,
  83. "testdata": (1, 0),
  84. },
  85. {
  86. "name": "binary_sensor_surge",
  87. "dps": ERROR_DPS,
  88. "device_class": BinarySensorDeviceClass.PROBLEM,
  89. "testdata": (2, 0),
  90. },
  91. {
  92. "name": "binary_sensor_overload",
  93. "dps": ERROR_DPS,
  94. "device_class": BinarySensorDeviceClass.PROBLEM,
  95. "testdata": (4, 0),
  96. },
  97. {
  98. "name": "binary_sensor_leakage_current",
  99. "dps": ERROR_DPS,
  100. "device_class": BinarySensorDeviceClass.SAFETY,
  101. "testdata": (8, 0),
  102. },
  103. {
  104. "name": "binary_sensor_high_temperature",
  105. "dps": ERROR_DPS,
  106. "device_class": BinarySensorDeviceClass.HEAT,
  107. "testdata": (16, 0),
  108. },
  109. {
  110. "name": "binary_sensor_fire",
  111. "dps": ERROR_DPS,
  112. "device_class": BinarySensorDeviceClass.SMOKE,
  113. "testdata": (32, 0),
  114. },
  115. {
  116. "name": "binary_sensor_high_power",
  117. "dps": ERROR_DPS,
  118. "device_class": BinarySensorDeviceClass.POWER,
  119. "testdata": (64, 0),
  120. },
  121. {
  122. "name": "binary_sensor_self_test",
  123. "dps": ERROR_DPS,
  124. "device_class": BinarySensorDeviceClass.PROBLEM,
  125. "testdata": (128, 0),
  126. },
  127. {
  128. "name": "binary_sensor_overcurrent",
  129. "dps": ERROR_DPS,
  130. "device_class": BinarySensorDeviceClass.PROBLEM,
  131. "testdata": (256, 0),
  132. },
  133. {
  134. "name": "binary_sensor_unbalanced",
  135. "dps": ERROR_DPS,
  136. "device_class": BinarySensorDeviceClass.PROBLEM,
  137. "testdata": (512, 0),
  138. },
  139. {
  140. "name": "binary_sensor_overvoltage",
  141. "dps": ERROR_DPS,
  142. "device_class": BinarySensorDeviceClass.PROBLEM,
  143. "testdata": (1024, 0),
  144. },
  145. {
  146. "name": "binary_sensor_undervoltage",
  147. "dps": ERROR_DPS,
  148. "device_class": BinarySensorDeviceClass.PROBLEM,
  149. "testdata": (2048, 0),
  150. },
  151. {
  152. "name": "binary_sensor_phase_fault",
  153. "dps": ERROR_DPS,
  154. "device_class": BinarySensorDeviceClass.PROBLEM,
  155. "testdata": (4096, 0),
  156. },
  157. {
  158. "name": "binary_sensor_outage",
  159. "dps": ERROR_DPS,
  160. "device_class": BinarySensorDeviceClass.POWER,
  161. "testdata": (0, 8192),
  162. },
  163. {
  164. "name": "binary_sensor_magnetism",
  165. "dps": ERROR_DPS,
  166. "device_class": BinarySensorDeviceClass.PROBLEM,
  167. "testdata": (16384, 0),
  168. },
  169. {
  170. "name": "binary_sensor_low_credit",
  171. "dps": ERROR_DPS,
  172. "device_class": BinarySensorDeviceClass.BATTERY,
  173. "testdata": (32768, 0),
  174. },
  175. {
  176. "name": "binary_sensor_credit",
  177. "dps": ERROR_DPS,
  178. "device_class": BinarySensorDeviceClass.PLUG,
  179. "testdata": (0, 65536),
  180. "unit": UnitOfEnergy.KILO_WATT_HOUR,
  181. },
  182. ],
  183. )
  184. self.mark_secondary(
  185. [
  186. "binary_sensor_credit",
  187. "binary_sensor_fire",
  188. "binary_sensor_high_power",
  189. "binary_sensor_high_temperature",
  190. "binary_sensor_leakage_current",
  191. "binary_sensor_low_credit",
  192. "binary_sensor_magnetism",
  193. "binary_sensor_outage",
  194. "binary_sensor_overcurrent",
  195. "binary_sensor_overload",
  196. "binary_sensor_overvoltage",
  197. "binary_sensor_phase_fault",
  198. "binary_sensor_self_test",
  199. "binary_sensor_short_circuit",
  200. "binary_sensor_surge",
  201. "binary_sensor_unbalanced",
  202. "binary_sensor_undervoltage",
  203. "button_energy_reset",
  204. "sensor_balance_energy",
  205. "sensor_current_a",
  206. "sensor_power_a",
  207. "sensor_voltage_a",
  208. "switch_prepay",
  209. ]
  210. )
  211. def test_multi_switch_state_attributes(self):
  212. self.dps[SERIAL_DPS] = "Breaker Number"
  213. self.dps[UNKNOWN101_DPS] = 101
  214. self.dps[UNKNOWN102_DPS] = 102
  215. self.dps[UNKNOWN103_DPS] = 103
  216. self.dps[UNKNOWN104_DPS] = 104
  217. self.dps[UNKNOWN105_DPS] = True
  218. self.dps[UNKNOWN106_DPS] = False
  219. self.assertDictEqual(
  220. self.multiSwitch["switch"].extra_state_attributes,
  221. {
  222. "breaker_number": "Breaker Number",
  223. "unknown_101": 101,
  224. "unknown_102": 102,
  225. "unknown_103": 103,
  226. "unknown_104": 104,
  227. "unknown_105": True,
  228. "unknown_106": False,
  229. },
  230. )
  231. def test_multiple_concurrent_errors(self):
  232. self.dps[ERROR_DPS] = 15
  233. self.assertTrue(self.multiBSensor["binary_sensor_short_circuit"].is_on)
  234. self.assertTrue(self.multiBSensor["binary_sensor_surge"].is_on)
  235. self.assertTrue(self.multiBSensor["binary_sensor_overload"].is_on)
  236. self.assertTrue(self.multiBSensor["binary_sensor_leakage_current"].is_on)
  237. self.assertFalse(self.multiBSensor["binary_sensor_high_temperature"].is_on)
  238. self.assertFalse(self.multiBSensor["binary_sensor_fire"].is_on)