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

56 lines
1.6 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"
class APIrequest:
# Get Req
async def get( dest: str="", payload: str="{}" ):
try:
apiReq = req.get( genUrl(dest), data = payload )
return apiReq
4 years ago
except req.exceptions.RequestException as err:
print(err)
# POST Req
async def post( dest: str="", payload: str="{}" ):
try:
apiReq = req.post( genUrl(params), data = payload )
return apiReq
4 years ago
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
print(apiReq)
print(apiReq.text)
return apiReq
except req.exceptions.RequestException as err:
print(err)
class controller:
async def toggleLight(index: int=1, isOn: bool=True):
await APIrequest.put( "/lights/1/state", '{"on":' + boolToString(isOn) + '}' )
4 years ago
def testReq():
loop.run_until_complete( controller.toggleLight(1, True) ) # try to turn on/off a light
4 years ago
loop.close()