Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import colored
  2. import time
  3. DT_TOGGLE = 0.1
  4. class test_smarthome(object):
  5. def __init__(self, house):
  6. self.house = house
  7. def __smoke__(self):
  8. result = ""
  9. result += "Smoke-Test\n"
  10. result += " GUI Element exists for every shelly instance\n"
  11. result += self.__gui_element_exists__("shelly")
  12. result += " On-Off test for every shelly instance\n"
  13. result += self.__on_off_test__("shelly")
  14. result += " GUI Element exists for every silvercrest_powerplug instance\n"
  15. result += self.__gui_element_exists__("silvercrest_powerplug")
  16. result += " On-Off test for every silvercrest_powerplug instance\n"
  17. result += self.__on_off_test__("silvercrest_powerplug")
  18. result += " GUI Element exists for every my_powerplug instance and port\n"
  19. result += self.__gui_element_exists_my_powerplug__()
  20. result += " On-Off test for every my_powerplug instance\n"
  21. result += self.__on_off_test_my_powerplug__()
  22. result += " GUI Element exists for every tradfri_light instance\n"
  23. result += self.__gui_element_exists__("tradfri_light")
  24. result += " Enable and disable test for gui elements corresponding with tradfri_light\n"
  25. result += self.__br_ct_enable_test__()
  26. result += " Chnage brightness and color_temp by gui test\n"
  27. result += self.__br_ct_change_test__()
  28. return result
  29. def __full__(self):
  30. result = "Full-Test"
  31. return result
  32. def smoke(self):
  33. print(self.__smoke__())
  34. def full(self):
  35. out = self.__smoke__()
  36. out += self.__full__()
  37. print(out)
  38. def print_error(self, text, lvl=2):
  39. return lvl*" " + colored.fg("light_red") + '* ' + text + "\n" + colored.attr("reset")
  40. def print_success(self, text, lvl=2):
  41. return lvl*" " + colored.fg("light_green") + '* ' + text + "\n" + colored.attr("reset")
  42. def __gui_element_exists__(self, obj_name):
  43. result = ""
  44. for member in self.house.getmembers():
  45. obj = self.house.getobjbyname(member)
  46. if obj.__class__.__name__ == obj_name:
  47. if obj_name == "tradfri_light":
  48. basename = member[:member.index('_zigbee')]
  49. else:
  50. basename = member
  51. try:
  52. gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
  53. except AttributeError:
  54. result += self.print_error("No GUI element available, while testing %s (%s)." % (member, obj_name))
  55. else:
  56. result += self.print_success("GUI element available, while testing %s (%s)." % (member, obj_name))
  57. return result
  58. def __gui_element_exists_my_powerplug__(self):
  59. result = ""
  60. for member in self.house.getmembers():
  61. obj = self.house.getobjbyname(member)
  62. if obj.__class__.__name__ == "my_powerplug":
  63. for channel in [obj.KEY_OUTPUT_0, obj.KEY_OUTPUT_1, obj.KEY_OUTPUT_2, obj.KEY_OUTPUT_3]:
  64. try:
  65. gui = self.house.getobjbyname('.'.join(member.split(
  66. '.')[:-1]) + '.gui_' + obj.names.get(channel.lower(), "__dummy__").lower())
  67. except AttributeError:
  68. result += self.print_error("No GUI element available, while testing %s (%s)." % (member, obj.names.get(channel)))
  69. else:
  70. result += self.print_success("GUI element available, while testing %s (%s)." % (member, obj.names.get(channel)))
  71. return result
  72. def __on_off_test_my_powerplug__(self):
  73. result = ""
  74. for member in self.house.getmembers():
  75. obj = self.house.getobjbyname(member)
  76. if obj.__class__.__name__ == "my_powerplug":
  77. for channel in [obj.KEY_OUTPUT_0, obj.KEY_OUTPUT_1, obj.KEY_OUTPUT_2, obj.KEY_OUTPUT_3]:
  78. try:
  79. gui = self.house.getobjbyname('.'.join(member.split(
  80. '.')[:-1]) + '.gui_' + obj.names.get(channel.lower(), "__dummy__").lower())
  81. except AttributeError:
  82. raise AttributeError
  83. pass # exists test already covers non existing gui-elements
  84. else:
  85. success = True
  86. # Initial state equal between obj and gui
  87. obj_state = obj.data.get(channel)
  88. gui_state = gui.data.get(gui.KEY_STATE)
  89. if obj_state != gui_state:
  90. result += self.print_error("Initial state of %s (%s) is not equal to GUI state (%s), while testing %s (%s)" %
  91. ("my_powerplug", obj_state, gui_state, member, obj.names.get(channel)))
  92. success = False
  93. # state obj change results in state change of obj and gui
  94. for i in range(1, 3):
  95. gui.command("toggle_state")
  96. time.sleep(2 * DT_TOGGLE)
  97. last_obj_state = obj_state
  98. obj_state = obj.data.get(channel)
  99. gui_state = gui.data.get(gui.KEY_STATE)
  100. if last_obj_state == obj_state:
  101. result += self.print_error("State after %d. toggle of gui state: State unchanged (%s), while testing %s (%s)" %
  102. (i, obj_state, member, obj.names.get(channel)))
  103. success = False
  104. if obj_state != gui_state:
  105. result += self.print_error("State after %d. toggle of gui state:: State of device (%s) is not equal to GUI state (%s), while testing %s (%s)" %
  106. (i, obj_state, gui_state, member, obj.names.get(channel)))
  107. success = False
  108. #
  109. if success:
  110. result += self.print_success("On-Off test successfull, while testing %s (%s)." % (member, obj.names.get(channel)))
  111. return result
  112. def __on_off_test__(self, obj_name):
  113. result = ""
  114. for member in self.house.getmembers():
  115. obj = self.house.getobjbyname(member)
  116. if obj.__class__.__name__ == obj_name:
  117. try:
  118. gui = self.house.getobjbyname('.'.join(member.split('.')[:-1]) + '.gui_' + member.split('.')[-1])
  119. except AttributeError:
  120. pass # exists test already covers non existing gui-elements
  121. else:
  122. success = True
  123. # Initial state equal between obj and gui
  124. obj_state = obj.data.get(obj.KEY_OUTPUT_0).lower() == "on"
  125. gui_state = gui.data.get(gui.KEY_STATE)
  126. if obj_state != gui_state:
  127. result += self.print_error("Initial state of %s (%s) is not equal to GUI state (%s), while testing %s (%s)" %
  128. (obj_name, obj_state, gui_state, member, obj_name))
  129. success = False
  130. # state obj change results in state change of obj and gui
  131. for i in range(1, 3):
  132. gui.command("toggle_state")
  133. time.sleep(2 * DT_TOGGLE)
  134. last_obj_state = obj_state
  135. obj_state = obj.data.get(obj.KEY_OUTPUT_0).lower() == "on"
  136. gui_state = gui.data.get(gui.KEY_STATE)
  137. if last_obj_state == obj_state:
  138. result += self.print_error("State after %d. toggle of gui state: State unchanged (%s), while testing %s (%s)" %
  139. (i, obj_state, member, obj_name))
  140. success = False
  141. if obj_state != gui_state:
  142. result += self.print_error("State after %d. toggle of gui state:: State of device (%s) is not equal to GUI state (%s), while testing %s (%s)" %
  143. (i, obj_state, gui_state, member, obj_name))
  144. success = False
  145. #
  146. if success:
  147. result += self.print_success("On-Off test successfull, while testing %s." % (member))
  148. return result
  149. def __br_ct_enable_test__(self):
  150. result = ""
  151. for member in self.house.getmembers():
  152. obj = self.house.getobjbyname(member)
  153. if obj.__class__.__name__ == "tradfri_light":
  154. basename = member[:member.index('_zigbee')]
  155. gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
  156. success = True
  157. #
  158. if gui.data.get(gui.KEY_ENABLE) != False:
  159. result += self.print_error("Inital enable state is not False, while testing %s." % (member))
  160. success = False
  161. #
  162. gui.command("toggle_state")
  163. time.sleep(2 * DT_TOGGLE)
  164. if gui.data.get(gui.KEY_ENABLE) != True:
  165. result += self.print_error("Enable state is not True after switching on, while testing %s." % (member))
  166. success = False
  167. #
  168. gui.command("toggle_state")
  169. time.sleep(2 * DT_TOGGLE)
  170. if gui.data.get(gui.KEY_ENABLE) != False:
  171. result += self.print_error("Enable state is not False after switching off, while testing %s." % (member))
  172. success = False
  173. #
  174. if success:
  175. result += self.print_success("Enable-Disable test successfull, while testing %s." % (member))
  176. return result
  177. def __br_ct_change_test__(self):
  178. result = ""
  179. for member in self.house.getmembers():
  180. obj = self.house.getobjbyname(member)
  181. if obj.__class__.__name__ == "tradfri_light":
  182. basename = member[:member.index('_zigbee')]
  183. gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
  184. success = True
  185. #
  186. if gui.data.get(gui.KEY_STATE) != True:
  187. gui.command("toggle_state")
  188. time.sleep(2 * DT_TOGGLE)
  189. if gui.data.get(gui.KEY_STATE) != True:
  190. result += self.print_error("Unable to switch on light, while testing %s." % (member))
  191. success = False
  192. continue
  193. #
  194. if "set_brightness" in obj.capabilities():
  195. brightness = gui.data.get(obj.KEY_BRIGHTNESS)
  196. targetvalue = brightness + (25 if brightness <= 50 else -25)
  197. gui.command("set_brightness %d" % targetvalue)
  198. time.sleep(2 * DT_TOGGLE)
  199. if gui.data.get(obj.KEY_BRIGHTNESS) != targetvalue:
  200. result += self.print_error("Brightness change by gui was not successfull, while testing %s." % (member))
  201. success = False
  202. if "set_color_temp" in obj.capabilities():
  203. color_temp = gui.data.get(obj.KEY_COLOR_TEMP)
  204. targetvalue = color_temp + (3 if color_temp <= 5 else -3)
  205. gui.command("set_color_temp %d" % targetvalue)
  206. time.sleep(2 * DT_TOGGLE)
  207. if gui.data.get(obj.KEY_COLOR_TEMP) != targetvalue:
  208. result += self.print_error("Color temperature change by gui was not successfull, while testing %s." % (member))
  209. success = False
  210. #
  211. gui.command("toggle_state")
  212. time.sleep(2 * DT_TOGGLE)
  213. #
  214. if success:
  215. result += self.print_success("Brightness-ColorTemp test successfull, while testing %s." % (member))
  216. return result