diff --git a/custom_components/polestar_api/polestar.py b/custom_components/polestar_api/polestar.py index a45612d..1e0bf19 100644 --- a/custom_components/polestar_api/polestar.py +++ b/custom_components/polestar_api/polestar.py @@ -133,15 +133,10 @@ def update_odometer(self) -> None: """Update data with current car odometer readings""" if data := self.polestar_api.get_car_odometer(self.vin): - average_speed_km_per_hour = ( - round(data.average_speed_km_per_hour) - if data.average_speed_km_per_hour - else None - ) self.data.update( { "current_odometer": data.odometer_meters, - "average_speed": average_speed_km_per_hour, + "average_speed": data.average_speed_km_per_hour, "current_trip_meter_automatic": data.trip_meter_automatic_km, "current_trip_meter_manual": data.trip_meter_manual_km, "last_updated_odometer_data": data.event_updated_timestamp, diff --git a/custom_components/polestar_api/sensor.py b/custom_components/polestar_api/sensor.py index c55b47e..acde69c 100644 --- a/custom_components/polestar_api/sensor.py +++ b/custom_components/polestar_api/sensor.py @@ -1,8 +1,6 @@ """Support for Polestar sensors.""" import logging -from contextlib import suppress -from dataclasses import dataclass from datetime import datetime, timedelta from typing import Final @@ -40,17 +38,7 @@ SCAN_INTERVAL = timedelta(seconds=60) -@dataclass -class PolestarSensorDescriptionMixin: - """Define an entity description mixin for sensor entities.""" - - round_digits: int | None - - -@dataclass -class PolestarSensorDescription( - SensorEntityDescription, PolestarSensorDescriptionMixin -): +class PolestarSensorDescription(SensorEntityDescription): """Class to describe an Polestar sensor entity.""" @@ -69,7 +57,6 @@ class PolestarSensorDescription( name="Range", icon="mdi:map-marker-distance", native_unit_of_measurement=UnitOfLength.KILOMETERS, - round_digits=2, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.DISTANCE, ), @@ -80,7 +67,6 @@ class PolestarSensorDescription( native_unit_of_measurement=UnitOfLength.METERS, suggested_unit_of_measurement=UnitOfLength.KILOMETERS, suggested_display_precision=0, - round_digits=2, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.DISTANCE, ), @@ -89,7 +75,6 @@ class PolestarSensorDescription( name="Average Speed", icon="mdi:speedometer", native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.SPEED, ), @@ -99,7 +84,6 @@ class PolestarSensorDescription( icon="mdi:map-marker-distance", native_unit_of_measurement=UnitOfLength.KILOMETERS, suggested_display_precision=1, - round_digits=2, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.DISTANCE, ), @@ -109,7 +93,6 @@ class PolestarSensorDescription( icon="mdi:map-marker-distance", native_unit_of_measurement=UnitOfLength.KILOMETERS, suggested_display_precision=1, - round_digits=2, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.DISTANCE, ), @@ -117,7 +100,6 @@ class PolestarSensorDescription( key="battery_charge_level", name="Battery Level", native_unit_of_measurement=PERCENTAGE, - round_digits=0, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.BATTERY, ), @@ -126,14 +108,12 @@ class PolestarSensorDescription( name="Charging Time", icon="mdi:battery-clock", native_unit_of_measurement=UnitOfTime.MINUTES, - round_digits=None, ), PolestarSensorDescription( key="charging_status", name="Charging Status", icon="mdi:ev-station", native_unit_of_measurement=None, - round_digits=None, ), PolestarSensorDescription( key="charging_power", @@ -141,7 +121,6 @@ class PolestarSensorDescription( icon="mdi:lightning-bolt", native_unit_of_measurement=UnitOfPower.WATT, suggested_display_precision=0, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.POWER, ), @@ -151,7 +130,6 @@ class PolestarSensorDescription( icon="mdi:current-ac", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, suggested_display_precision=0, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.CURRENT, ), @@ -160,7 +138,6 @@ class PolestarSensorDescription( name="Charging Connection Status", icon="mdi:connection", native_unit_of_measurement=None, - round_digits=None, device_class=None, ), PolestarSensorDescription( @@ -169,7 +146,6 @@ class PolestarSensorDescription( icon="mdi:battery-clock", native_unit_of_measurement="kWh/100km", suggested_display_precision=1, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=None, ), @@ -178,7 +154,6 @@ class PolestarSensorDescription( name="Estimated Charging Time To Target Distance", icon="mdi:battery-clock", native_unit_of_measurement=UnitOfTime.MINUTES, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=None, ), @@ -187,14 +162,12 @@ class PolestarSensorDescription( name="VIN", icon="mdi:card-account-details", native_unit_of_measurement=None, - round_digits=None, ), PolestarSensorDescription( key="software_version", name="Software Version", icon="mdi:information-outline", native_unit_of_measurement=None, - round_digits=None, ), PolestarSensorDescription( key="software_version_release", @@ -203,21 +176,18 @@ class PolestarSensorDescription( state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.TIMESTAMP, native_unit_of_measurement=None, - round_digits=None, ), PolestarSensorDescription( key="registration_number", name="Registration Number", icon="mdi:numeric-1-box", native_unit_of_measurement=None, - round_digits=None, ), PolestarSensorDescription( key="internal_vehicle_id", name="Internal Vehicle ID", icon="mdi:numeric-1-box", native_unit_of_measurement=None, - round_digits=None, entity_registry_enabled_default=False, ), PolestarSensorDescription( @@ -225,7 +195,6 @@ class PolestarSensorDescription( name="Time Full Charged", icon="mdi:battery-clock", native_unit_of_measurement=None, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.DURATION, ), @@ -234,14 +203,12 @@ class PolestarSensorDescription( name="Model Name", icon="mdi:car-electric", native_unit_of_measurement=None, - round_digits=None, ), PolestarSensorDescription( key="last_updated_odometer_data", name="Last Updated Odometer Data", icon="mdi:clock", native_unit_of_measurement=None, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.TIMESTAMP, entity_category=EntityCategory.DIAGNOSTIC, @@ -251,7 +218,6 @@ class PolestarSensorDescription( name="Last Updated Battery Data", icon="mdi:clock", native_unit_of_measurement=None, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.TIMESTAMP, entity_category=EntityCategory.DIAGNOSTIC, @@ -262,7 +228,6 @@ class PolestarSensorDescription( icon="mdi:map-marker-distance", native_unit_of_measurement=UnitOfLength.KILOMETERS, suggested_display_precision=0, - round_digits=None, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.DISTANCE, ), @@ -271,7 +236,6 @@ class PolestarSensorDescription( name="API Status Code (Data)", icon="mdi:heart", native_unit_of_measurement=None, - round_digits=None, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, ), @@ -280,7 +244,6 @@ class PolestarSensorDescription( name="API Status Code (Auth)", icon="mdi:heart", native_unit_of_measurement=None, - round_digits=None, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, ), @@ -289,7 +252,6 @@ class PolestarSensorDescription( name="Auth Token Expired At", icon="mdi:clock-time-eight", native_unit_of_measurement=None, - round_digits=None, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, ), @@ -298,7 +260,6 @@ class PolestarSensorDescription( name="Torque", icon="mdi:card-account-details", native_unit_of_measurement="Nm", - round_digits=None, ), PolestarSensorDescription( key="battery_capacity", @@ -306,7 +267,6 @@ class PolestarSensorDescription( icon="mdi:battery-check", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, suggested_display_precision=0, - round_digits=None, state_class=SensorStateClass.TOTAL, device_class=SensorDeviceClass.ENERGY, ), @@ -355,9 +315,6 @@ def __init__( self._attr_native_value = self.car.data.get(self.entity_description.key) self._attr_extra_state_attributes = {} - if entity_description.round_digits is not None: - self.attr_suggested_display_precision = entity_description.round_digits - if entity_description.state_class is not None: self._attr_state_class = entity_description.state_class if entity_description.device_class is not None: @@ -451,21 +408,6 @@ def state(self) -> StateType: self._sensor_option_unit_of_measurement ) - # only round value if native value is not None - if self._attr_native_value: # noqa - # round the value - if self.entity_description.round_digits is not None: # noqa - # if the value is integer, remove the decimal - if self.entity_description.round_digits == 0 and isinstance( - self._attr_native_value, int - ): - self._attr_native_value = int(self._attr_native_value) - with suppress(ValueError): - self._attr_native_value = round( - float(self._attr_native_value), - self.entity_description.round_digits, - ) - return self._attr_native_value @property