Hyundai-Kia-Connect / kia_uvo

A Home Assistant HACS integration that supports Kia Connect(Uvo) and Hyundai Bluelink. The integration supports the EU, Canada and the USA.
MIT License
427 stars 85 forks source link

Token expires causes exception #202

Closed Personear closed 2 years ago

Personear commented 2 years ago

Description: When the BlueLink token expires, the below error occurs. To fix, I have to reload the integration. It looks like the code doesn't expect an expired token.

Reproduce: Not sure how to expire a token. Maybe it's based on time or over utilizing the quota? Once a token is expired, request an update from cache (not forced) and you'll get the below error.

Region and Brand: USA - Hyundai Elantra 2021 SEL

2021-12-09 11:08:21 DEBUG (SyncWorker_5) [custom_components.kia_uvo.HyundaiBlueLinkAPIUSA] kia_uvo - get_cached_vehicle_status response {'errorSubCode': 'C401_2', 'systemName': 'IDM', 'functionName': 'remoteVehicleStatus', 'errorSubMessage': 'Token has expired', 'errorMessage': 'Token has expired', 'errorCode': 401, 'serviceName': 'CheckToken'}
2021-12-09 11:08:21 ERROR (MainThread) [custom_components.kia_uvo.Vehicle] kia_uvo - Exception in update : 'vehicleStatus' - traceback: Traceback (most recent call last):
  File "/config/custom_components/kia_uvo/Vehicle.py", line 56, in update
    self.vehicle_data = await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/kia_uvo/HyundaiBlueLinkAPIUSA.py", line 146, in get_cached_vehicle_status
    vehicle_status["vehicleStatus"] = response["vehicleStatus"]
KeyError: 'vehicleStatus'
cdnninja commented 2 years ago

What version are you using?

Personear commented 2 years ago

v1.2.2

cdnninja commented 2 years ago

odd. Could we get more logs, in particular looking to see if the token is getting refreshed by the system. It should see what time it expires and renew it before using it.

Does this happen each time or does it only happen when you log into the Hyundai app? I think the Kia USA API expires all tokens whenever the app is used. If that is the case we can do something to handle this better.

Personear commented 2 years ago

Here's some more logs. I redacted some personal info (email, access tokens, refresh tokens). It does indeed show when the token should expire and does refresh the token if needed. Maybe the token can be expired if too many requests are used at once? It does not happen if I log in to the Hyundai app. I can't actually seem to replicate it now.

I'm running v1.2.3 now.


2021-12-12 22:30:38 DEBUG (MainThread) [custom_components.kia_uvo.Vehicle] kia_uvo - Received token into Vehicle Object {'valid_until': '2021-12-12 11:45:42.333384', 'access_token': 'REDACTED', 'refresh_token': 'REDACTED', 'device_id': None, 'vehicle_name': '2021 ELANTRA', 'vehicle_id': 'REDACTED', 'vehicle_regid': 'REDACTED', 'vehicle_model': 'ELANTRA', 'vehicle_registration_date': '20210211', 'stamp': 'NoStamp'}
2021-12-12 22:30:38 DEBUG (MainThread) [custom_components.kia_uvo.Vehicle] kia_uvo - Refresh token started 2021-12-12 11:45:42.333384 2021-12-12 22:30:38.380702 True
2021-12-12 22:30:38 DEBUG (MainThread) [custom_components.kia_uvo.Vehicle] kia_uvo - Refresh token expired
2021-12-12 22:30:40 DEBUG (SyncWorker_5) [custom_components.kia_uvo.HyundaiBlueLinkAPIUSA] kia_uvo - Sign In Response {"access_token":"REDACTED","refresh_token":"REDACTED","expires_in":"1799","username":"REDACTED"}
2021-12-12 22:30:40 DEBUG (SyncWorker_5) [custom_components.kia_uvo.HyundaiBlueLinkAPIUSA] kia_uvo - Access Token Value REDACTED
2021-12-12 22:30:40 DEBUG (SyncWorker_5) [custom_components.kia_uvo.HyundaiBlueLinkAPIUSA] kia_uvo - Refresh Token Value REDACTED
cdnninja commented 2 years ago

If this has not happened again maybe we close this?

Personear commented 2 years ago

Sure. I'll close it.