From 62a2cc5236384e392f99ba876160927bca052882 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Wed, 8 May 2024 16:30:14 -0400 Subject: [PATCH 1/2] Read Tesla proxy info from CarAPI in Powerwall module --- lib/TWCManager/EMS/TeslaPowerwall2.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/TWCManager/EMS/TeslaPowerwall2.py b/lib/TWCManager/EMS/TeslaPowerwall2.py index e29eb1c3..5f3f322b 100644 --- a/lib/TWCManager/EMS/TeslaPowerwall2.py +++ b/lib/TWCManager/EMS/TeslaPowerwall2.py @@ -238,6 +238,7 @@ def getStormWatch(self): carapi = self.master.getModuleByName("TeslaAPI") token = carapi.getCarApiBearerToken() expiry = carapi.getCarApiTokenExpireTime() + baseURL = carapi.getCarApiBaseURL() now = time.time() key = "CLOUD/live_status" @@ -252,12 +253,12 @@ def getStormWatch(self): "Content-Type": "application/json", } if not self.cloudID: - url = "https://owner-api.teslamotors.com/api/1/products" + url = baseURL.replace("vehicles", "products") bodyjson = None products = list() try: - r = self.httpSession.get(url, headers=headers) + r = self.httpSession.get(url, headers=headers, verify=carapi.verifyCert) r.raise_for_status() bodyjson = r.json() products = [ @@ -282,12 +283,12 @@ def getStormWatch(self): logger.info("Couldn't find a Powerwall on your Tesla account.") if self.cloudID: - url = f"https://owner-api.teslamotors.com/api/1/energy_sites/{self.cloudID}/live_status" + url = baseURL.replace("vehicles", "energy_sites") + url = f"{url}/{self.cloudID}/live_status" bodyjson = None - result = dict() try: - r = self.httpSession.get(url, headers=headers) + r = self.httpSession.get(url, headers=headers, verify=carapi.verifyCert) r.raise_for_status() bodyjson = r.json() lastData = bodyjson["response"] From 78d11ff196a3b514dde51d9e5192939dc0023730 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Wed, 8 May 2024 16:50:47 -0400 Subject: [PATCH 2/2] Catch missing scope --- lib/TWCManager/EMS/TeslaPowerwall2.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/TWCManager/EMS/TeslaPowerwall2.py b/lib/TWCManager/EMS/TeslaPowerwall2.py index 5f3f322b..dff89e63 100644 --- a/lib/TWCManager/EMS/TeslaPowerwall2.py +++ b/lib/TWCManager/EMS/TeslaPowerwall2.py @@ -293,6 +293,8 @@ def getStormWatch(self): bodyjson = r.json() lastData = bodyjson["response"] except: + if r.status_code is 403: + logger.warn("Error fetching Powerwall cloud data; does your API token have energy_device_data scope?") pass self.lastFetch[key] = (now, lastData)