From c434aae77e6dcb5bca720b01a4c9a1967f157c67 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 4 Aug 2020 22:05:03 +0200 Subject: [PATCH] Finished remote --- hue_controller.py | 2 ++ hue_remote.py | 69 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/hue_controller.py b/hue_controller.py index f61f353..915cb2d 100644 --- a/hue_controller.py +++ b/hue_controller.py @@ -133,6 +133,8 @@ class controller: else: controller.setLightPreset( index, presetID ) + def countLights(): + return len(LIGHTS) # Controller "system" functions def delay(n:int): diff --git a/hue_remote.py b/hue_remote.py index 150441a..f16528c 100755 --- a/hue_remote.py +++ b/hue_remote.py @@ -11,7 +11,7 @@ def help(): print("--Help page--") print( "'" + cmd + "' : Display this help page" ) - print( "'" + cmd + " light (index)' ... : Specify light target" ) + print( "'" + cmd + " light (index)' ... : Specify light target, from 1-" + str(hue.controller.countLights()) ) print( "'" + cmd + " lights' ... : Specify all lights\n" ) print("--Commands--") @@ -31,26 +31,56 @@ boolConvert = { def parseCommand( cmd:list, pos:int, i=-1 ): index = int(i) - if( cmd[pos] == "on" or cmd[pos] == "off" ): - if( index == -1 ): - hue.controller.Power( boolConvert[cmd[pos]] ) - else: - hue.controller.powerLight( index, boolConvert[cmd[pos]] ) + try: + if( cmd[pos] == "on" or cmd[pos] == "off" ): + if( index == -1 ): + hue.controller.Power( boolConvert[cmd[pos]] ) + else: + hue.controller.powerLight( index, boolConvert[cmd[pos]] ) - elif( cmd[pos] == "switch" ): - if(index == -1): - hue.controller.switchLights() - else: - hue.controller.switchLight(index) + return - elif( cmd[pos] == "set" ): - if( cmd[pos+1] == "preset" ): - hue.controller.setPreset( cmd[pos+2], index ) + elif( cmd[pos] == "switch" ): + if(index == -1): + hue.controller.switchLights() + else: + hue.controller.switchLight(index) - #elif( cmd[pos+1] == "color" ): + return + elif( cmd[pos] == "set" ): + if( cmd[pos+1] == "preset" ): + hue.controller.setPreset( cmd[pos+2], index ) + return - #elif( cmd[pos+1] == "preset" ): + elif( cmd[pos+1] == "color" ): + if( len(cmd) > pos+4 ): + r, g, b = int(cmd[pos+2]), int(cmd[pos+3]), int(cmd[pos+4]) + + if( index == -1 ): + hue.controller.setAllLightsColor( r, g, b ) # this code is bad + else: + hue.controller.setLightColor( index, r, g, b ) + + return + else: + print("Error: Missing parameters") + help() + + elif( cmd[pos+1] == "brightness" ): + if( len(cmd) > pos+2 ): + bri = int(cmd[pos+2]) + + if( index == -1 ): + hue.controller.setBrightness(bri) + else: + hue.controller.setLightBrightness( index, bri ) + + return + help() # display help if function did nothing + + except (RuntimeError, TypeError, NameError, IndexError): + help() # display the help page if parameters are missing (it will give out an IndexError) def parseCommandline(): # this is the most spaghetti code I have ever written but it works and I do not intend to fix @@ -58,8 +88,7 @@ def parseCommandline(): # this is the most spaghetti code I have ever written bu print(cmd) if( len(cmd) > 1 ): if( cmd[1] == "light" ): - index = cmd[2] - parseCommand( cmd, 3, index ) + parseCommand( cmd, 3, cmd[2] ) elif( cmd[1] == "lights" ): parseCommand( cmd, 2 ) @@ -68,8 +97,8 @@ def parseCommandline(): # this is the most spaghetti code I have ever written bu def init(): - hue.controller.init() + hue.controller.init() # very important to initialize the controller parseCommandline() - hue.controller.end() + hue.controller.end() # also to end it init()