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

119 lines
3.5 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
import time
from lib.func import * # useful functions
4 years ago
4 years ago
import config # Configuration for the controller (/config.py <- change this file)
LIGHTS = {} # dictionary of all the lights
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
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:
# Internal get functions
async def getLights():
return await APIrequest.get("/lights")
async def getLight(index: int=1):
return await APIrequest.get( "/lights/" + str(index) )
# Lower level light manipulation (async)
async def toggleLight(index: int=1, isOn: bool=True):
await APIrequest.put( "/lights/" + str(index) + "/state", '{"on":' + boolToString(isOn) + '}' )
4 years ago
async def toggleLights(isOn: bool=True):
for key in LIGHTS:
await controller.toggleLight(key, isOn)
async def setLightRGB( index: int, r:int, g:int, b:int ):
h, s, v = rgbToHsv(r, g, b)
payload = '{"sat":' + str(s) + ', "bri":' + str(v) + ', "hue":' + str(h) + '}'
await APIrequest.put( "/lights/" + str(index) + "/state", payload )
# Normal functions
def switchLight( index: int=1 ):
key = LIGHTS.get(str(index))
if(key):
4 years ago
if( key.get("state") ):
curPower = LIGHTS[str(index)]["state"]["on"]
loop.run_until_complete( controller.toggleLight(index, not curPower))
else:
print("Error: Light index '" + str(index) + "' out of range")
4 years ago
def switchLights():
for key in LIGHTS:
controller.switchLight(key)
4 years ago
# Light color
def setLightColor(index:int, r:int, g:int, b:int):
if( LIGHTS.get(str(index)) ):
loop.run_until_complete( controller.setLightRGB(index, r, g, b) )
else:
print("Error: Light index '" + str(index) + "' out of range")
def setAllLightsColor(r:int, g:int, b:int):
for key in LIGHTS:
controller.setLightColor(key, r, g, b)
def Power(isOn: bool=True): # Controlling the power of the lights
4 years ago
loop.run_until_complete( controller.toggleLights(isOn) )
# Controller "system" functions
def delay(n: int):
time.sleep(n)
def init():
jsonLights = loop.run_until_complete(APIrequest.get("/lights"))
global LIGHTS
LIGHTS = json.loads(jsonLights.text)
4 years ago
print(LIGHTS)
def end():
loop.close()
4 years ago
def testReq():
controller.init()
controller.Power(True)
4 years ago
controller.setAllLightsColor( 178, 199, 255 )
controller.end()