diff --git a/custom_components/audiconnect/dashboard.py b/custom_components/audiconnect/dashboard.py index 61e61ea0..b63b3b21 100644 --- a/custom_components/audiconnect/dashboard.py +++ b/custom_components/audiconnect/dashboard.py @@ -2,7 +2,6 @@ import logging import re -from datetime import datetime _LOGGER = logging.getLogger(__name__) @@ -128,6 +127,7 @@ def attributes(self): class Sensor(Instrument): def __init__(self, attr, name, icon, unit): super().__init__(component="sensor", attr=attr, name=name, icon=icon) + self.device_class = None self._unit = unit self._convert = False @@ -319,6 +319,7 @@ def str_state(self): class TripData(Instrument): def __init__(self, attr, name): super().__init__(component="sensor", attr=attr, name=name) + self.device_class = None self.unit = None @property @@ -361,6 +362,7 @@ def __init__(self): name="Last Update", icon="mdi:update", ) + self.device_class = "timestamp" self.unit = None @property @@ -370,14 +372,12 @@ def is_mutable(self): @property def str_state(self): ts = super().state - if type(ts) == datetime: - return str(ts.astimezone(tz=None)) if ts else None - return ts + return ts.astimezone(tz=None).isoformat() if ts else None @property def state(self): val = super().state - return val + return val.astimezone(tz=None).isoformat() if val else None def create_instruments(): diff --git a/custom_components/audiconnect/sensor.py b/custom_components/audiconnect/sensor.py index 5cda2921..6952368a 100644 --- a/custom_components/audiconnect/sensor.py +++ b/custom_components/audiconnect/sensor.py @@ -2,6 +2,7 @@ import logging +from homeassistant.components.sensor import DEVICE_CLASSES, SensorEntity from homeassistant.const import CONF_USERNAME from .audi_entity import AudiEntity @@ -27,7 +28,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(sensors, True) -class AudiSensor(AudiEntity): +class AudiSensor(AudiEntity, SensorEntity): """Representation of a Audi sensor.""" @property @@ -39,3 +40,13 @@ def state(self): def unit_of_measurement(self): """Return the unit of measurement.""" return self._instrument.unit + + @property + def device_class(self): + """Return the class of this sensor, from DEVICE_CLASSES.""" + if ( + self._instrument.device_class is not None + and self._instrument.device_class in DEVICE_CLASSES + ): + return self._instrument.device_class + return None