From c30e465ff75bfa3ce1c21ed59b024c41417cd20b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 4 Aug 2020 14:45:56 +0200 Subject: [PATCH] Began work on presets --- hue_controller.py | 30 +++++++++++++++++++++++++----- presets.py | 7 +++++++ 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 presets.py diff --git a/hue_controller.py b/hue_controller.py index 6edec8c..788e7ce 100644 --- a/hue_controller.py +++ b/hue_controller.py @@ -6,6 +6,7 @@ import time from lib.func import * # useful functions import config # Configuration for the controller (/config.py <- change this file) +from presets import * # presets for the lights LIGHTS = {} # dictionary of all the lights @@ -82,20 +83,40 @@ class controller: for key in LIGHTS: controller.switchLight(key) - # Light color - def setLightColor(index:int, r:int, g:int, b:int): + # Light control + 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): + def setLightBrightness( index:int, b:int ): + if( LIGHTS.get(str(index)) ): + payload = '{"bri":' + str(b) + '}' + APIrequest.put( "/lights/" + str(index) + "/state", payload ) + 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) + controller.setLightColor( key, r, g, b ) def Power(isOn: bool=True): # Controlling the power of the lights loop.run_until_complete( controller.toggleLights(isOn) ) + # Presets + def setPreset(index:int, p:str): + if( LIGHTS.get(str(index)) ): + if( PRESETS.get(p) ): + preset = PRESETS[p] + r, g, b = preset["color"] + controller.setLightColor( index, r, g, b ) + else: + print("Error: Unknown preset '" + p + "'") + else: + print("Error: Light index '" + str(index) + "' out of range") + # Controller "system" functions def delay(n: int): time.sleep(n) @@ -110,7 +131,6 @@ class controller: def end(): loop.close() - def testReq(): controller.init() controller.Power(True) diff --git a/presets.py b/presets.py new file mode 100644 index 0000000..17cb7a2 --- /dev/null +++ b/presets.py @@ -0,0 +1,7 @@ +# Presets goes in here +PRESETS = { + "default": { + "color": (178, 199, 255), + "brightness": 255 + } +}