fetchIP
E. Almqvist 4 years ago
parent fe25fda6da
commit ecb7d269ad
  1. 29
      hue_controller.py
  2. 12
      main.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()

@ -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()

Loading…
Cancel
Save