Skip to content

Commit

Permalink
on exception, after the exception we should fetch again the data
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyuentuen committed Jan 3, 2024
1 parent a2ea344 commit 62e5aab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions custom_components/polestar_api/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand All @@ -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'):
Expand Down Expand Up @@ -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
Expand All @@ -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)

13 changes: 6 additions & 7 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,19 @@ 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
if self.entity_description.round_digits == 0 and isinstance(self._attr_native_value, int):
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
Expand Down

0 comments on commit 62e5aab

Please sign in to comment.