Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove round digits #259

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions custom_components/polestar_api/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 1 addition & 59 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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."""


Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -109,15 +93,13 @@ 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,
),
PolestarSensorDescription(
key="battery_charge_level",
name="Battery Level",
native_unit_of_measurement=PERCENTAGE,
round_digits=0,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.BATTERY,
),
Expand All @@ -126,22 +108,19 @@ 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",
name="Charging Power",
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,
),
Expand All @@ -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,
),
Expand All @@ -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(
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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",
Expand All @@ -203,29 +176,25 @@ 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(
key="estimated_fully_charged_time",
name="Time Full Charged",
icon="mdi:battery-clock",
native_unit_of_measurement=None,
round_digits=None,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -298,15 +260,13 @@ class PolestarSensorDescription(
name="Torque",
icon="mdi:card-account-details",
native_unit_of_measurement="Nm",
round_digits=None,
),
PolestarSensorDescription(
key="battery_capacity",
name="Battery Capacity",
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,
),
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Loading