mirror of
https://github.com/natekspencer/hacs-oasis_mini.git
synced 2025-12-06 18:44:14 -05:00
Merge pull request #110 from natekspencer/last-updated-sensor
Add an additional last updated diagnostic sensor for devices
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any, Callable, Final, Iterable
|
||||
|
||||
@@ -21,6 +22,7 @@ from .utils import (
|
||||
create_svg,
|
||||
decrypt_svg_content,
|
||||
get_image_url_from_track,
|
||||
now,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING: # avoid runtime circular imports
|
||||
@@ -140,6 +142,9 @@ class OasisDevice:
|
||||
self._track: dict | None = None
|
||||
self._track_task: asyncio.Task | None = None
|
||||
|
||||
# Diagnostic metadata
|
||||
self.last_updated: datetime | None = None
|
||||
|
||||
@property
|
||||
def brightness(self) -> int:
|
||||
"""
|
||||
@@ -259,6 +264,8 @@ class OasisDevice:
|
||||
if changed:
|
||||
self._notify_listeners()
|
||||
|
||||
self.last_updated = now()
|
||||
|
||||
def parse_status_string(self, raw_status: str) -> dict[str, Any] | None:
|
||||
"""
|
||||
Parse a semicolon-separated device status string into a structured state dictionary.
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
@@ -68,6 +71,13 @@ DESCRIPTORS = [
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
suggested_display_precision=1,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="last_updated",
|
||||
translation_key="last_updated",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
]
|
||||
DESCRIPTORS.extend(
|
||||
SensorEntityDescription(
|
||||
@@ -84,11 +94,6 @@ class OasisDeviceSensorEntity(OasisDeviceEntity, SensorEntity):
|
||||
"""Oasis device sensor entity."""
|
||||
|
||||
@property
|
||||
def native_value(self) -> str | None:
|
||||
"""
|
||||
Provide the current sensor value from the underlying device.
|
||||
|
||||
Returns:
|
||||
`str` with the sensor's current value, or `None` if the attribute is not present or has no value. The value is taken from the device attribute named by the entity description's `key`.
|
||||
"""
|
||||
def native_value(self) -> str | int | float | datetime | None:
|
||||
"""Provide the current sensor value from the underlying device."""
|
||||
return getattr(self.device, self.entity_description.key)
|
||||
|
||||
@@ -125,6 +125,9 @@
|
||||
"18": "Error while downloading the job file"
|
||||
}
|
||||
},
|
||||
"last_updated": {
|
||||
"name": "Last updated"
|
||||
},
|
||||
"led_color_id": {
|
||||
"name": "LED color ID"
|
||||
},
|
||||
|
||||
@@ -125,6 +125,9 @@
|
||||
"18": "Error while downloading the job file"
|
||||
}
|
||||
},
|
||||
"last_updated": {
|
||||
"name": "Last updated"
|
||||
},
|
||||
"led_color_id": {
|
||||
"name": "LED color ID"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user