Skip to content

Commit

Permalink
Merge branch 'main' into better_refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeBishop authored May 16, 2024
2 parents 79743cd + f307e97 commit 23aade9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
19 changes: 14 additions & 5 deletions lib/TWCManager/EMS/TeslaPowerwall2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -252,12 +253,14 @@ 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 = [
Expand All @@ -282,16 +285,22 @@ 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"]
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)
Expand Down
15 changes: 13 additions & 2 deletions lib/TWCManager/Vehicle/TeslaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ def car_api_available(
# in 15 minutes. We'll show an error about this
# later.
vehicle.delayNextWakeAttempt = 15 * 60
else:
vehicle.delayNextWakeAttempt = 25

if state == "error":
logger.info(
Expand Down Expand Up @@ -723,7 +725,7 @@ def car_api_charge(self, charge):
if apiResponseDict["response"]["result"] == True:
self.resetCarApiLastErrorTime(vehicle)
elif charge:
reason = apiResponseDict["response"]["reason"]
reason = self.findReason(apiResponseDict)
if reason in [
"complete",
"charging",
Expand Down Expand Up @@ -782,7 +784,7 @@ def car_api_charge(self, charge):
# Stop charge failed with an error I
# haven't seen before, so wait
# carApiErrorRetryMins mins before trying again.
reason = apiResponseDict["response"]["reason"]
reason = self.findReason(apiResponseDict)
logger.info(
'ERROR "'
+ reason
Expand Down Expand Up @@ -811,6 +813,13 @@ def car_api_charge(self, charge):

return result

def findReason(self, apiResponseDict):
if "reason" in apiResponseDict["response"]:
return apiResponseDict["response"]["reason"]
elif "string" in apiResponseDict["response"]:
return apiResponseDict["response"]["string"].split(": ")[-1]
return ""

def applyChargeLimit(self, limit, checkArrival=False, checkDeparture=False):
if limit != -1 and (limit < 50 or limit > 100):
logger.log(logging.INFO8, "applyChargeLimit skipped")
Expand Down Expand Up @@ -1241,6 +1250,7 @@ def wakeVehicle(self, vehicle):
try:
req = requests.post(url, headers=headers, verify=self.verifyCert)
logger.log(logging.INFO8, "Car API cmd wake_up" + str(req))
req.raise_for_status()
apiResponseDict = json.loads(req.text)
except requests.exceptions.RequestException:
if req.status_code == 401 and "expired" in req.text:
Expand Down Expand Up @@ -1406,6 +1416,7 @@ def get_car_api(self, url, checkReady=True, provesOnline=True):
for _ in range(0, 3):
try:
req = requests.get(url, headers=headers, verify=self.verifyCert)
req.raise_for_status()
logger.log(logging.INFO8, "Car API cmd " + url + " " + str(req))
apiResponseDict = json.loads(req.text)
# This error can happen here as well:
Expand Down

0 comments on commit 23aade9

Please sign in to comment.