CLI tool to control your IoT gadgets.
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.
roomcomputer/hue_controller.py

40 lines
1.3 KiB

import requests as req # Used for HTTP requests for the Hue API
import json # API uses JSON
4 years ago
import asyncio # ASync stuff
4 years ago
import config # Configuration for the controller (/config.py <- change this file)
4 years ago
loop = asyncio.get_event_loop() # ASync loop
def genUrl(params: str):
4 years ago
return "http://" + config.address + "/api/" + config.username + params
4 years ago
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=""):
4 years ago
try:
4 years ago
apiReq = req.Request( method, genUrl(params) ) # initialize the request
apiReqPrep = apiReq.prepare() # prepair it
apiReqPrep.body = body # apply the body to the request
4 years ago
apiSession = req.Session()
4 years ago
response = apiSession.send(apiReqPrep) # send it to the Hue bridge
4 years ago
4 years ago
print(response)
print(response.text)
4 years ago
except req.exceptions.RequestException as err:
raise SystemExit(err) # throw the error
4 years ago
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
4 years ago
loop.run_until_complete( setLightPower(1, False) ) # try to turn on/off a light
loop.close()