From f70a1e6f121b02067eb3475812bb91addebd3e2f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 21 Oct 2020 18:34:30 +0200 Subject: [PATCH] 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"))