From 13e6aebd9972432f00c2bb3d62614ab86c4b23df Mon Sep 17 00:00:00 2001 From: Rich Date: Thu, 7 Apr 2016 21:33:16 -0400 Subject: [PATCH] Added options parsing, and createUserfunction --- hue_off.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/hue_off.py b/hue_off.py index 5b4b3eb..cf48f5c 100755 --- a/hue_off.py +++ b/hue_off.py @@ -1,21 +1,72 @@ import sys -from qhue import bridge +import getopt +from qhue import Bridge from qhue import create_new_username -def main(): +def main(argv): # IP address of your Hue Bridge - bridge = + bridge = "0" + username = "" + createuser = False # How often (in seconds) to check the pwrstatd log for events check_interval = 60 + # Parse the options + try: + opts, args = getopt.getopt(argv,"hb:ciu:",["bridge=","createuser=","interval=","username="]) + except getopt.GetoptError: + optUsage() + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + optUsage() + sys.exit() + elif opt in ("-b", "--bridge"): + bridge = arg + elif opt in ("-c", "--createuser"): + createuser = True + elif opt in ("-i", "--interval"): + check_interval = arg + elif opt in ("-u", "--username"): + username = arg + # Display error if user did not specify a username or ask to create one + if (username == "") & (createuser == False): + print "Error: No username specified!" + sys.exit(2) + # Display error if user specified a username AND asked to create one + elif (username != "") & (createuser == True): + print "Error: Can't create new user when username is specified!" + sys.exit(2) -def createUsername(): - # This function will create a new user account on the Hue bridge if the '-c' argument is passed + # Debugging + #print "Bridge IP: %s" % bridge + #print "Createuser: %s" % createuser + #print "Username: %s" % username + #raw_input("Press enter to continue") + + if createuser: + username = createUsername(bridge) +def createUsername(bridge): + # This function will create a new user account on the Hue bridge + # only if the '-c' argument is passed + + username = create_new_username("192.168.0.45") + print "Your Username is: %s" % username + return username +def optUsage(): + # This function displays options usage information + + print "Options Usage:" + print " -b, --bridge | IP address of your Hue Bridge" + print " -c, --createuser | Create a new user on your Hue Bridge" + print " -i, --interval | Set how often (in seconds) to check the pwrstatd log. Defaults to 60" + print " -u, --username | Username to be used for Hue Bridge API calls" + return 0 if __name__ == "__main__": - main() + main(sys.argv[1:])