parent
fe25fda6da
commit
ecb7d269ad
@ -1,20 +1,39 @@ |
|||||||
import requests as req # Used for HTTP requests for the Hue API |
import requests as req # Used for HTTP requests for the Hue API |
||||||
import json # API uses JSON |
import json # API uses JSON |
||||||
|
import asyncio # ASync stuff |
||||||
import config # Configuration for the controller (/config.py <- change this file) |
import config # Configuration for the controller (/config.py <- change this file) |
||||||
|
|
||||||
|
loop = asyncio.get_event_loop() # ASync loop |
||||||
|
|
||||||
def genUrl(params: str): |
def genUrl(params: str): |
||||||
return "http://" + config.address + "/api/" + config.username + params |
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: |
try: |
||||||
apiReq = req.Request( ReqType, genUrl(params) ) |
apiReq = req.Request( method, genUrl(params) ) # initialize the request |
||||||
apiReqPrep = apiReq.prepare() |
apiReqPrep = apiReq.prepare() # prepair it |
||||||
|
|
||||||
|
apiReqPrep.body = body # apply the body to the request |
||||||
|
|
||||||
apiSession = req.Session() |
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: |
except req.exceptions.RequestException as err: |
||||||
raise SystemExit(err) # throw the error |
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() |
||||||
|
Loading…
Reference in new issue