From edd58c7f50a5ff79b2db929bdce55afbd941f499 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 21 Oct 2020 18:23:14 +0200 Subject: [PATCH 1/3] f strings --- modules/hue/hue_controller.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/hue/hue_controller.py b/modules/hue/hue_controller.py index bbc1b4a..74a1a4a 100644 --- a/modules/hue/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -10,6 +10,8 @@ from os.path import expanduser # to get the home dir homedir = expanduser("~") # get the home directory of the current user +IP_FETCH_URL = "https://discovery.meethue.com/" # returns the HUE bridges local IP + LIGHTS = {} # dictionary of all the lights CONFIG = {} # the configuration PRESETS = {} # the presets @@ -147,7 +149,7 @@ class controller: def delay(n:int): time.sleep(n) - def init( cfgPath="{0}/.config/roomcomputer/config.json".format(homedir), presetPath="{0}/.config/roomcomputer/presets.json".format(homedir) ): + def init( cfgPath=f"{homedir}/.config/roomcomputer/config.json", presetPath=f"{homedir}/.config/roomcomputer/presets.json" ): config = readconfig(cfgPath) presets = readconfig(presetPath) @@ -156,9 +158,13 @@ class controller: global PRESETS PRESETS = presets - + global PRE_URL - PRE_URL = "http://" + CONFIG["address"] + "/api/" + CONFIG["username"] + if( "address" in CONFIG ): # check if there is an address + PRE_URL = f"http://{CONFIG['address']}/api/{CONFIG['username']}" + else: + # Fetch the address instead + jsonLights = loop.run_until_complete(APIrequest.get("/lights")) global LIGHTS From f70a1e6f121b02067eb3475812bb91addebd3e2f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 21 Oct 2020 18:34:30 +0200 Subject: [PATCH 2/3] Added ip fetching --- modules/hue/hue_controller.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/hue/hue_controller.py b/modules/hue/hue_controller.py index 74a1a4a..8026ed4 100644 --- a/modules/hue/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -23,6 +23,17 @@ def genUrl(params: str): return PRE_URL + params class APIrequest: + + async def fetchBridgeIP(): + try: + apiReq = req.get(IP_FETCH_URL) + return apiReq.json()["internalipaddress"] + + except req.exceptions.RequestException as err: + print("Unable to fetch HUE Bridge IP!") + print(err) + exit() + # Get Req async def get( dest: str="", payload: str="" ): try: @@ -160,10 +171,14 @@ class controller: PRESETS = presets global PRE_URL + PRE_URL = "https://" + if( "address" in CONFIG ): # check if there is an address - PRE_URL = f"http://{CONFIG['address']}/api/{CONFIG['username']}" - else: - # Fetch the address instead + PRE_URL += f"{CONFIG['address']}" + else: # else then fetch it + PRE_URL += APIrequest.fetchBridgeIP() + + PRE_URL += f"/api/{CONFIG['username']}" # append the rest jsonLights = loop.run_until_complete(APIrequest.get("/lights")) From af00694476d74be02493aa96013986e5e737aced Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 21 Oct 2020 18:51:53 +0200 Subject: [PATCH 3/3] Fixed bugs with HTTPS etc --- modules/hue/hue_controller.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/modules/hue/hue_controller.py b/modules/hue/hue_controller.py index 8026ed4..9138930 100644 --- a/modules/hue/hue_controller.py +++ b/modules/hue/hue_controller.py @@ -10,24 +10,25 @@ from os.path import expanduser # to get the home dir homedir = expanduser("~") # get the home directory of the current user -IP_FETCH_URL = "https://discovery.meethue.com/" # returns the HUE bridges local IP +IP_FETCH_URL = "http://discovery.meethue.com/" # returns the HUE bridges local IP LIGHTS = {} # dictionary of all the lights CONFIG = {} # the configuration PRESETS = {} # the presets -PRE_URL = "" # prefix +BRIDGE_ADDRESS = "" loop = asyncio.get_event_loop() # ASync loop def genUrl(params: str): - return PRE_URL + params + return f"http://{BRIDGE_ADDRESS}/api/{CONFIG['username']}{params}" class APIrequest: - async def fetchBridgeIP(): + def fetchBridgeIP(): try: apiReq = req.get(IP_FETCH_URL) - return apiReq.json()["internalipaddress"] + data = apiReq.json() + return data[0]["internalipaddress"] except req.exceptions.RequestException as err: print("Unable to fetch HUE Bridge IP!") @@ -170,16 +171,12 @@ class controller: global PRESETS PRESETS = presets - global PRE_URL - PRE_URL = "https://" - - if( "address" in CONFIG ): # check if there is an address - PRE_URL += f"{CONFIG['address']}" - else: # else then fetch it - PRE_URL += APIrequest.fetchBridgeIP() - - PRE_URL += f"/api/{CONFIG['username']}" # append the rest - + global BRIDGE_ADDRESS + # If there is no address in the config then get it via the API + if( "address" in CONFIG ): + BRIDGE_ADDRESS = CONFIG["address"] + else: + BRIDGE_ADDRESS = APIrequest.fetchBridgeIP() jsonLights = loop.run_until_complete(APIrequest.get("/lights")) global LIGHTS