4
0

test_smartmcb_smt006_energymeter.py 9.0 KB

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