mitch-dc / volkswagen_we_connect_id

Apache License 2.0
202 stars 53 forks source link

VolkswagenIDVehicleSensor has the wrong class. #233

Closed ColinRobbins closed 1 month ago

ColinRobbins commented 11 months ago

Version of the custom_component

Installation method (hacs / manual)

HACS

Installation method of hass (venv, docker, hassio,...)

Hassio

Describe the bug

VolkswagenIDVehicleSensor has the wrong class.

Debug log


Logger: homeassistant.components.sensor
Source: components/sensor/__init__.py:528
Integration: Sensor (documentation, issues)
First occurred: 12:10:14 (1 occurrences)
Last logged: 12:10:14

Entity sensor.electra_last_trip_average_electric_consumption (<class 'custom_components.volkswagen_we_connect_id.sensor.VolkswagenIDVehicleSensor'>) is using state class 'measurement' which is impossible considering device class ('energy') it is using; expected None or one of 'total', 'total_increasing'; Please update your configuration if your entity is manually configured, otherwise report it to the author of the 'volkswagen_we_connect_id' custom integration
northalpha commented 9 months ago

This maybe an issue because as far as i can see all VolkswagenIDSensor and VolkswagenIDVehicleSensor are initialsed with SensorStateClass.MEASUREMENT if the sensor itself has declared an native_unit_of_measurement

which is not possible if the sensor is of device_class=SensorDeviceClass.ENERGY

https://github.com/mitch-dc/volkswagen_we_connect_id/blob/c7f95c458e5b31a468703dab8d46f5746bfdb741/custom_components/volkswagen_we_connect_id/sensor.py#L525-L527

fix maybe skipping device_class=SensorDeviceClass.ENERGY from initialising with SensorStateClass.MEASUREMENT but with state_class=SensorStateClass.TOTAL

northalpha commented 9 months ago

okay this would work as a workaround until a more generic fix would be available otherwise this is very specific to this one code path, thoughts @mitch-dc or @pdcastro?

--- a/custom_components/volkswagen_we_connect_id/sensor.py
+++ b/custom_components/volkswagen_we_connect_id/sensor.py
@@ -430,8 +430,8 @@ VEHICLE_SENSORS: tuple[VolkswagenIdEntityDescription, ...] = (
     VolkswagenIdEntityDescription(
         key="lastTripAverageElectricConsumption",
         name="Last Trip Average Electric consumption",
-        native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
         device_class=SensorDeviceClass.ENERGY,
+        state_class=SensorStateClass.TOTAL,
         value=lambda vehicle: vehicle.trips["shortTerm"].averageElectricConsumption.value,
     ),

I can make an MR if you think this would be sufficient.

ColinRobbins commented 1 month ago

Not seeing the issue now, so assuming fixed.