Skip to content

Commit

Permalink
add exception on config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyuentuen committed Jan 2, 2024
1 parent f47ed4b commit 2fead1c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
19 changes: 15 additions & 4 deletions custom_components/polestar_api/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = [
Expand Down Expand Up @@ -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."""
Expand Down
5 changes: 2 additions & 3 deletions custom_components/polestar_api/polestar.py
Original file line number Diff line number Diff line change
@@ -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"}

Expand Down
18 changes: 7 additions & 11 deletions custom_components/polestar_api/pypolestar/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

0 comments on commit 2fead1c

Please sign in to comment.