Skip to content

Commit

Permalink
fix #2 prevent spike value
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Nov 19, 2023
1 parent b43908f commit 0c46233
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PolestarSensorDescriptionMixin:
round_digits: int | None
unit: str | None
response_path: str | None
max_value: int | None


@dataclass
Expand Down Expand Up @@ -77,6 +78,7 @@ class PolestarSensorDescription(
round_digits=0,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.BATTERY,
max_value=None,
),
PolestarSensorDescription(
key="last_updated",
Expand All @@ -87,6 +89,7 @@ class PolestarSensorDescription(
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TIMESTAMP,
max_value=None,
),
PolestarSensorDescription(
key="electric_range",
Expand All @@ -98,6 +101,7 @@ class PolestarSensorDescription(
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DISTANCE,
max_value=570, # prevent spike value, and this should be the max range of polestar
),
PolestarSensorDescription(
key="estimated_charging_time",
Expand All @@ -107,6 +111,7 @@ class PolestarSensorDescription(
response_path="estimatedChargingTime.value",
unit='Minutes',
round_digits=None,
max_value=None,
),
PolestarSensorDescription(
key="estimated_fully_charged_time",
Expand All @@ -118,6 +123,7 @@ class PolestarSensorDescription(
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
max_value=None,
),
PolestarSensorDescription(
key="charging_connection_status",
Expand All @@ -128,6 +134,7 @@ class PolestarSensorDescription(
unit=None,
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
max_value=None,
),
PolestarSensorDescription(
key="charging_system_status",
Expand All @@ -137,6 +144,7 @@ class PolestarSensorDescription(
response_path="chargingSystemStatus.value",
unit=None,
round_digits=None,
max_value=None,
),

)
Expand Down Expand Up @@ -248,6 +256,13 @@ def state(self) -> StateType:
self._attr_native_value = int(
self._attr_native_value.replace('.0', ''))

# prevent exponentianal value, we only give state value that is lower than the max value
if self.entity_description.max_value is not None:
if isinstance(self._attr_native_value, str):
self._attr_native_value = int(self._attr_native_value)
if self._attr_native_value > self.entity_description.max_value:
return None

# Custom state for estimated_fully_charged_time
if self.entity_description.key == 'estimated_fully_charged_time':
value = int(self._attr_native_value)
Expand Down

0 comments on commit 0c46233

Please sign in to comment.