Skip to content

Commit

Permalink
fix: LastUpdate timestamp Bug 299 (#300)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 21, 2024
1 parent 061aa85 commit 2d271f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 5 additions & 5 deletions custom_components/audiconnect/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
import re
from datetime import datetime

_LOGGER = logging.getLogger(__name__)

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -361,6 +362,7 @@ def __init__(self):
name="Last Update",
icon="mdi:update",
)
self.device_class = "timestamp"
self.unit = None

@property
Expand All @@ -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():
Expand Down
13 changes: 12 additions & 1 deletion custom_components/audiconnect/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 2d271f8

Please sign in to comment.