Skip to content

Commit

Permalink
calculate fully charge time
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Nov 18, 2023
1 parent e8d23af commit e42521c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions custom_components/polestar_api/polestar_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
import json
import logging
from .const import (
ACCESS_TOKEN_MANAGER_ID,
Expand Down Expand Up @@ -124,6 +125,7 @@ async def get_data(self, path, reponse_path=None):
resp = await response.json(content_type=None)

_LOGGER.debug(f"Response {resp}")

data = resp['data']

# add cache_data[path]
Expand Down
26 changes: 22 additions & 4 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json
from datetime import datetime, timedelta
import logging
from typing import Final
from dataclasses import dataclass
from datetime import timedelta

from .const import MAX_CHARGE_RANGE
from .entity import TibberEVEntity
Expand Down Expand Up @@ -76,7 +75,7 @@ class PolestarSensorDescription(
path="{vin}/recharge-status",
response_path="batteryChargeLevel.value",
unit=PERCENTAGE,
round_digits=1,
round_digits=0,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.BATTERY,
),
Expand All @@ -100,6 +99,17 @@ class PolestarSensorDescription(
unit='Minutes',
round_digits=None,
),
PolestarSensorDescription(
key="estimated_fully_charged_time",
name="Fully charged time",
icon="mdi:battery-clock",
path="{vin}/recharge-status",
response_path="estimatedChargingTime.value",
unit=None,
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
),
PolestarSensorDescription(
key="charging_connection_status",
name="Charg. connection status",
Expand All @@ -108,6 +118,7 @@ class PolestarSensorDescription(
response_path="chargingConnectionStatus.value",
unit=None,
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
),
PolestarSensorDescription(
key="charging_system_status",
Expand Down Expand Up @@ -212,13 +223,20 @@ def native_unit_of_measurement(self) -> str | None:
@property
def state(self) -> StateType:
"""Return the state of the sensor."""

# parse the long text with a shorter one from the dict
if self.entity_description.key == 'charging_connection_status':
return ChargingConnectionStatusDict.get(self._attr_native_value, self._attr_native_value)
if self.entity_description.key == 'charging_system_status':
return ChargingSystemStatusDict.get(self._attr_native_value, self._attr_native_value)

# Custom state for estimated_fully_charged_time
if self.entity_description.key == 'estimated_fully_charged_time':
if self._attr_native_value is not None:
value = int(self._attr_native_value)
if value > 0:
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 self._attr_native_value is not None:
Expand Down

0 comments on commit e42521c

Please sign in to comment.