From 8296532eecda2935d1417ae4af4e6cc8ef70310b Mon Sep 17 00:00:00 2001 From: Tuen Lee Date: Wed, 3 Jan 2024 17:14:33 +0100 Subject: [PATCH 1/2] tmp commit catch error --- custom_components/polestar_api/polestar.py | 1 + .../polestar_api/pypolestar/auth.py | 18 ++++++++++++------ custom_components/polestar_api/sensor.py | 13 ++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index c999e68..962d520 100644 --- a/custom_components/polestar_api/polestar.py +++ b/custom_components/polestar_api/polestar.py @@ -7,6 +7,7 @@ from homeassistant.core import HomeAssistant from homeassistant.util.unit_system import METRIC_SYSTEM, UnitSystem + from .pypolestar.exception import PolestarApiException, PolestarAuthException from .pypolestar.polestar import PolestarApi diff --git a/custom_components/polestar_api/pypolestar/auth.py b/custom_components/polestar_api/pypolestar/auth.py index a6bdf6f..2abaeb9 100644 --- a/custom_components/polestar_api/pypolestar/auth.py +++ b/custom_components/polestar_api/pypolestar/auth.py @@ -14,6 +14,7 @@ class PolestarAuth: """base class for Polestar authentication.""" def __init__(self, username: str, password: str) -> None: + """Initialize the Polestar authentication.""" self.username = username self.password = password self.access_token = None @@ -23,7 +24,7 @@ def __init__(self, username: str, password: str) -> None: self._client_session = httpx.AsyncClient() async def get_token(self, refresh=False) -> None: - # get access / refresh token + """Get the token from Polestar.""" headers = {"Content-Type": "application/json"} operationName = "getAuthToken" if not refresh: @@ -51,6 +52,7 @@ async def get_token(self, refresh=False) -> None: self.latest_call_code = result.status_code resultData = result.json() if result.status_code != 200 or ("errors" in resultData and len(resultData["errors"])): + _LOGGER.error(result) raise PolestarAuthException("Error getting token", result.status_code) _LOGGER.debug(resultData) @@ -68,7 +70,7 @@ async def _get_code(self) -> None: # check if code is in query_params if query_params.get('code'): - return query_params.get('code')[0] + return query_params.get('code') # get the resumePath if query_params.get('resumePath'): @@ -102,6 +104,7 @@ async def _get_code(self) -> None: self.latest_call_code = result.status_code if result.status_code != 200: + _LOGGER.error(result) raise PolestarAuthException("Error getting code callback", result.status_code) # url encode the code @@ -111,13 +114,16 @@ async def _get_code(self) -> None: return code async def _get_resume_path(self): - # Get Resume Path + """Get Resume Path from Polestar.""" params = { "response_type": "code", "client_id": "polmystar", "redirect_uri": "https://www.polestar.com/sign-in-callback" } result = await self._client_session.get("https://polestarid.eu.polestar.com/as/authorization.oauth2", params=params, timeout=HTTPX_TIMEOUT) - if result.status_code != 303: - raise PolestarAuthException("Error getting resume path ", result.status_code) - return result.next_request.url.params + if result.status_code in (303, 302): + return result.next_request.url.params + + _LOGGER.error(result.text) + raise PolestarAuthException("Error getting resume path ", result.status_code) + diff --git a/custom_components/polestar_api/sensor.py b/custom_components/polestar_api/sensor.py index ace4f67..4d02b39 100644 --- a/custom_components/polestar_api/sensor.py +++ b/custom_components/polestar_api/sensor.py @@ -548,13 +548,6 @@ def state(self) -> StateType: return estimate_range - - - if self.entity_description.key in ('current_odometer_meters'): - if int(self._attr_native_value) > 1000: - km = self._attr_native_value / 1000 - self._attr_native_value = int(km) - # round the value if self.entity_description.round_digits is not None: # if the value is integer, remove the decimal @@ -562,6 +555,12 @@ def state(self) -> StateType: return int(self._attr_native_value) return round(float(self._attr_native_value), self.entity_description.round_digits) + if self.entity_description.key in ('current_odometer_meters'): + _LOGGER.info("current_odometer_meters %s", self._attr_native_value) + if int(self._attr_native_value) > 1000: + return self._attr_native_value / 1000 + + return self._attr_native_value @property From 254d8b3e759cfe244299fa0da83253b9f0837d33 Mon Sep 17 00:00:00 2001 From: Vincent Gibara Date: Wed, 3 Jan 2024 11:55:17 -0500 Subject: [PATCH 2/2] French translation updated --- .../polestar_api/translations/fr.json | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/custom_components/polestar_api/translations/fr.json b/custom_components/polestar_api/translations/fr.json index 711edc0..478448e 100644 --- a/custom_components/polestar_api/translations/fr.json +++ b/custom_components/polestar_api/translations/fr.json @@ -21,17 +21,11 @@ }, "entity": { "sensor": { - "polestar_estimate_distance_to_empty_miles": { - "name": "Distance restante en miles" - }, - "polestar_estimate_distance_to_empty_km": { - "name": "Distance restante en km" - }, "polestar_current_odometer_meters": { "name": "Odomètre" }, - "polestar_average_speed_km_per_hour": { - "name": "Vitesse moyenne en km/h" + "polestar_average_speed_per_hour": { + "name": "Vitesse moyenne" }, "polestar_current_trip_meter_automatic": { "name": "Compteur kilométrique automatique" @@ -49,16 +43,16 @@ "name": "État de la recharge" }, "polestar_charging_power_watts": { - "name": "Puissance de recharge (watts)" + "name": "Puissance de recharge" }, "polestar_charging_current_amps": { - "name": "Puissance de recharge (Amps)" + "name": "Puissance de recharge" }, "polestar_charger_connection_status": { "name": "État de connexion du chargeur" }, - "polestar_average_energy_consumption_kwh_per_100_km": { - "name": "Consommation d'énergie moyenne en kWh/100km" + "polestar_average_energy_consumption_kwh_per_100": { + "name": "Consommation d'énergie moyenne" }, "polestar_estimated_charging_time_minutes_to_target_distance": { "name": "Temps de recharge restant pour la distance jusqu'à destination" @@ -81,11 +75,11 @@ "polestar_last_updated_battery_data": { "name": "Dernière mise à jour des données de batterie" }, - "polestar_estimate_full_charge_range_miles": { - "name": "Estimé de l'autonomie complète en miles" - }, "polestar_estimate_full_charge_range": { - "name": "Estimé de l'autonomie complète en km" + "name": "Estimé de l'autonomie complète" + }, + "polestar_estimate_range": { + "name": "Estimé de l'autonomie" }, "polestar_api_status_code": { "name": "Code d'état de l'API"