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.
108 lines
2.7 KiB
108 lines
2.7 KiB
4 years ago
|
#!/usr/bin/env python
|
||
4 years ago
|
# from lib.input import * # Commandline parser
|
||
|
|
||
|
import sys
|
||
4 years ago
|
|
||
|
import hue_controller as hue # Actual controller
|
||
|
|
||
|
cmd = "hue"
|
||
|
|
||
|
def help():
|
||
4 years ago
|
print("--Help page--")
|
||
4 years ago
|
|
||
4 years ago
|
print( "'" + cmd + "' : Display this help page" )
|
||
|
print( "'" + cmd + " light (index)' ... : Specify light target, from 1-" + str(hue.controller.countLights()) )
|
||
|
print( "'" + cmd + " lights' ... : Specify all lights\n" )
|
||
4 years ago
|
|
||
4 years ago
|
print("--Commands--")
|
||
|
print( "'on'/'off' : Turn light(s) on/off" )
|
||
|
print( "'switch' : Switch the light(s) power" )
|
||
|
print( "'set ...'" )
|
||
|
print( " 'preset (preset ID)' : Set the preset (from presets.py)" )
|
||
|
print( " 'color (red) (green) (blue)' : Set the color, from 0-255" )
|
||
|
print( " 'brightness (brightness)' : Set the brightness, from 0-255" )
|
||
4 years ago
|
|
||
4 years ago
|
print("\nExamples:\n'hue light 2 on' : Turn on light 2\n'hue lights set color 255 255 255' : Set all lights colors to white")
|
||
4 years ago
|
|
||
4 years ago
|
boolConvert = {
|
||
4 years ago
|
"on": True,
|
||
|
"off": False
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
# this is the most spaghetti-ish code I have ever written but it works
|
||
|
|
||
4 years ago
|
def parseCommand( cmd:list, pos:int, i=-1 ):
|
||
4 years ago
|
index = int(i)
|
||
|
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]] )
|
||
4 years ago
|
|
||
4 years ago
|
return
|
||
4 years ago
|
|
||
4 years ago
|
elif( cmd[pos] == "switch" ):
|
||
|
if(index == -1):
|
||
|
hue.controller.switchLights()
|
||
|
else:
|
||
|
hue.controller.switchLight(index)
|
||
4 years ago
|
|
||
4 years ago
|
return
|
||
4 years ago
|
|
||
4 years ago
|
elif( cmd[pos] == "set" ):
|
||
|
if( cmd[pos+1] == "preset" ):
|
||
|
hue.controller.setPreset( cmd[pos+2], index )
|
||
|
return
|
||
4 years ago
|
|
||
4 years ago
|
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])
|
||
4 years ago
|
|
||
4 years ago
|
if( index == -1 ):
|
||
|
hue.controller.setAllLightsColor( r, g, b ) # this code is bad
|
||
|
else:
|
||
|
hue.controller.setLightColor( index, r, g, b )
|
||
4 years ago
|
|
||
4 years ago
|
return
|
||
|
else:
|
||
|
print("Error: Missing parameters")
|
||
|
help()
|
||
4 years ago
|
|
||
4 years ago
|
elif( cmd[pos+1] == "brightness" ):
|
||
|
if( len(cmd) > pos+2 ):
|
||
|
bri = int(cmd[pos+2])
|
||
4 years ago
|
|
||
4 years ago
|
if( index == -1 ):
|
||
|
hue.controller.setBrightness(bri)
|
||
|
else:
|
||
|
hue.controller.setLightBrightness( index, bri )
|
||
4 years ago
|
|
||
4 years ago
|
return
|
||
|
help() # display help if function did nothing
|
||
4 years ago
|
|
||
4 years ago
|
except (RuntimeError, TypeError, NameError, IndexError) as err:
|
||
|
help() # display the help page if parameters are missing (it will give out an IndexError)
|
||
|
print( "\n\nError: " + str(err) )
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
def parseCommandline():
|
||
4 years ago
|
cmd = sys.argv
|
||
4 years ago
|
|
||
4 years ago
|
if( len(cmd) > 1 ):
|
||
|
if( cmd[1] == "light" ):
|
||
|
parseCommand( cmd, 3, cmd[2] )
|
||
4 years ago
|
|
||
4 years ago
|
elif( cmd[1] == "lights" ):
|
||
|
parseCommand( cmd, 2 )
|
||
|
else:
|
||
|
help()
|
||
4 years ago
|
|
||
|
|
||
|
def init():
|
||
4 years ago
|
hue.controller.init() # very important to initialize the controller
|
||
|
parseCommandline()
|
||
|
hue.controller.end() # also to end it
|
||
4 years ago
|
|
||
4 years ago
|
init() # actually call the init function
|