From 62e5aabff3d04d921174257683263c8803526e17 Mon Sep 17 00:00:00 2001 From: Tuen Lee Date: Wed, 3 Jan 2024 17:14:33 +0100 Subject: [PATCH] on exception, after the exception we should fetch again the data --- custom_components/polestar_api/polestar.py | 2 ++ .../polestar_api/pypolestar/auth.py | 18 ++++++++++++------ custom_components/polestar_api/sensor.py | 13 ++++++------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index c999e68..ab80c60 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 @@ -80,6 +81,7 @@ async def async_update(self) -> None: _LOGGER.error("Unexpected Error on update data %s", str(e)) self.polestarApi.next_update = datetime.now() + timedelta(seconds=60) self.polestarApi.latest_call_code = 500 + self.polestarApi.updating = False def set_config_unit(self, unit:UnitSystem): """Set unit system for the device.""" 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