diff --git a/hue_controller.py b/hue_controller.py index 3aafd08..1694d2d 100644 --- a/hue_controller.py +++ b/hue_controller.py @@ -1,20 +1,39 @@ import requests as req # Used for HTTP requests for the Hue API import json # API uses JSON +import asyncio # ASync stuff import config # Configuration for the controller (/config.py <- change this file) +loop = asyncio.get_event_loop() # ASync loop + def genUrl(params: str): return "http://" + config.address + "/api/" + config.username + params -await def API_Request(ReqType: str="GET", params: str=""): # Requests for the API +def boolToString(v: bool): # To fix the dumb python syntax + if(v): + return "true" + else: + return "false" + +async def API_Request(method: str="GET", params: str="", body: str=""): try: - apiReq = req.Request( ReqType, genUrl(params) ) - apiReqPrep = apiReq.prepare() + apiReq = req.Request( method, genUrl(params) ) # initialize the request + apiReqPrep = apiReq.prepare() # prepair it + + apiReqPrep.body = body # apply the body to the request apiSession = req.Session() - response = apiSession.send(apiReqPrep) + response = apiSession.send(apiReqPrep) # send it to the Hue bridge - print(response.body) + print(response) + print(response.text) except req.exceptions.RequestException as err: raise SystemExit(err) # throw the error +async def setLightPower(index: int=1, on: bool=True): + await API_Request( "PUT", "/lights/" + str(index) + "/state", "{'on':" + boolToString(on) + "}" ) + +def testReq(): + # loop.run_until_complete( API_Request("GET", "/lights/1") ) # get test + loop.run_until_complete( setLightPower(1, False) ) # try to turn on/off a light + loop.close() diff --git a/main.py b/main.py index 29effa0..324048d 100755 --- a/main.py +++ b/main.py @@ -3,17 +3,7 @@ from lib.input import * # Commandline parser import hue_controller as hue # Actual controller -params = "" -if( inputHasKeys(["-p"]) ): - params = getValueOfKey("-p") - -reqtype = "GET" -if( inputHasKeys(["-t"]) ): - reqtype = getValueOfKey("-t") - - def init(): - print(reqtype, params) - hue.API_Request(reqtype, params) + hue.testReq() init()