From 3fc107616c2c1189370fdd177bc47a4b35441610 Mon Sep 17 00:00:00 2001 From: Tuen Lee Date: Sun, 24 Dec 2023 23:01:36 +0100 Subject: [PATCH] try to get token before get 200 unauthorized --- .../polestar_api/pypolestar/polestar.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/custom_components/polestar_api/pypolestar/polestar.py b/custom_components/polestar_api/pypolestar/polestar.py index d9dd1fa..1dc7ab0 100644 --- a/custom_components/polestar_api/pypolestar/polestar.py +++ b/custom_components/polestar_api/pypolestar/polestar.py @@ -108,6 +108,16 @@ async def get_ev_data(self, vin: str): if self.updating is True: return self.updating = True + + # check if the token is still valid + try: + if self.auth.token_expiry < datetime.now(): + await self.auth.get_token() + except PolestarAuthException as e: + _LOGGER.exception("Auth Exception: %s", str(e)) + self.updating = False + return + try: await self._get_odometer_data(vin) except PolestarNotAuthorizedException: @@ -157,6 +167,9 @@ async def get_graph_ql(self, params: dict): raise PolestarApiException( f"Get GraphQL error: {result.text}") resultData = result.json() + # if we get result with errors and with message "user is not authorized" then we throw an exception + if resultData.get('errors') and resultData['errors'][0]['message'] == "User is not authorized": + raise PolestarNotAuthorizedException("Unauthorized Exception") _LOGGER.debug(resultData) return resultData