@ -20,167 +20,171 @@ BRIDGE_ADDRESS = ""
loop = asyncio . get_event_loop ( ) # ASync loop
loop = asyncio . get_event_loop ( ) # ASync loop
def genUrl ( params : str ) :
def genUrl ( params : str ) :
return f " http:// { BRIDGE_ADDRESS } /api/ { CONFIG [ ' username ' ] } { params } "
return f " http:// { BRIDGE_ADDRESS } /api/ { CONFIG [ ' username ' ] } { params } "
class APIrequest :
class APIrequest :
def fetchBridgeIP ( ) :
def fetchBridgeIP ( ) :
try :
try :
apiReq = req . get ( IP_FETCH_URL )
apiReq = req . get ( IP_FETCH_URL )
data = apiReq . json ( )
data = apiReq . json ( )
return data [ 0 ] [ " internalipaddress " ]
return data [ 0 ] [ " internalipaddress " ]
except req . exceptions . RequestException as err :
except req . exceptions . RequestException as err :
print ( " Unable to fetch HUE Bridge IP! " )
print ( " Unable to fetch HUE Bridge IP! " )
print ( err )
print ( err )
exit ( )
exit ( )
# Get Req
# Get Req
async def get ( dest : str = " " , payload : str = " " ) :
async def get ( dest : str = " " , payload : str = " " ) :
try :
try :
apiReq = req . get ( genUrl ( dest ) , data = payload )
apiReq = req . get ( genUrl ( dest ) , data = payload )
if ( apiReq . status_code != 200 ) : # print out the error if the status code is not 200
if ( apiReq . status_code != 200 ) : # print out the error if the status code is not 200
print ( apiReq )
print ( apiReq )
print ( apiReq . text )
print ( apiReq . text )
return apiReq
return apiReq
except req . exceptions . RequestException as err :
except req . exceptions . RequestException as err :
print ( err )
print ( err )
# PUT Req
# PUT Req
async def put ( dest : str = " " , payload : str = " " ) :
async def put ( dest : str = " " , payload : str = " " ) :
try :
try :
apiReq = req . put ( genUrl ( dest ) , data = payload ) # send the payload
apiReq = req . put ( genUrl ( dest ) , data = payload ) # send the payload
if ( apiReq . status_code != 200 ) :
if ( apiReq . status_code != 200 ) :
print ( apiReq )
print ( apiReq )
print ( apiReq . text )
print ( apiReq . text )
return apiReq
return apiReq
except req . exceptions . RequestException as err :
except req . exceptions . RequestException as err :
print ( err )
print ( err )
class controller :
class controller :
# Internal get functions
# Internal get functions
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)
# Lower level light manipulation (async)
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 ) :
async def toggleLights ( isOn : bool = True ) :
for key in LIGHTS :
for key in LIGHTS :
await controller . toggleLight ( key , isOn )
await controller . toggleLight ( key , isOn )
async def setLightRGB ( index : int , r : int , g : int , b : int ) :
async def setLightRGB ( index : int , r : int , g : int , b : int ) :
h , s , v = rgbToHsv ( r , g , b )
h , s , v = rgbToHsv ( r , g , b )
payload = ' { " sat " : ' + str ( s ) + ' , " bri " : ' + str ( v ) + ' , " hue " : ' + str ( h ) + ' } '
payload = ' { " sat " : ' + str ( s ) + ' , " bri " : ' + str ( v ) + ' , " hue " : ' + str ( h ) + ' } '
await APIrequest . put ( " /lights/ " + str ( index ) + " /state " , payload )
await APIrequest . put ( " /lights/ " + str ( index ) + " /state " , payload )
# Normal functions
# Normal functions
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 " ) ) :
curPower = LIGHTS [ str ( index ) ] [ " state " ] [ " on " ]
curPower = LIGHTS [ str ( index ) ] [ " state " ] [ " on " ]
loop . run_until_complete ( controller . toggleLight ( index , not curPower ) )
loop . run_until_complete ( controller . toggleLight ( index , not curPower ) )
else :
else :
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
def switchLights ( ) :
def switchLights ( ) :
for key in LIGHTS :
for key in LIGHTS :
controller . switchLight ( key )
controller . switchLight ( key )
# Light control
# Light control
def setLightColor ( index : int , r : int , g : int , b : int ) :
def setLightColor ( index : int , r : int , g : int , b : int ) :
if ( LIGHTS . get ( str ( index ) ) ) :
if ( LIGHTS . get ( str ( index ) ) ) :
loop . run_until_complete ( controller . setLightRGB ( index , r , g , b ) )
loop . run_until_complete ( controller . setLightRGB ( index , r , g , b ) )
else :
else :
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
def setLightBrightness ( index : int , b : int ) :
def setLightBrightness ( index : int , b : int ) :
if ( LIGHTS . get ( str ( index ) ) ) :
if ( LIGHTS . get ( str ( index ) ) ) :
payload = ' { " bri " : ' + str ( b ) + ' } '
payload = ' { " bri " : ' + str ( b ) + ' } '
loop . run_until_complete ( APIrequest . put ( " /lights/ " + str ( index ) + " /state " , payload ) )
loop . run_until_complete ( APIrequest . put ( " /lights/ " + str ( index ) + " /state " , payload ) )
else :
else :
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
def setBrightness ( b : int ) :
def setBrightness ( b : int ) :
for key in LIGHTS :
for key in LIGHTS :
controller . setLightBrightness ( key , b )
controller . setLightBrightness ( key , b )
def setAllLightsColor ( r : int , g : int , b : int ) :
def setAllLightsColor ( r : int , g : int , b : int ) :
for key in LIGHTS :
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
def Power ( isOn : bool = True ) : # Controlling the power of the lights
loop . run_until_complete ( controller . toggleLights ( isOn ) )
loop . run_until_complete ( controller . toggleLights ( isOn ) )
def powerLight ( index : int , isOn : bool = True ) :
def powerLight ( index : int , isOn : bool = True ) :
loop . run_until_complete ( controller . toggleLight ( index , isOn ) )
loop . run_until_complete ( controller . toggleLight ( index , isOn ) )
# Presets
# Presets
def setLightPreset ( index : int , p : str ) :
def setLightPreset ( index : int , p : str ) :
if ( LIGHTS . get ( str ( index ) ) ) :
if ( LIGHTS . get ( str ( index ) ) ) :
if ( PRESETS . get ( p ) ) :
if ( PRESETS . get ( p ) ) :
preset = PRESETS [ p ]
preset = PRESETS [ p ]
r , g , b = preset [ " color " ]
r , g , b = preset [ " color " ]
brightness = preset [ " brightness " ]
brightness = preset [ " brightness " ]
controller . setLightColor ( index , r , g , b )
controller . setLightColor ( index , r , g , b )
controller . setLightBrightness ( index , brightness )
controller . setLightBrightness ( index , brightness )
else :
else :
print ( " Error: Unknown preset ' " + p + " ' " )
print ( " Error: Unknown preset ' " + p + " ' " )
else :
else :
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
print ( " Error: Light index ' " + str ( index ) + " ' out of range " )
def setPreset ( presetID : str , index : int = - 1 ) :
def setPreset ( presetID : str , index : int = - 1 ) :
if ( PRESETS . get ( presetID ) ) :
if ( PRESETS . get ( presetID ) ) :
if ( index == - 1 ) :
if ( index == - 1 ) :
for key in LIGHTS :
for key in LIGHTS :
controller . setLightPreset ( key , presetID )
controller . setLightPreset ( key , presetID )
else :
else :
controller . setLightPreset ( index , presetID )
controller . setLightPreset ( index , presetID )
else :
else :
print ( " Error: Unknown preset ' " + presetID + " ' " )
print ( " Error: Unknown preset ' " + presetID + " ' " )
def countLights ( ) :
def countLights ( ) :
return len ( LIGHTS )
return len ( LIGHTS )
# Controller "system" functions
# Controller "system" functions
def delay ( n : int ) :
def delay ( n : int ) :
time . sleep ( n )
time . sleep ( n )
def init ( cfgPath = f " { homedir } /.config/roomcomputer/config.json " , presetPath = f " { homedir } /.config/roomcomputer/presets.json " ) :
def init ( cfgPath = f " { homedir } /.config/roomcomputer/config.json " , presetPath = f " { homedir } /.config/roomcomputer/presets.json " ) :
config = readconfig ( cfgPath )
config = readconfig ( cfgPath )
presets = readconfig ( presetPath )
presets = readconfig ( presetPath )
global CONFIG
global CONFIG
CONFIG = config [ " hue " ]
CONFIG = config [ " hue " ]
global PRESETS
global PRESETS
PRESETS = presets
PRESETS = presets
global BRIDGE_ADDRESS
global BRIDGE_ADDRESS
# If there is no address in the config then get it via the API
# If there is no address in the config then get it via the API
if ( " address " in CONFIG ) :
if ( " address " in CONFIG ) :
BRIDGE_ADDRESS = CONFIG [ " address " ]
BRIDGE_ADDRESS = CONFIG [ " address " ]
else :
else :
BRIDGE_ADDRESS = APIrequest . fetchBridgeIP ( )
BRIDGE_ADDRESS = APIrequest . fetchBridgeIP ( )
jsonLights = loop . run_until_complete ( APIrequest . get ( " /lights " ) )
try :
global LIGHTS
jsonLights = loop . run_until_complete ( APIrequest . get ( " /lights " ) )
LIGHTS = json . loads ( jsonLights . text )
global LIGHTS
LIGHTS = json . loads ( jsonLights . text )
def end ( ) :
except Exception as err :
loop . close ( )
print ( " \033 [91mUnable to fetch lights. This could be because your configuration file is incomplete! Please check your config at \" ~/.config/roomcomputer/config.json \" . \033 [0m " )
raise err
def end ( ) :
loop . close ( )