Smarthome Functionen
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test.py 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import colored
  2. import inspect
  3. from __simulation__ import devices
  4. import time
  5. DT_TOGGLE = 0.3
  6. TEST_FULL = 'full'
  7. TEST_SMOKE = 'smoke'
  8. #
  9. COLOR_SUCCESS = colored.fg("light_green")
  10. COLOR_FAIL = colored.fg("light_red")
  11. class test_smarthome(object):
  12. def __init__(self, rooms):
  13. # add testcases by room objects
  14. for name in rooms.getmembers():
  15. obj = rooms.getobjbyname(name)
  16. if obj.__class__.__name__ == "videv_light":
  17. common_name = '.'.join(name.split('.')[:-1]) + '.' + name.split('.')[-1][6:]
  18. li_device = rooms.getobjbyname(common_name + '_zigbee') if obj.enable_brightness or obj.enable_color_temp else None
  19. try:
  20. sw_device = rooms.getobjbyname(common_name) if obj.enable_state else None
  21. except AttributeError:
  22. # must be a device without switching device
  23. sw_device = li_device
  24. setattr(self, common_name.replace('.', '_'), testcase_light(obj, sw_device, li_device))
  25. # add test collection
  26. self.all = test_collection(self)
  27. def getmembers(self, prefix=''):
  28. rv = []
  29. for name, obj in inspect.getmembers(self):
  30. if prefix:
  31. full_name = prefix + '.' + name
  32. else:
  33. full_name = name
  34. if not name.startswith('_'):
  35. try:
  36. if obj.__class__.__bases__[0].__name__ == "testcase" or obj.__class__.__name__ == "test_collection":
  37. rv.append(full_name)
  38. else:
  39. rv.extend(obj.getmembers(full_name))
  40. except AttributeError:
  41. pass
  42. return rv
  43. def getobjbyname(self, name):
  44. if name.startswith("test."):
  45. name = name[5:]
  46. obj = self
  47. for subname in name.split('.'):
  48. obj = getattr(obj, subname)
  49. return obj
  50. def command(self, full_command):
  51. try:
  52. parameter = " " + full_command.split(' ')[1]
  53. except IndexError:
  54. parameter = ""
  55. command = full_command.split(' ')[0].split('.')[-1] + parameter
  56. device_name = '.'.join(full_command.split(' ')[0].split('.')[:-1])
  57. self.getobjbyname(device_name).command(command)
  58. class test_result_base(object):
  59. def __init__(self):
  60. self.__init_test_counters__()
  61. def __init_test_counters__(self):
  62. self.test_counter = 0
  63. self.success_tests = 0
  64. self.failed_tests = 0
  65. def statistic(self):
  66. return (self.test_counter, self.success_tests, self.failed_tests)
  67. def print_statistic(self):
  68. color = COLOR_SUCCESS if self.test_counter == self.success_tests else COLOR_FAIL
  69. print(color + "*** SUCCESS: (%4d/%4d) FAIL: (%4d/%4d) ***\n" % (self.success_tests,
  70. self.test_counter, self.failed_tests, self.test_counter) + colored.attr("reset"))
  71. class test_collection(test_result_base):
  72. def __init__(self, test_instance):
  73. super().__init__()
  74. self.test_instance = test_instance
  75. def capabilities(self):
  76. return [TEST_FULL, TEST_SMOKE]
  77. def command(self, command):
  78. self.__init_test_counters__()
  79. for member in self.test_instance.getmembers():
  80. obj = self.test_instance.getobjbyname(member)
  81. if id(obj) != id(self):
  82. obj.test_all(command)
  83. num, suc, fail = obj.statistic()
  84. self.test_counter += num
  85. self.success_tests += suc
  86. self.failed_tests += fail
  87. self.print_statistic()
  88. class testcase(test_result_base):
  89. def __init__(self):
  90. super().__init__()
  91. self.__test_list__ = []
  92. def capabilities(self):
  93. if len(self.__test_list__) > 0 and not 'test_all' in self.__test_list__:
  94. self.__test_list__.append('test_all')
  95. self.__test_list__.sort()
  96. return self.__test_list__
  97. def test_all(self, test=TEST_FULL):
  98. test_counter = 0
  99. success_tests = 0
  100. failed_tests = 0
  101. for tc_name in self.capabilities():
  102. if tc_name != "test_all":
  103. self.command(tc_name, test)
  104. test_counter += self.test_counter
  105. success_tests += self.success_tests
  106. failed_tests += self.failed_tests
  107. self.test_counter = test_counter
  108. self.success_tests = success_tests
  109. self.failed_tests = failed_tests
  110. def command(self, command, test=TEST_FULL):
  111. self.__init_test_counters__()
  112. tc = getattr(self, command)
  113. self.__init_test_counters__()
  114. rv = tc(test)
  115. self.print_statistic()
  116. def heading(self, desciption):
  117. print(desciption)
  118. def sub_heading(self, desciption):
  119. print(2 * " " + desciption)
  120. def result(self, desciption, success):
  121. self.test_counter += 1
  122. if success:
  123. self.success_tests += 1
  124. else:
  125. self.failed_tests += 1
  126. print(4 * " " + ("SUCCESS - " if success else "FAIL - ") + desciption)
  127. class testcase_light(testcase):
  128. def __init__(self, videv, sw_device, li_device):
  129. self.videv = videv
  130. self.sw_device = sw_device
  131. self.li_device = li_device
  132. self.__test_list__ = []
  133. if self.videv.enable_state:
  134. self.__test_list__.append('test_power_on_off')
  135. if self.videv.enable_brightness:
  136. self.__test_list__.append('test_brightness')
  137. if self.videv.enable_color_temp:
  138. self.__test_list__.append('test_color_temp')
  139. def test_power_on_off(self, test=TEST_FULL):
  140. self.heading("Power On/ Off test (%s)" % self.videv.topic)
  141. #
  142. sw_state = self.sw_device.get_data(self.sw_device.KEY_OUTPUT_0)
  143. #
  144. for i in range(0, 2):
  145. self.sub_heading("State change of switching device")
  146. #
  147. self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: not self.sw_device.data.get(self.sw_device.KEY_OUTPUT_0)})
  148. time.sleep(DT_TOGGLE)
  149. self.result("Virtual device state after Switch on by switching device", sw_state != self.videv.get_data(self.videv.KEY_STATE))
  150. self.result("Switching device state after Switch on by switching device",
  151. sw_state != self.sw_device.get_data(self.sw_device.KEY_OUTPUT_0))
  152. self.sub_heading("State change of virtual device")
  153. #
  154. self.videv.send(self.videv.KEY_STATE, not self.videv.data.get(self.videv.KEY_STATE))
  155. time.sleep(DT_TOGGLE)
  156. self.result("Virtual device state after Switch off by virtual device", sw_state == self.videv.get_data(self.videv.KEY_STATE))
  157. self.result("Switching device state after Switch on by switching device",
  158. sw_state == self.sw_device.get_data(self.sw_device.KEY_OUTPUT_0))
  159. def test_brightness(self, test=TEST_FULL):
  160. self.heading("Brightness test (%s)" % self.videv.topic)
  161. #
  162. br_state = self.li_device.get_data(self.li_device.KEY_BRIGHTNESS)
  163. delta = -15 if br_state > 50 else 15
  164. self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: True})
  165. time.sleep(DT_TOGGLE)
  166. for i in range(0, 2):
  167. self.sub_heading("Brightness change by light device")
  168. #
  169. self.li_device.store_data(**{self.li_device.KEY_BRIGHTNESS: br_state + delta})
  170. time.sleep(DT_TOGGLE)
  171. self.result("Virtual device state after setting brightness by light device",
  172. br_state + delta == self.videv.get_data(self.videv.KEY_BRIGHTNESS))
  173. self.result("Light device state after setting brightness by light device", br_state +
  174. delta == self.li_device.get_data(self.li_device.KEY_BRIGHTNESS))
  175. self.sub_heading("Brightness change by virtual device")
  176. #
  177. self.videv.send(self.videv.KEY_BRIGHTNESS, br_state)
  178. time.sleep(DT_TOGGLE)
  179. self.result("Virtual device state after setting brightness by light device", br_state == self.videv.get_data(self.videv.KEY_BRIGHTNESS))
  180. self.result("Light device state after setting brightness by light device",
  181. br_state == self.li_device.get_data(self.li_device.KEY_BRIGHTNESS))
  182. self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: False})
  183. time.sleep(DT_TOGGLE)
  184. def test_color_temp(self, test=TEST_FULL):
  185. self.heading("Color temperature test (%s)" % self.videv.topic)
  186. #
  187. ct_state = self.li_device.get_data(self.li_device.KEY_COLOR_TEMP)
  188. delta = -3 if ct_state > 5 else 3
  189. self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: True})
  190. time.sleep(DT_TOGGLE)
  191. for i in range(0, 2):
  192. self.sub_heading("Color temperature change by light device")
  193. #
  194. self.li_device.store_data(**{self.li_device.KEY_COLOR_TEMP: ct_state + delta})
  195. time.sleep(DT_TOGGLE)
  196. self.result("Virtual device state after setting color temperature by light device",
  197. ct_state + delta == self.videv.get_data(self.videv.KEY_COLOR_TEMP))
  198. self.result("Light device state after setting color temperature by light device", ct_state +
  199. delta == self.li_device.get_data(self.li_device.KEY_COLOR_TEMP))
  200. self.sub_heading("Color temperature change by virtual device")
  201. #
  202. self.videv.send(self.videv.KEY_COLOR_TEMP, ct_state)
  203. time.sleep(DT_TOGGLE)
  204. self.result("Virtual device state after setting color temperature by light device",
  205. ct_state == self.videv.get_data(self.videv.KEY_COLOR_TEMP))
  206. self.result("Light device state after setting color temperature by light device",
  207. ct_state == self.li_device.get_data(self.li_device.KEY_COLOR_TEMP))
  208. self.sw_device.store_data(**{self.sw_device.KEY_OUTPUT_0: False})
  209. time.sleep(DT_TOGGLE)