Skip to content

Commit

Permalink
fix #64 parse m to km
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyuentuen committed Jan 4, 2024
1 parent 02f15a0 commit 8e86b9c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class PolestarSensorDescription(
query="getOdometerData",
field_name="odometerMeters",
unit=UnitOfLength.KILOMETERS,
round_digits=0,
round_digits=2,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DISTANCE,
max_value=None,
Expand Down Expand Up @@ -548,17 +548,20 @@ def state(self) -> StateType:

return estimate_range

if self.entity_description.key in ('current_odometer_meters'):
_LOGGER.debug("current_odometer_meters %s", self._attr_native_value)
# convert m to km, if not int value then it has already convert to km
if isinstance(self._attr_native_value, int):
self._attr_native_value = self._attr_native_value / 1000

# 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)
self._attr_native_value = int(self._attr_native_value)
self._attr_native_value = 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
Expand Down

0 comments on commit 8e86b9c

Please sign in to comment.