From f2f74e11a45d8fad60824fa6f024505c1e844774 Mon Sep 17 00:00:00 2001 From: Philipp Kainz Date: Tue, 14 May 2024 07:49:56 +0200 Subject: [PATCH 1/6] trap failing api in fetch_soc --- packages/modules/vehicles/polestar/api.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index 47d48935c6..841b95a692 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -92,7 +92,11 @@ def check_vin(self) -> None: def fetch_soc(user_id: str, password: str, vin: str, vehicle: int) -> CarState: api = PolestarApi(user_id, password, vin) bat_data = api.get_battery_data() - soc = bat_data['batteryChargeLevelPercentage'] - est_range = bat_data['estimatedDistanceToEmptyKm'] + # preset values will be returned if api fails + soc = -1 + est_range = -1 + if bat_data is not None: + soc = bat_data['batteryChargeLevelPercentage'] + est_range = bat_data['estimatedDistanceToEmptyKm'] return CarState(soc, est_range, time.strftime("%m/%d/%Y, %H:%M:%S")) From dab70b8aa520e88fa1b0e405f3e967fb01efc257 Mon Sep 17 00:00:00 2001 From: PK Date: Wed, 15 May 2024 11:42:31 +0200 Subject: [PATCH 2/6] req.get_http_session implemented --- packages/modules/vehicles/polestar/api.py | 31 ++++++++++------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index 841b95a692..098d47daea 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -1,5 +1,5 @@ import logging -import requests +from modules.common import req import time from modules.vehicles.polestar.auth import PolestarAuth from modules.common.component_state import CarState @@ -13,7 +13,7 @@ class PolestarApi: def __init__(self, username: str, password: str, vin: str) -> None: self.auth = PolestarAuth(username, password, vin) self.vin = vin - self.client_session = requests.session() + self.client_session = req.get_http_session() def query_params(self, params: dict, url='https://pc-api.polestar.com/eu-north-1/mystar-v2/') -> dict or None: access_token = self.auth.get_auth_token() @@ -29,15 +29,13 @@ def query_params(self, params: dict, url='https://pc-api.polestar.com/eu-north-1 log.info("query_params:%s", params['query']) try: result = self.client_session.get(url=url, params=params, headers=headers) - except requests.RequestException as e: - log.error("query_params:http error:%s", e) - return None - - if result.status_code == 401: - self.auth.delete_token() - raise Exception("query_params error: get response %d: unauthorized Exception", result.status_code) - if result.status_code != 200: - raise Exception("query_params error: get response %d: %s", result.status_code, result.text) + except Exception as e: + if result.status_code == 401: + self.auth.delete_token() + if self.auth.access_token is not None: + # if we got an access code but the query failed, VIN could be wrong, so let`s check it + self.check_vin() + raise e result_data = result.json() if result_data.get('errors'): @@ -60,9 +58,7 @@ def get_battery_data(self) -> dict or None: if result is not None and result['data'] is not None and result['data']['getBatteryData'] is not None \ and result['data']['getBatteryData']['batteryChargeLevelPercentage'] is not None: return result['data']['getBatteryData'] - elif self.auth.access_token is not None: - # if we got an access code but the query failed, VIN could be wrong, so let`s check it - self.check_vin() + else: return None def check_vin(self) -> None: @@ -78,8 +74,7 @@ def check_vin(self) -> None: # get list of cars and store the ones not matching our vin cars = result['data']['getConsumerCarsV2'] if len(cars) == 0: - raise Exception("Es konnten keine Fahrzeuge im Account gefunden werden. Bitte in den Einstellungen " + - "prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") + raise Exception("Es konnten keine Fahrzeuge im Account gefunden werden. Bitte in den Einstellungen prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") for i in range(0, len(cars)): if cars[i]['vin'] == self.vin: pass @@ -93,8 +88,8 @@ def fetch_soc(user_id: str, password: str, vin: str, vehicle: int) -> CarState: api = PolestarApi(user_id, password, vin) bat_data = api.get_battery_data() # preset values will be returned if api fails - soc = -1 - est_range = -1 + soc = 0 + est_range = 0 if bat_data is not None: soc = bat_data['batteryChargeLevelPercentage'] est_range = bat_data['estimatedDistanceToEmptyKm'] From a365823cddd586976a27342b90ddf09c3bdef1f7 Mon Sep 17 00:00:00 2001 From: PK Date: Wed, 15 May 2024 11:53:14 +0200 Subject: [PATCH 3/6] long line trncated --- packages/modules/vehicles/polestar/api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index 098d47daea..7eab5cc4e1 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -74,7 +74,9 @@ def check_vin(self) -> None: # get list of cars and store the ones not matching our vin cars = result['data']['getConsumerCarsV2'] if len(cars) == 0: - raise Exception("Es konnten keine Fahrzeuge im Account gefunden werden. Bitte in den Einstellungen prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") + raise Exception("Es konnten keine Fahrzeuge im Account gefunden werden. Bitte in den Einstellungen " + + "prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") + for i in range(0, len(cars)): if cars[i]['vin'] == self.vin: pass From fac31ab5d21db9e57ac813558dc9c57da6ed3082 Mon Sep 17 00:00:00 2001 From: PK Date: Wed, 15 May 2024 11:58:25 +0200 Subject: [PATCH 4/6] indent fixed --- packages/modules/vehicles/polestar/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index 7eab5cc4e1..afd77786e4 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -75,7 +75,7 @@ def check_vin(self) -> None: cars = result['data']['getConsumerCarsV2'] if len(cars) == 0: raise Exception("Es konnten keine Fahrzeuge im Account gefunden werden. Bitte in den Einstellungen " + - "prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") + "prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") for i in range(0, len(cars)): if cars[i]['vin'] == self.vin: From 7757541b2bce3be00e13d56b144da3beca414144 Mon Sep 17 00:00:00 2001 From: PK Date: Wed, 22 May 2024 16:54:48 +0200 Subject: [PATCH 5/6] polestar api:cleaned and exceptions added --- packages/modules/vehicles/polestar/api.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index afd77786e4..fd68bb9e2c 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -18,8 +18,7 @@ def __init__(self, username: str, password: str, vin: str) -> None: def query_params(self, params: dict, url='https://pc-api.polestar.com/eu-north-1/mystar-v2/') -> dict or None: access_token = self.auth.get_auth_token() if access_token is None: - log.error("query_params:not yet authenticated") - return None + raise Exception("query_params error:could not get auth token") headers = { "Content-Type": "application/json", @@ -42,7 +41,6 @@ def query_params(self, params: dict, url='https://pc-api.polestar.com/eu-north-1 error_message = result_data['errors'][0]['message'] raise Exception("query_params error: %s", error_message) - log.debug(result_data) return result_data def get_battery_data(self) -> dict or None: @@ -55,11 +53,8 @@ def get_battery_data(self) -> dict or None: result = self.query_params(params) - if result is not None and result['data'] is not None and result['data']['getBatteryData'] is not None \ - and result['data']['getBatteryData']['batteryChargeLevelPercentage'] is not None: - return result['data']['getBatteryData'] - else: - return None + return result['data']['getBatteryData'] + def check_vin(self) -> None: # get Vehicle Data @@ -89,11 +84,7 @@ def check_vin(self) -> None: def fetch_soc(user_id: str, password: str, vin: str, vehicle: int) -> CarState: api = PolestarApi(user_id, password, vin) bat_data = api.get_battery_data() - # preset values will be returned if api fails - soc = 0 - est_range = 0 - if bat_data is not None: - soc = bat_data['batteryChargeLevelPercentage'] - est_range = bat_data['estimatedDistanceToEmptyKm'] + soc = bat_data['batteryChargeLevelPercentage'] + est_range = bat_data['estimatedDistanceToEmptyKm'] return CarState(soc, est_range, time.strftime("%m/%d/%Y, %H:%M:%S")) From d3476c72b0b127f871dc23057e3cf13ac24afcdc Mon Sep 17 00:00:00 2001 From: PK Date: Wed, 22 May 2024 16:59:50 +0200 Subject: [PATCH 6/6] blank line removed --- packages/modules/vehicles/polestar/api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index fd68bb9e2c..464c701e03 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -55,7 +55,6 @@ def get_battery_data(self) -> dict or None: return result['data']['getBatteryData'] - def check_vin(self) -> None: # get Vehicle Data params = {