test_smartmcb_smt006_energymeter.py 8.8 KB

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