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.
153 lines
4.8 KiB
153 lines
4.8 KiB
4 years ago
|
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 time
|
||
|
|
||
|
from lib.func import * # useful functions
|
||
4 years ago
|
|
||
4 years ago
|
import config # Configuration for the controller (/config.py <- change this file)
|
||
4 years ago
|
from presets import * # presets for the lights
|
||
4 years ago
|
|
||
4 years ago
|
LIGHTS = {} # dictionary of all the lights
|
||
|
|
||
4 years ago
|
loop = asyncio.get_event_loop() # ASync loop
|
||
|
|
||
4 years ago
|
def genUrl(params: str):
|
||
4 years ago
|
return "http://" + config.address + "/api/" + config.username + params
|
||
4 years ago
|
|
||
4 years ago
|
class APIrequest:
|
||
|
# Get Req
|
||
4 years ago
|
async def get( dest: str="", payload: str="" ):
|
||
4 years ago
|
try:
|
||
|
apiReq = req.get( genUrl(dest), data = payload )
|
||
4 years ago
|
|
||
4 years ago
|
if( apiReq.status_code != 200 ): # print out the error if the status code is not 200
|
||
|
print(apiReq)
|
||
|
print(apiReq.text)
|
||
4 years ago
|
|
||
4 years ago
|
return apiReq
|
||
4 years ago
|
|
||
4 years ago
|
except req.exceptions.RequestException as err:
|
||
|
print(err)
|
||
4 years ago
|
|
||
4 years ago
|
# PUT Req
|
||
4 years ago
|
async def put( dest: str="", payload: str="" ):
|
||
4 years ago
|
try:
|
||
|
apiReq = req.put( genUrl(dest), data = payload ) # send the payload
|
||
4 years ago
|
|
||
|
if( apiReq.status_code != 200 ):
|
||
|
print(apiReq)
|
||
|
print(apiReq.text)
|
||
|
|
||
4 years ago
|
return apiReq
|
||
|
|
||
|
except req.exceptions.RequestException as err:
|
||
|
print(err)
|
||
|
|
||
|
|
||
|
class controller:
|
||
4 years ago
|
|
||
4 years ago
|
# Internal get functions
|
||
4 years ago
|
async def getLights():
|
||
|
return await APIrequest.get("/lights")
|
||
|
|
||
|
async def getLight(index: int=1):
|
||
|
return await APIrequest.get( "/lights/" + str(index) )
|
||
|
|
||
4 years ago
|
# Lower level light manipulation (async)
|
||
4 years ago
|
async def toggleLight(index: int=1, isOn: bool=True):
|
||
4 years ago
|
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)
|
||
|
|
||
4 years ago
|
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
|
||
4 years ago
|
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))
|
||
4 years ago
|
else:
|
||
4 years ago
|
print("Error: Light index '" + str(index) + "' out of range")
|
||
4 years ago
|
|
||
4 years ago
|
def switchLights():
|
||
|
for key in LIGHTS:
|
||
|
controller.switchLight(key)
|
||
|
|
||
4 years ago
|
# Light control
|
||
|
def setLightColor( index:int, r:int, g:int, b:int ):
|
||
4 years ago
|
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")
|
||
|
|
||
4 years ago
|
def setLightBrightness( index:int, b:int ):
|
||
|
if( LIGHTS.get(str(index)) ):
|
||
|
payload = '{"bri":' + str(b) + '}'
|
||
4 years ago
|
loop.run_until_complete( APIrequest.put( "/lights/" + str(index) + "/state", payload ) )
|
||
4 years ago
|
else:
|
||
|
print("Error: Light index '" + str(index) + "' out of range")
|
||
|
|
||
4 years ago
|
def setBrightness( b:int ):
|
||
|
for key in LIGHTS:
|
||
|
controller.setLightBrightness( key, b )
|
||
4 years ago
|
|
||
|
def setAllLightsColor( r:int, g:int, b:int ):
|
||
4 years ago
|
for key in LIGHTS:
|
||
4 years ago
|
controller.setLightColor( key, r, g, b )
|
||
4 years ago
|
|
||
4 years ago
|
def Power(isOn:bool=True): # Controlling the power of the lights
|
||
4 years ago
|
loop.run_until_complete( controller.toggleLights(isOn) )
|
||
|
|
||
4 years ago
|
def powerLight( index:int, isOn:bool=True ):
|
||
|
loop.run_until_complete( controller.toggleLight( index, isOn ) )
|
||
|
|
||
4 years ago
|
# Presets
|
||
4 years ago
|
def setLightPreset( index:int, p:str ):
|
||
4 years ago
|
if( LIGHTS.get(str(index)) ):
|
||
|
if( PRESETS.get(p) ):
|
||
|
preset = PRESETS[p]
|
||
|
r, g, b = preset["color"]
|
||
4 years ago
|
brightness = preset["brightness"]
|
||
|
|
||
4 years ago
|
controller.setLightColor( index, r, g, b )
|
||
4 years ago
|
controller.setLightBrightness( index, brightness )
|
||
4 years ago
|
else:
|
||
|
print("Error: Unknown preset '" + p + "'")
|
||
|
else:
|
||
|
print("Error: Light index '" + str(index) + "' out of range")
|
||
|
|
||
4 years ago
|
def setPreset( presetID:str, index:int=-1 ):
|
||
4 years ago
|
if( PRESETS.get(presetID) ):
|
||
|
if( index == -1 ):
|
||
|
for key in LIGHTS:
|
||
|
controller.setLightPreset( key, presetID )
|
||
|
else:
|
||
|
controller.setLightPreset( index, presetID )
|
||
4 years ago
|
else:
|
||
4 years ago
|
print("Error: Unknown preset '" + presetID + "'")
|
||
4 years ago
|
|
||
4 years ago
|
def countLights():
|
||
|
return len(LIGHTS)
|
||
4 years ago
|
|
||
4 years ago
|
# Controller "system" functions
|
||
4 years ago
|
def delay(n:int):
|
||
4 years ago
|
time.sleep(n)
|
||
|
|
||
4 years ago
|
def init():
|
||
|
jsonLights = loop.run_until_complete(APIrequest.get("/lights"))
|
||
4 years ago
|
|
||
4 years ago
|
global LIGHTS
|
||
|
LIGHTS = json.loads(jsonLights.text)
|
||
4 years ago
|
|
||
|
def end():
|
||
|
loop.close()
|