233 行
12 KiB
Python

import colored
import time
DT_TOGGLE = 0.1
class test_smarthome(object):
def __init__(self, house):
self.house = house
def __smoke__(self):
result = ""
result += "Smoke-Test\n"
result += " GUI Element exists for every shelly instance\n"
result += self.__gui_element_exists__("shelly")
result += " On-Off test for every shelly instance\n"
result += self.__on_off_test__("shelly")
result += " GUI Element exists for every silvercrest_powerplug instance\n"
result += self.__gui_element_exists__("silvercrest_powerplug")
result += " On-Off test for every silvercrest_powerplug instance\n"
result += self.__on_off_test__("silvercrest_powerplug")
result += " GUI Element exists for every my_powerplug instance and port\n"
result += self.__gui_element_exists_my_powerplug__()
result += " On-Off test for every my_powerplug instance\n"
result += self.__on_off_test_my_powerplug__()
result += " GUI Element exists for every tradfri_light instance\n"
result += self.__gui_element_exists__("tradfri_light")
result += " Enable and disable test for gui elements corresponding with tradfri_light\n"
result += self.__br_ct_enable_test__()
result += " Chnage brightness and color_temp by gui test\n"
result += self.__br_ct_change_test__()
return result
def __full__(self):
result = "Full-Test"
return result
def smoke(self):
print(self.__smoke__())
def full(self):
out = self.__smoke__()
out += self.__full__()
print(out)
def print_error(self, text, lvl=2):
return lvl*" " + colored.fg("light_red") + '* ' + text + "\n" + colored.attr("reset")
def print_success(self, text, lvl=2):
return lvl*" " + colored.fg("light_green") + '* ' + text + "\n" + colored.attr("reset")
def __gui_element_exists__(self, obj_name):
result = ""
for member in self.house.getmembers():
obj = self.house.getobjbyname(member)
if obj.__class__.__name__ == obj_name:
if obj_name == "tradfri_light":
basename = member[:member.index('_zigbee')]
else:
basename = member
try:
gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
except AttributeError:
result += self.print_error("No GUI element available, while testing %s (%s)." % (member, obj_name))
else:
result += self.print_success("GUI element available, while testing %s (%s)." % (member, obj_name))
return result
def __gui_element_exists_my_powerplug__(self):
result = ""
for member in self.house.getmembers():
obj = self.house.getobjbyname(member)
if obj.__class__.__name__ == "my_powerplug":
for channel in [obj.KEY_OUTPUT_0, obj.KEY_OUTPUT_1, obj.KEY_OUTPUT_2, obj.KEY_OUTPUT_3]:
try:
gui = self.house.getobjbyname('.'.join(member.split(
'.')[:-1]) + '.gui_' + obj.names.get(channel.lower(), "__dummy__").lower())
except AttributeError:
result += self.print_error("No GUI element available, while testing %s (%s)." % (member, obj.names.get(channel)))
else:
result += self.print_success("GUI element available, while testing %s (%s)." % (member, obj.names.get(channel)))
return result
def __on_off_test_my_powerplug__(self):
result = ""
for member in self.house.getmembers():
obj = self.house.getobjbyname(member)
if obj.__class__.__name__ == "my_powerplug":
for channel in [obj.KEY_OUTPUT_0, obj.KEY_OUTPUT_1, obj.KEY_OUTPUT_2, obj.KEY_OUTPUT_3]:
try:
gui = self.house.getobjbyname('.'.join(member.split(
'.')[:-1]) + '.gui_' + obj.names.get(channel.lower(), "__dummy__").lower())
except AttributeError:
raise AttributeError
pass # exists test already covers non existing gui-elements
else:
success = True
# Initial state equal between obj and gui
obj_state = obj.data.get(channel)
gui_state = gui.data.get(gui.KEY_STATE)
if obj_state != gui_state:
result += self.print_error("Initial state of %s (%s) is not equal to GUI state (%s), while testing %s (%s)" %
("my_powerplug", obj_state, gui_state, member, obj.names.get(channel)))
success = False
# state obj change results in state change of obj and gui
for i in range(1, 3):
gui.command("toggle_state")
time.sleep(2 * DT_TOGGLE)
last_obj_state = obj_state
obj_state = obj.data.get(channel)
gui_state = gui.data.get(gui.KEY_STATE)
if last_obj_state == obj_state:
result += self.print_error("State after %d. toggle of gui state: State unchanged (%s), while testing %s (%s)" %
(i, obj_state, member, obj.names.get(channel)))
success = False
if obj_state != gui_state:
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)" %
(i, obj_state, gui_state, member, obj.names.get(channel)))
success = False
#
if success:
result += self.print_success("On-Off test successfull, while testing %s (%s)." % (member, obj.names.get(channel)))
return result
def __on_off_test__(self, obj_name):
result = ""
for member in self.house.getmembers():
obj = self.house.getobjbyname(member)
if obj.__class__.__name__ == obj_name:
try:
gui = self.house.getobjbyname('.'.join(member.split('.')[:-1]) + '.gui_' + member.split('.')[-1])
except AttributeError:
pass # exists test already covers non existing gui-elements
else:
success = True
# Initial state equal between obj and gui
obj_state = obj.data.get(obj.KEY_OUTPUT_0).lower() == "on"
gui_state = gui.data.get(gui.KEY_STATE)
if obj_state != gui_state:
result += self.print_error("Initial state of %s (%s) is not equal to GUI state (%s), while testing %s (%s)" %
(obj_name, obj_state, gui_state, member, obj_name))
success = False
# state obj change results in state change of obj and gui
for i in range(1, 3):
gui.command("toggle_state")
time.sleep(2 * DT_TOGGLE)
last_obj_state = obj_state
obj_state = obj.data.get(obj.KEY_OUTPUT_0).lower() == "on"
gui_state = gui.data.get(gui.KEY_STATE)
if last_obj_state == obj_state:
result += self.print_error("State after %d. toggle of gui state: State unchanged (%s), while testing %s (%s)" %
(i, obj_state, member, obj_name))
success = False
if obj_state != gui_state:
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)" %
(i, obj_state, gui_state, member, obj_name))
success = False
#
if success:
result += self.print_success("On-Off test successfull, while testing %s." % (member))
return result
def __br_ct_enable_test__(self):
result = ""
for member in self.house.getmembers():
obj = self.house.getobjbyname(member)
if obj.__class__.__name__ == "tradfri_light":
basename = member[:member.index('_zigbee')]
gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
success = True
#
if gui.data.get(gui.KEY_ENABLE) != False:
result += self.print_error("Inital enable state is not False, while testing %s." % (member))
success = False
#
gui.command("toggle_state")
time.sleep(2 * DT_TOGGLE)
if gui.data.get(gui.KEY_ENABLE) != True:
result += self.print_error("Enable state is not True after switching on, while testing %s." % (member))
success = False
#
gui.command("toggle_state")
time.sleep(2 * DT_TOGGLE)
if gui.data.get(gui.KEY_ENABLE) != False:
result += self.print_error("Enable state is not False after switching off, while testing %s." % (member))
success = False
#
if success:
result += self.print_success("Enable-Disable test successfull, while testing %s." % (member))
return result
def __br_ct_change_test__(self):
result = ""
for member in self.house.getmembers():
obj = self.house.getobjbyname(member)
if obj.__class__.__name__ == "tradfri_light":
basename = member[:member.index('_zigbee')]
gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
success = True
#
if gui.data.get(gui.KEY_STATE) != True:
gui.command("toggle_state")
time.sleep(2 * DT_TOGGLE)
if gui.data.get(gui.KEY_STATE) != True:
result += self.print_error("Unable to switch on light, while testing %s." % (member))
success = False
continue
#
if "set_brightness" in obj.capabilities():
brightness = gui.data.get(obj.KEY_BRIGHTNESS)
targetvalue = brightness + (25 if brightness <= 50 else -25)
gui.command("set_brightness %d" % targetvalue)
time.sleep(2 * DT_TOGGLE)
if gui.data.get(obj.KEY_BRIGHTNESS) != targetvalue:
result += self.print_error("Brightness change by gui was not successfull, while testing %s." % (member))
success = False
if "set_color_temp" in obj.capabilities():
color_temp = gui.data.get(obj.KEY_COLOR_TEMP)
targetvalue = color_temp + (3 if color_temp <= 5 else -3)
gui.command("set_color_temp %d" % targetvalue)
time.sleep(2 * DT_TOGGLE)
if gui.data.get(obj.KEY_COLOR_TEMP) != targetvalue:
result += self.print_error("Color temperature change by gui was not successfull, while testing %s." % (member))
success = False
#
gui.command("toggle_state")
time.sleep(2 * DT_TOGGLE)
#
if success:
result += self.print_success("Brightness-ColorTemp test successfull, while testing %s." % (member))
return result