Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EU): SPA2 control token for charge port #506

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion hyundai_kia_connect_api/KiaUvoApiEU.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime as dt
import logging
import uuid
import math
from time import sleep
from urllib.parse import parse_qs, urlparse

Expand Down Expand Up @@ -817,7 +818,7 @@ def charge_port_action(
payload = {"action": action.value}
_LOGGER.debug(f"{DOMAIN} - Charge Port Action Request: {payload}")
response = requests.post(
url, json=payload, headers=self._get_authenticated_headers(token)
url, json=payload, headers=self._get_control_headers(token)
).json()
_LOGGER.debug(f"{DOMAIN} - Charge Port Action Response: {response}")
_check_response_for_errors(response)
Expand Down Expand Up @@ -1380,6 +1381,27 @@ def _get_refresh_token(self, stamp, authorization_code):
refresh_token = token_type + " " + response["access_token"]
return token_type, refresh_token

def _get_control_token(self, token: Token) -> Token:
url = self.USER_API_URL + "pin?token="
headers = {
"Authorization": token.access_token,
"Content-type": "application/json",
"Host": self.BASE_URL,
"Accept-Encoding": "gzip",
"User-Agent": USER_AGENT_OK_HTTP,
}

data = {"deviceId": token.device_id, "pin": token.pin}
_LOGGER.debug(f"{DOMAIN} - Get Control Token Data: {data}")
response = requests.put(url, json=data, headers=headers)
response = response.json()
_LOGGER.debug(f"{DOMAIN} - Get Control Token Response {response}")
control_token = "Bearer " + response["controlToken"]
control_token_expire_at = math.floor(
cdnninja marked this conversation as resolved.
Show resolved Hide resolved
dt.datetime.now().timestamp() + response["expiresTime"]
)
return control_token, control_token_expire_at

def check_action_status(
self,
token: Token,
Expand Down