Skip to content

Commit

Permalink
commit last one
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyuentuen committed Dec 31, 2023
1 parent a1d32f9 commit b710f11
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 38 deletions.
18 changes: 6 additions & 12 deletions custom_components/polestar_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
from aiohttp import ClientConnectionError
from async_timeout import timeout

from .pypolestar.exception import PolestarApiException
from .pypolestar.polestar import PolestarApi
from .polestar import Polestar
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_USERNAME,
CONF_PASSWORD,
Platform,
)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from .const import (
DOMAIN,
TIMEOUT
)

from .const import DOMAIN, TIMEOUT
from .polestar import Polestar
from .pypolestar.exception import PolestarApiException
from .pypolestar.polestar import PolestarApi

PLATFORMS = [
Platform.SENSOR,
Expand Down
5 changes: 2 additions & 3 deletions custom_components/polestar_api/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from aiohttp import ClientError
from async_timeout import timeout
import voluptuous as vol
from .polestar import Polestar

from homeassistant import config_entries
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD

from homeassistant.const import CONF_PASSWORD, CONF_USERNAME

from .const import DOMAIN, TIMEOUT
from .polestar import Polestar

_LOGGER = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions custom_components/polestar_api/entity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging

from .polestar import Polestar
from homeassistant.helpers.entity import DeviceInfo, Entity

from .const import DOMAIN as POLESTAR_API_DOMAIN
from homeassistant.helpers.entity import DeviceInfo, Entity
from .polestar import Polestar

_LOGGER = logging.getLogger(__name__)

Expand Down
7 changes: 3 additions & 4 deletions custom_components/polestar_api/pypolestar/auth.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import logging
from datetime import datetime, timedelta
import json
import httpx
import logging

from datetime import datetime, timedelta
import httpx

from .exception import PolestarAuthException


_LOGGER = logging.getLogger(__name__)


Expand Down
15 changes: 10 additions & 5 deletions custom_components/polestar_api/pypolestar/polestar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from datetime import datetime, timedelta
import logging
import httpx

from datetime import datetime, timedelta
import httpx

from .exception import PolestarApiException, PolestarAuthException, PolestarNoDataException, PolestarNotAuthorizedException
from .auth import PolestarAuth
from .const import CACHE_TIME, BATTERY_DATA, CAR_INFO_DATA, ODO_METER_DATA
from .const import BATTERY_DATA, CACHE_TIME, CAR_INFO_DATA, ODO_METER_DATA
from .exception import (
PolestarApiException,
PolestarAuthException,
PolestarNoDataException,
PolestarNotAuthorizedException,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -172,7 +177,7 @@ async def get_graph_ql(self, params: dict):
if resultData.get('errors'):
self.latest_call_code = 500
error_message = resultData['errors'][0]['message']
if error_message == "User is not authorized":
if error_message == "User not authenticated":
raise PolestarNotAuthorizedException("Unauthorized Exception")
_LOGGER.error(error_message)

Expand Down
22 changes: 10 additions & 12 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,6 @@ def state(self) -> StateType:
return datetime.now().replace(second=0, microsecond=0) + timedelta(minutes=round(value))
return 'Not charging'

# 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 ('estimate_full_charge_range', 'estimate_full_charge_range_miles'):
battery_level = self._device.get_latest_data(
self.entity_description.query, 'batteryChargeLevelPercentage')
Expand All @@ -522,12 +515,17 @@ def state(self) -> StateType:

return estimate_range

if self.entity_description.key in 'current_odometer_meters' and int(self._attr_native_value) > 1000:
self._attr_native_value = int(self._attr_native_value)
km = round(self._attr_native_value / 1000,
self.entity_description.round_digits if self.entity_description.round_digits is not None else 0)
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)

return 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)

return self._attr_native_value

Expand Down

0 comments on commit b710f11

Please sign in to comment.