Light on/off

fetchIP
E. Almqvist 4 years ago
parent aa69abaf52
commit 6a42563bc5
  1. 26
      hue_controller.py

@ -1,6 +1,7 @@
import requests as req # Used for HTTP requests for the Hue API import requests as req # Used for HTTP requests for the Hue API
import json # API uses JSON import json # API uses JSON
import asyncio # ASync stuff import asyncio # ASync stuff
import config # Configuration for the controller (/config.py <- change this file) import config # Configuration for the controller (/config.py <- change this file)
LIGHTS = {} # dictionary of all the lights LIGHTS = {} # dictionary of all the lights
@ -57,32 +58,51 @@ class APIrequest:
class controller: class controller:
# Info
async def getLights(): async def getLights():
return await APIrequest.get("/lights") return await APIrequest.get("/lights")
async def getLight(index: int=1): async def getLight(index: int=1):
return await APIrequest.get( "/lights/" + str(index) ) return await APIrequest.get( "/lights/" + str(index) )
# Lower level light manipulation
async def toggleLight(index: int=1, isOn: bool=True): async def toggleLight(index: int=1, isOn: bool=True):
await APIrequest.put( "/lights/" + str(index) + "/state", '{"on":' + boolToString(isOn) + '}' ) await APIrequest.put( "/lights/" + str(index) + "/state", '{"on":' + boolToString(isOn) + '}' )
async def toggleLights(isOn: bool=True):
for key in LIGHTS:
await controller.toggleLight(key, isOn)
# Turning lights on/off
def switchLight( index: int=1 ): def switchLight( index: int=1 ):
key = LIGHTS.get(str(index)) key = LIGHTS.get(str(index))
if(key): if(key):
if( key.get(state) ): if( key.get("state") ):
loop.run_until_complete( controller.toggleLight( index, boolToString(StrBool[LIGHTS[str(index)]["state"]["on"]]) ) ) curPower = LIGHTS[str(index)]["state"]["on"]
loop.run_until_complete( controller.toggleLight(index, not curPower))
else: else:
print("Error: Light index out of range") print("Error: Light index out of range")
def switchLights():
for key in LIGHTS:
controller.switchLight(key)
def Power(isOn: bool=True):
loop.run_until_complete( controller.toggleLights(isOn) )
# Very important init function
def init(): def init():
jsonLights = loop.run_until_complete(APIrequest.get("/lights")) jsonLights = loop.run_until_complete(APIrequest.get("/lights"))
global LIGHTS global LIGHTS
LIGHTS = json.loads(jsonLights.text) LIGHTS = json.loads(jsonLights.text)
print(LIGHTS) print(LIGHTS)
print("----------------")
def testReq(): def testReq():
controller.init() controller.init()
controller.switchLight( 1 ) controller.Power(False) # turn on all lights
#controller.switchLights()
# loop.run_until_complete( controller.toggleLight(1, True) ) # try to turn on/off a light # loop.run_until_complete( controller.toggleLight(1, True) ) # try to turn on/off a light
loop.close() loop.close()

Loading…
Cancel
Save