diff --git a/custom_components/polestar_api/__init__.py b/custom_components/polestar_api/__init__.py index 1aba493..ed84dd5 100644 --- a/custom_components/polestar_api/__init__.py +++ b/custom_components/polestar_api/__init__.py @@ -1,10 +1,11 @@ """Polestar EV integration.""" import asyncio +from asyncio import timeout import logging from aiohttp import ClientConnectionError -from async_timeout import timeout +import httpx from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform @@ -14,7 +15,7 @@ from .const import DOMAIN, TIMEOUT from .polestar import Polestar -from .pypolestar.exception import PolestarApiException +from .pypolestar.exception import PolestarApiException, PolestarAuthException from .pypolestar.polestar import PolestarApi PLATFORMS = [ @@ -48,8 +49,18 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b return True except PolestarApiException as e: - _LOGGER.exception("API Exception %s", str(e)) - + _LOGGER.exception("API Exception on update data %s", str(e)) + except PolestarAuthException as e: + _LOGGER.exception("Auth Exception on update data %s", str(e)) + except httpx.ConnectTimeout as e: + _LOGGER.exception("Connection Timeout on update data %s", str(e)) + except httpx.ConnectError as e: + _LOGGER.exception("Connection Error on update data %s", str(e)) + except httpx.ReadTimeout as e: + _LOGGER.exception("Read Timeout on update data %s", str(e)) + except Exception as e: + _LOGGER.exception("Unexpected Error on update data %s", str(e)) + return False async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Unload a config entry.""" diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index 5517a84..cc0c9b4 100644 --- a/custom_components/polestar_api/polestar.py +++ b/custom_components/polestar_api/polestar.py @@ -1,15 +1,14 @@ """Polestar API for Polestar integration.""" -import datetime +from datetime import datetime, timedelta import logging -import httpx +import httpx from urllib3 import disable_warnings from homeassistant.core import HomeAssistant from .pypolestar.exception import PolestarApiException, PolestarAuthException from .pypolestar.polestar import PolestarApi -from datetime import datetime, timedelta POST_HEADER_JSON = {"Content-Type": "application/json"} diff --git a/custom_components/polestar_api/pypolestar/auth.py b/custom_components/polestar_api/pypolestar/auth.py index ffd18f8..0766c37 100644 --- a/custom_components/polestar_api/pypolestar/auth.py +++ b/custom_components/polestar_api/pypolestar/auth.py @@ -10,7 +10,7 @@ class PolestarAuth: - """ base class for Polestar authentication""" + """base class for Polestar authentication.""" def __init__(self, username: str, password: str) -> None: self.username = username @@ -50,8 +50,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"])): - raise PolestarAuthException( - f"Error getting token", result.status_code) + raise PolestarAuthException("Error getting token", result.status_code) _LOGGER.debug(resultData) if resultData['data']: @@ -68,11 +67,11 @@ 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')[0] # get the resumePath if query_params.get('resumePath'): - resumePath = query_params.get(('resumePath')) + resumePath = query_params.get('resumePath') if resumePath is None: return @@ -91,8 +90,7 @@ async def _get_code(self) -> None: ) self.latest_call_code = result.status_code if result.status_code != 302: - raise PolestarAuthException( - f"Error getting code", result.status_code) + raise PolestarAuthException("Error getting code", result.status_code) # get the realUrl url = result.url @@ -103,8 +101,7 @@ async def _get_code(self) -> None: self.latest_call_code = result.status_code if result.status_code != 200: - raise PolestarAuthException( - f"Error getting code callback", result.status_code) + raise PolestarAuthException("Error getting code callback", result.status_code) # url encode the code result = await self._client_session.get(url) @@ -121,6 +118,5 @@ async def _get_resume_path(self): } result = await self._client_session.get("https://polestarid.eu.polestar.com/as/authorization.oauth2", params=params) if result.status_code != 303: - raise PolestarAuthException( - f"Error getting resume path ", result.status_code) + raise PolestarAuthException("Error getting resume path ", result.status_code) return result.next_request.url.params