From 9075db95b34f48e2a8b7a1cf68f08758d4a816e9 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sun, 4 Oct 2020 17:48:30 +0200 Subject: [PATCH] Removed spaces and replaced them with tabs --- hue_remote/__pycache__/config.cpython-38.pyc | Bin 0 -> 249 bytes .../__pycache__/hue_controller.cpython-38.pyc | Bin 0 -> 5761 bytes hue_remote/__pycache__/presets.cpython-38.pyc | Bin 0 -> 468 bytes hue_remote/hue_controller.py | 248 +++++++++--------- hue_remote/hue_remote.py | 132 +++++----- 5 files changed, 190 insertions(+), 190 deletions(-) create mode 100644 hue_remote/__pycache__/config.cpython-38.pyc create mode 100644 hue_remote/__pycache__/hue_controller.cpython-38.pyc create mode 100644 hue_remote/__pycache__/presets.cpython-38.pyc diff --git a/hue_remote/__pycache__/config.cpython-38.pyc b/hue_remote/__pycache__/config.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..02d91d3aa22e32b0405f896968938bb2b33d97a6 GIT binary patch literal 249 zcmWIL<>g`kf;YD+o z3rb5;i>kN{EsgXH%`Ef`^o*l4Tn&m!4IER$+`L^q3|);4B1{at9KF)=%hF9kOAQl4 zvQvt@1C9JM{WO_xu@w}hre#*#Voyv-DM~FazQs{moLZEZn47wip@;=&FPQjcs-Kac zo2s9hlbE9)P?VpQnp{$>UzDGp3v+^gMrmq%QEF~}NveKweqLH;x?Vx$Ee@O9{FKt1 QR68b+H$cX*FtacM0O&qLj{pDw literal 0 HcmV?d00001 diff --git a/hue_remote/__pycache__/hue_controller.cpython-38.pyc b/hue_remote/__pycache__/hue_controller.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c378753c02e6c3220ba72d0e17d366b6af7e32a7 GIT binary patch literal 5761 zcmd5=&2!tv6~`_<2vHJE)3RhGw!FZ~;E?a7z^0lKumx8Mgr(`}EXgoj<=VfXEO@ArEP zJeZx$YWVg3xmW#bUeo?To#anPXB9^vA%rHFt5ujf^9omIy`rnLQ894luDKrTSrtq5 z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7DS}u>yvz!S_ZwKska}wOt;mBh<1CDwqkO`30J6ll?26KUMHd64HN{vrY>#%)GkSW zORe9Mn6Nap^P(aboPn@)ThAK>9CMlclx@yg`Ap6z7u8_5joNim{nbHxwm&D)4 z0`<{IKEwd{${Fl=^@cCksb{;5jpauzuP&(`S28h!1PT|G9A2NR&J)KAtF%9lGeoU| lcs_o4Rr|sTW=Hm=__NSuO2~l96oD6i9Dk4o&N)4I`rnvwg){&F literal 0 HcmV?d00001 diff --git a/hue_remote/__pycache__/presets.cpython-38.pyc b/hue_remote/__pycache__/presets.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bba3543e66d880cd02b86c646c20691ccf889214 GIT binary patch literal 468 zcma)!KTg9i6o=ioX+oh0iHRd*AxumaV(G$wDhf-Ms*N9QkyA&{iA2vrFVK~Z6L1H2 zL_+cv7=ULJ5NufT_p`tE=cd=|GCI-gYP<;;dvTxN$5qei>I;Hkq9uZLK*^hI`wSc3 zX)Jl;;zxhfz9Ig#lQ)g9!OPEn~MS4CM@ ofa$!3F+!m#NJ|6_l|C+4ChiwX)Dq4@TBT12K`VG~cmB=oC->EFXaE2J literal 0 HcmV?d00001 diff --git a/hue_remote/hue_controller.py b/hue_remote/hue_controller.py index b7777b5..20bd133 100644 --- a/hue_remote/hue_controller.py +++ b/hue_remote/hue_controller.py @@ -13,140 +13,140 @@ LIGHTS = {} # dictionary of all the lights loop = asyncio.get_event_loop() # ASync loop def genUrl(params: str): - return "http://" + config.address + "/api/" + config.username + params + return "http://" + config.address + "/api/" + config.username + params class APIrequest: - # Get Req - async def get( dest: str="", payload: str="" ): - try: - apiReq = req.get( genUrl(dest), data = payload ) + # Get Req + async def get( dest: str="", payload: str="" ): + try: + apiReq = req.get( genUrl(dest), data = payload ) - if( apiReq.status_code != 200 ): # print out the error if the status code is not 200 - print(apiReq) - print(apiReq.text) + if( apiReq.status_code != 200 ): # print out the error if the status code is not 200 + print(apiReq) + print(apiReq.text) - return apiReq + return apiReq - except req.exceptions.RequestException as err: - print(err) + except req.exceptions.RequestException as err: + print(err) - # PUT Req - async def put( dest: str="", payload: str="" ): - try: - apiReq = req.put( genUrl(dest), data = payload ) # send the payload + # PUT Req + async def put( dest: str="", payload: str="" ): + try: + apiReq = req.put( genUrl(dest), data = payload ) # send the payload - if( apiReq.status_code != 200 ): - print(apiReq) - print(apiReq.text) + if( apiReq.status_code != 200 ): + print(apiReq) + print(apiReq.text) - return apiReq + return apiReq - except req.exceptions.RequestException as err: - print(err) + except req.exceptions.RequestException as err: + print(err) class controller: - # Internal get functions - async def getLights(): - return await APIrequest.get("/lights") - - async def getLight(index: int=1): - return await APIrequest.get( "/lights/" + str(index) ) - - # Lower level light manipulation (async) - async def toggleLight(index: int=1, isOn: bool=True): - await APIrequest.put( "/lights/" + str(index) + "/state", '{"on":' + boolToString(isOn) + '}' ) - - async def toggleLights(isOn: bool=True): - for key in LIGHTS: - await controller.toggleLight(key, isOn) - - async def setLightRGB( index: int, r:int, g:int, b:int ): - h, s, v = rgbToHsv(r, g, b) - payload = '{"sat":' + str(s) + ', "bri":' + str(v) + ', "hue":' + str(h) + '}' - - await APIrequest.put( "/lights/" + str(index) + "/state", payload ) - - # Normal functions - def switchLight( index: int=1 ): - key = LIGHTS.get(str(index)) - if(key): - if( key.get("state") ): - curPower = LIGHTS[str(index)]["state"]["on"] - loop.run_until_complete( controller.toggleLight(index, not curPower)) - else: - print("Error: Light index '" + str(index) + "' out of range") - - def switchLights(): - for key in LIGHTS: - controller.switchLight(key) - - # Light control - def setLightColor( index:int, r:int, g:int, b:int ): - if( LIGHTS.get(str(index)) ): - loop.run_until_complete( controller.setLightRGB(index, r, g, b) ) - else: - print("Error: Light index '" + str(index) + "' out of range") - - def setLightBrightness( index:int, b:int ): - if( LIGHTS.get(str(index)) ): - payload = '{"bri":' + str(b) + '}' - loop.run_until_complete( APIrequest.put( "/lights/" + str(index) + "/state", payload ) ) - else: - print("Error: Light index '" + str(index) + "' out of range") - - def setBrightness( b:int ): - for key in LIGHTS: - controller.setLightBrightness( key, b ) - - def setAllLightsColor( r:int, g:int, b:int ): - for key in LIGHTS: - controller.setLightColor( key, r, g, b ) - - def Power(isOn:bool=True): # Controlling the power of the lights - loop.run_until_complete( controller.toggleLights(isOn) ) - - def powerLight( index:int, isOn:bool=True ): - loop.run_until_complete( controller.toggleLight( index, isOn ) ) - - # Presets - def setLightPreset( index:int, p:str ): - if( LIGHTS.get(str(index)) ): - if( PRESETS.get(p) ): - preset = PRESETS[p] - r, g, b = preset["color"] - brightness = preset["brightness"] - - controller.setLightColor( index, r, g, b ) - controller.setLightBrightness( index, brightness ) - else: - print("Error: Unknown preset '" + p + "'") - else: - print("Error: Light index '" + str(index) + "' out of range") - - def setPreset( presetID:str, index:int=-1 ): - if( PRESETS.get(presetID) ): - if( index == -1 ): - for key in LIGHTS: - controller.setLightPreset( key, presetID ) - else: - controller.setLightPreset( index, presetID ) - else: - print("Error: Unknown preset '" + presetID + "'") - - def countLights(): - return len(LIGHTS) - - # Controller "system" functions - def delay(n:int): - time.sleep(n) - - def init(): - jsonLights = loop.run_until_complete(APIrequest.get("/lights")) - - global LIGHTS - LIGHTS = json.loads(jsonLights.text) - - def end(): - loop.close() + # Internal get functions + async def getLights(): + return await APIrequest.get("/lights") + + async def getLight(index: int=1): + return await APIrequest.get( "/lights/" + str(index) ) + + # Lower level light manipulation (async) + async def toggleLight(index: int=1, isOn: bool=True): + await APIrequest.put( "/lights/" + str(index) + "/state", '{"on":' + boolToString(isOn) + '}' ) + + async def toggleLights(isOn: bool=True): + for key in LIGHTS: + await controller.toggleLight(key, isOn) + + async def setLightRGB( index: int, r:int, g:int, b:int ): + h, s, v = rgbToHsv(r, g, b) + payload = '{"sat":' + str(s) + ', "bri":' + str(v) + ', "hue":' + str(h) + '}' + + await APIrequest.put( "/lights/" + str(index) + "/state", payload ) + + # Normal functions + def switchLight( index: int=1 ): + key = LIGHTS.get(str(index)) + if(key): + if( key.get("state") ): + curPower = LIGHTS[str(index)]["state"]["on"] + loop.run_until_complete( controller.toggleLight(index, not curPower)) + else: + print("Error: Light index '" + str(index) + "' out of range") + + def switchLights(): + for key in LIGHTS: + controller.switchLight(key) + + # Light control + def setLightColor( index:int, r:int, g:int, b:int ): + if( LIGHTS.get(str(index)) ): + loop.run_until_complete( controller.setLightRGB(index, r, g, b) ) + else: + print("Error: Light index '" + str(index) + "' out of range") + + def setLightBrightness( index:int, b:int ): + if( LIGHTS.get(str(index)) ): + payload = '{"bri":' + str(b) + '}' + loop.run_until_complete( APIrequest.put( "/lights/" + str(index) + "/state", payload ) ) + else: + print("Error: Light index '" + str(index) + "' out of range") + + def setBrightness( b:int ): + for key in LIGHTS: + controller.setLightBrightness( key, b ) + + def setAllLightsColor( r:int, g:int, b:int ): + for key in LIGHTS: + controller.setLightColor( key, r, g, b ) + + def Power(isOn:bool=True): # Controlling the power of the lights + loop.run_until_complete( controller.toggleLights(isOn) ) + + def powerLight( index:int, isOn:bool=True ): + loop.run_until_complete( controller.toggleLight( index, isOn ) ) + + # Presets + def setLightPreset( index:int, p:str ): + if( LIGHTS.get(str(index)) ): + if( PRESETS.get(p) ): + preset = PRESETS[p] + r, g, b = preset["color"] + brightness = preset["brightness"] + + controller.setLightColor( index, r, g, b ) + controller.setLightBrightness( index, brightness ) + else: + print("Error: Unknown preset '" + p + "'") + else: + print("Error: Light index '" + str(index) + "' out of range") + + def setPreset( presetID:str, index:int=-1 ): + if( PRESETS.get(presetID) ): + if( index == -1 ): + for key in LIGHTS: + controller.setLightPreset( key, presetID ) + else: + controller.setLightPreset( index, presetID ) + else: + print("Error: Unknown preset '" + presetID + "'") + + def countLights(): + return len(LIGHTS) + + # Controller "system" functions + def delay(n:int): + time.sleep(n) + + def init(): + jsonLights = loop.run_until_complete(APIrequest.get("/lights")) + + global LIGHTS + LIGHTS = json.loads(jsonLights.text) + + def end(): + loop.close() diff --git a/hue_remote/hue_remote.py b/hue_remote/hue_remote.py index 65005ff..d0bf730 100755 --- a/hue_remote/hue_remote.py +++ b/hue_remote/hue_remote.py @@ -8,100 +8,100 @@ import hue_controller as hue # Actual controller cmd = "hue" def help(): - print("--Help page--") + print("--Help page--") - 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" ) + 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" ) - 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" ) + 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" ) - 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") + 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") boolConvert = { - "on": True, - "off": False + "on": True, + "off": False } # this is the most spaghetti-ish code I have ever written but it works def parseCommand( cmd:list, pos:int, i=-1 ): - 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]] ) + 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]] ) - return + return - elif( cmd[pos] == "switch" ): - if(index == -1): - hue.controller.switchLights() - else: - hue.controller.switchLight(index) + elif( cmd[pos] == "switch" ): + if(index == -1): + hue.controller.switchLights() + else: + hue.controller.switchLight(index) - return + return - elif( cmd[pos] == "set" ): - if( cmd[pos+1] == "preset" ): - hue.controller.setPreset( cmd[pos+2], index ) - return + elif( cmd[pos] == "set" ): + if( cmd[pos+1] == "preset" ): + hue.controller.setPreset( cmd[pos+2], index ) + return - 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]) + 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 ) + 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() + return + else: + print("Error: Missing parameters") + help() - elif( cmd[pos+1] == "brightness" ): - if( len(cmd) > pos+2 ): - bri = int(cmd[pos+2]) + 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 ) + if( index == -1 ): + hue.controller.setBrightness(bri) + else: + hue.controller.setLightBrightness( index, bri ) - return - help() # display help if function did nothing + return + help() # display help if function did nothing - 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) ) + 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) ) def parseCommandline(): - cmd = sys.argv + cmd = sys.argv - if( len(cmd) > 1 ): - if( cmd[1] == "light" ): - parseCommand( cmd, 3, cmd[2] ) + if( len(cmd) > 1 ): + if( cmd[1] == "light" ): + parseCommand( cmd, 3, cmd[2] ) - elif( cmd[1] == "lights" ): - parseCommand( cmd, 2 ) - else: - help() + elif( cmd[1] == "lights" ): + parseCommand( cmd, 2 ) + else: + help() def init(): - hue.controller.init() # very important to initialize the controller - parseCommandline() - hue.controller.end() # also to end it + hue.controller.init() # very important to initialize the controller + parseCommandline() + hue.controller.end() # also to end it init() # actually call the init function