From e45191d58251b892bbd3f01dab2c41704631571b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 4 Aug 2020 13:20:27 +0200 Subject: [PATCH] Added light color --- hue_controller.py | 63 +++++++++++++++++++++++++++-------------------- lib/func.py | 23 +++++++++++++++++ 2 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 lib/func.py diff --git a/hue_controller.py b/hue_controller.py index 1bd53ca..a9361ac 100644 --- a/hue_controller.py +++ b/hue_controller.py @@ -1,30 +1,19 @@ import requests as req # Used for HTTP requests for the Hue API import json # API uses JSON import asyncio # ASync stuff +import time + +from lib.func import * # useful functions import config # Configuration for the controller (/config.py <- change this file) LIGHTS = {} # dictionary of all the lights - loop = asyncio.get_event_loop() # ASync loop def genUrl(params: str): return "http://" + config.address + "/api/" + config.username + params -boolStr = { - True: "true", - False: "false" -} - -StrBool = { # because doing something else is too expensive - "true": True, - "false": False -} - -def boolToString(v: bool): # To fix the dumb python syntax - return boolStr[v] - class APIrequest: # Get Req async def get( dest: str="", payload: str="" ): @@ -58,14 +47,14 @@ class APIrequest: class controller: - # Info + # 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 + # Lower level light manipulation (async) async def toggleLight(index: int=1, isOn: bool=True): await APIrequest.put( "/lights/" + str(index) + "/state", '{"on":' + boolToString(isOn) + '}' ) @@ -73,7 +62,13 @@ class controller: for key in LIGHTS: await controller.toggleLight(key, isOn) - # Turning lights on/off + 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): @@ -81,28 +76,42 @@ class controller: curPower = LIGHTS[str(index)]["state"]["on"] loop.run_until_complete( controller.toggleLight(index, not curPower)) else: - print("Error: Light index out of range") + print("Error: Light index '" + str(index) + "' out of range") def switchLights(): for key in LIGHTS: controller.switchLight(key) - def Power(isOn: bool=True): + 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 loop.run_until_complete( controller.toggleLights(isOn) ) - # Very important init function + # 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) - print(LIGHTS) - print("----------------") + + def end(): + loop.close() + def testReq(): controller.init() - controller.Power(False) # turn on all lights - - #controller.switchLights() - # loop.run_until_complete( controller.toggleLight(1, True) ) # try to turn on/off a light + controller.Power(True) + controller.setAllLightsColor( 255, 255, 255 ) - loop.close() + controller.end() diff --git a/lib/func.py b/lib/func.py new file mode 100644 index 0000000..8f3ae51 --- /dev/null +++ b/lib/func.py @@ -0,0 +1,23 @@ +import colorsys + +boolStr = { + True: "true", + False: "false" +} + +def boolToString(v: bool): # To fix the dumb python syntax + return boolStr[v] + +def rgbToDecimal( r:int, g:int, b:int ): + return round(r/255), round(g/255), round(b/255) + +def svNumFix(n: float): + return int(round(n*254, 0)) + +def hueNumFix(n: float): + return int(round(n*65535)) + +def rgbToHsv( r:int, g:int, b:int ): + R, G, B = rgbToDecimal(r, g, b) + H, S, V = colorsys.rgb_to_hsv(R, G, B) + return hueNumFix(H), svNumFix(S), svNumFix(V)