test_smartmcb_smt006_energymeter.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. "name": "switch_energy_reset",
  59. "dps": RESET_DPS,
  60. },
  61. ],
  62. )
  63. self.setUpMultiSensors(
  64. [
  65. {
  66. "name": "sensor_energy",
  67. "dps": TOTALENERGY_DPS,
  68. "device_class": SensorDeviceClass.ENERGY,
  69. "unit": UnitOfEnergy.KILO_WATT_HOUR,
  70. "state_class": STATE_CLASS_TOTAL_INCREASING,
  71. "testdata": (123456, 1234.56),
  72. },
  73. {
  74. "name": "sensor_balance_energy",
  75. "dps": BALANCE_DPS,
  76. "device_class": SensorDeviceClass.ENERGY,
  77. "unit": UnitOfEnergy.KILO_WATT_HOUR,
  78. "state_class": STATE_CLASS_TOTAL,
  79. "testdata": (123456, 1234.56),
  80. },
  81. ],
  82. )
  83. self.setUpMultiBinarySensors(
  84. [
  85. {
  86. "name": "binary_sensor_short_circuit",
  87. "dps": ERROR_DPS,
  88. "device_class": BinarySensorDeviceClass.PROBLEM,
  89. "testdata": (1, 0),
  90. },
  91. {
  92. "name": "binary_sensor_surge",
  93. "dps": ERROR_DPS,
  94. "device_class": BinarySensorDeviceClass.PROBLEM,
  95. "testdata": (2, 0),
  96. },
  97. {
  98. "name": "binary_sensor_overload",
  99. "dps": ERROR_DPS,
  100. "device_class": BinarySensorDeviceClass.PROBLEM,
  101. "testdata": (4, 0),
  102. },
  103. {
  104. "name": "binary_sensor_leakage_current",
  105. "dps": ERROR_DPS,
  106. "device_class": BinarySensorDeviceClass.SAFETY,
  107. "testdata": (8, 0),
  108. },
  109. {
  110. "name": "binary_sensor_high_temperature",
  111. "dps": ERROR_DPS,
  112. "device_class": BinarySensorDeviceClass.HEAT,
  113. "testdata": (16, 0),
  114. },
  115. {
  116. "name": "binary_sensor_fire",
  117. "dps": ERROR_DPS,
  118. "device_class": BinarySensorDeviceClass.SMOKE,
  119. "testdata": (32, 0),
  120. },
  121. {
  122. "name": "binary_sensor_high_power",
  123. "dps": ERROR_DPS,
  124. "device_class": BinarySensorDeviceClass.POWER,
  125. "testdata": (64, 0),
  126. },
  127. {
  128. "name": "binary_sensor_self_test",
  129. "dps": ERROR_DPS,
  130. "device_class": BinarySensorDeviceClass.PROBLEM,
  131. "testdata": (128, 0),
  132. },
  133. {
  134. "name": "binary_sensor_overcurrent",
  135. "dps": ERROR_DPS,
  136. "device_class": BinarySensorDeviceClass.PROBLEM,
  137. "testdata": (256, 0),
  138. },
  139. {
  140. "name": "binary_sensor_unbalanced",
  141. "dps": ERROR_DPS,
  142. "device_class": BinarySensorDeviceClass.PROBLEM,
  143. "testdata": (512, 0),
  144. },
  145. {
  146. "name": "binary_sensor_overvoltage",
  147. "dps": ERROR_DPS,
  148. "device_class": BinarySensorDeviceClass.PROBLEM,
  149. "testdata": (1024, 0),
  150. },
  151. {
  152. "name": "binary_sensor_undervoltage",
  153. "dps": ERROR_DPS,
  154. "device_class": BinarySensorDeviceClass.PROBLEM,
  155. "testdata": (2048, 0),
  156. },
  157. {
  158. "name": "binary_sensor_phase_fault",
  159. "dps": ERROR_DPS,
  160. "device_class": BinarySensorDeviceClass.PROBLEM,
  161. "testdata": (4096, 0),
  162. },
  163. {
  164. "name": "binary_sensor_outage",
  165. "dps": ERROR_DPS,
  166. "device_class": BinarySensorDeviceClass.POWER,
  167. "testdata": (0, 8192),
  168. },
  169. {
  170. "name": "binary_sensor_magnetism",
  171. "dps": ERROR_DPS,
  172. "device_class": BinarySensorDeviceClass.PROBLEM,
  173. "testdata": (16384, 0),
  174. },
  175. {
  176. "name": "binary_sensor_low_credit",
  177. "dps": ERROR_DPS,
  178. "device_class": BinarySensorDeviceClass.BATTERY,
  179. "testdata": (32768, 0),
  180. },
  181. {
  182. "name": "binary_sensor_credit",
  183. "dps": ERROR_DPS,
  184. "device_class": BinarySensorDeviceClass.PLUG,
  185. "testdata": (0, 65536),
  186. "unit": UnitOfEnergy.KILO_WATT_HOUR,
  187. },
  188. ],
  189. )
  190. self.mark_secondary(
  191. [
  192. "binary_sensor_credit",
  193. "binary_sensor_fire",
  194. "binary_sensor_high_power",
  195. "binary_sensor_high_temperature",
  196. "binary_sensor_leakage_current",
  197. "binary_sensor_low_credit",
  198. "binary_sensor_magnetism",
  199. "binary_sensor_outage",
  200. "binary_sensor_overcurrent",
  201. "binary_sensor_overload",
  202. "binary_sensor_overvoltage",
  203. "binary_sensor_phase_fault",
  204. "binary_sensor_self_test",
  205. "binary_sensor_short_circuit",
  206. "binary_sensor_surge",
  207. "binary_sensor_unbalanced",
  208. "binary_sensor_undervoltage",
  209. "button_energy_reset",
  210. "sensor_balance_energy",
  211. "switch_energy_reset",
  212. "switch_prepay",
  213. ]
  214. )
  215. def test_multi_switch_state_attributes(self):
  216. self.dps[SERIAL_DPS] = "Breaker Number"
  217. self.dps[UNKNOWN101_DPS] = 101
  218. self.dps[UNKNOWN102_DPS] = 102
  219. self.dps[UNKNOWN103_DPS] = 103
  220. self.dps[UNKNOWN104_DPS] = 104
  221. self.dps[UNKNOWN105_DPS] = True
  222. self.dps[UNKNOWN106_DPS] = False
  223. self.assertDictEqual(
  224. self.multiSwitch["switch"].extra_state_attributes,
  225. {
  226. "breaker_number": "Breaker Number",
  227. "unknown_101": 101,
  228. "unknown_102": 102,
  229. "unknown_103": 103,
  230. "unknown_104": 104,
  231. "unknown_105": True,
  232. "unknown_106": False,
  233. },
  234. )
  235. def test_multiple_concurrent_errors(self):
  236. self.dps[ERROR_DPS] = 15
  237. self.assertTrue(self.multiBSensor["binary_sensor_short_circuit"].is_on)
  238. self.assertTrue(self.multiBSensor["binary_sensor_surge"].is_on)
  239. self.assertTrue(self.multiBSensor["binary_sensor_overload"].is_on)
  240. self.assertTrue(self.multiBSensor["binary_sensor_leakage_current"].is_on)
  241. self.assertFalse(self.multiBSensor["binary_sensor_high_temperature"].is_on)
  242. self.assertFalse(self.multiBSensor["binary_sensor_fire"].is_on)