Skip to content

Commit

Permalink
Merge pull request #585 from ngardiner/reason_location
Browse files Browse the repository at this point in the history
Reason code seems to have moved
  • Loading branch information
MikeBishop authored May 16, 2024
2 parents e5b76d8 + 4b0b972 commit f307e97
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/TWCManager/Vehicle/TeslaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,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 @@ -722,7 +724,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 @@ -781,7 +783,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 @@ -810,6 +812,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 @@ -1228,8 +1237,16 @@ 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:
# If the token is expired, refresh it and try again
self.apiRefresh()
return self.wakeVehicle(vehicle)
elif req.status_code == 429:
# We're explicitly being told to back off
self.errorCount = max(30, self.errorCount)
return False
except json.decoder.JSONDecodeError:
return False
Expand Down Expand Up @@ -1386,14 +1403,22 @@ 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:
# {'response': {'reason': 'could_not_wake_buses', 'result': False}}
# This one is somewhat common:
# {'response': None, 'error': 'vehicle unavailable: {:error=>"vehicle unavailable:"}', 'error_description': ''}
except requests.exceptions.RequestException:
pass
if req.status_code == 401 and "expired" in req.text:
# If the token is expired, refresh it and try again
self.apiRefresh()
continue
elif req.status_code == 429:
# We're explicitly being told to back off
self.errorCount = max(30, self.errorCount)
return False, None
except json.decoder.JSONDecodeError:
pass

Expand Down

0 comments on commit f307e97

Please sign in to comment.