mitch-dc/volkswagen_we_connect_id

VolkswagenIDVehicleSensor has the wrong class.

ColinRobbins opened this issue · 3 comments

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

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

if sensor.native_unit_of_measurement:
self._attr_native_unit_of_measurement = sensor.native_unit_of_measurement
self._attr_state_class = SensorStateClass.MEASUREMENT

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

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.