diff --git a/custom_components/polestar_api/sensor.py b/custom_components/polestar_api/sensor.py index 3f94e1e..66d5407 100644 --- a/custom_components/polestar_api/sensor.py +++ b/custom_components/polestar_api/sensor.py @@ -23,7 +23,7 @@ from homeassistant.helpers import entity_platform from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from homeassistant.util.unit_conversion import DistanceConverter +from homeassistant.util.unit_conversion import DistanceConverter, SpeedConverter from . import DOMAIN as POLESTAR_API_DOMAIN from .entity import PolestarEntity @@ -130,9 +130,9 @@ class PolestarSensorDescription( query="getOdometerData", field_name="averageSpeedKmPerHour", native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR, - round_digits=None, + round_digits=2, state_class=SensorStateClass.MEASUREMENT, - device_class=SensorDeviceClass.DISTANCE, + device_class=SensorDeviceClass.SPEED, max_value=150, dict_data=None ), @@ -519,6 +519,11 @@ def state(self) -> StateType: self._sensor_data, self.entity_description.native_unit_of_measurement, self._sensor_option_unit_of_measurement ) self._attr_native_unit_of_measurement = self._sensor_option_unit_of_measurement + elif self._sensor_option_unit_of_measurement in (UnitOfSpeed.MILES_PER_HOUR, UnitOfSpeed.KILOMETERS_PER_HOUR): + self._attr_native_value = SpeedConverter.convert( + self._sensor_data, self.entity_description.native_unit_of_measurement, self._sensor_option_unit_of_measurement + ) + self._attr_native_unit_of_measurement = self._sensor_option_unit_of_measurement if self.entity_description.key in ("estimate_range", "estimate_full_charge_range"): if self._sensor_option_unit_of_measurement == UnitOfLength.MILES: self.entity_description.max_value = 410