|
2 | 2 |
|
3 | 3 | from collections.abc import Callable |
4 | 4 | from dataclasses import dataclass |
| 5 | +from datetime import datetime, timedelta |
5 | 6 | from enum import StrEnum |
6 | 7 |
|
7 | 8 | from pynecil import LiveDataResponse, OperatingMode, PowerSource |
|
23 | 24 | from homeassistant.core import HomeAssistant |
24 | 25 | from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
25 | 26 | from homeassistant.helpers.typing import StateType |
| 27 | +from homeassistant.util import dt as dt_util |
26 | 28 |
|
27 | 29 | from . import IronOSConfigEntry |
28 | 30 | from .const import OHM |
@@ -56,7 +58,7 @@ class PinecilSensor(StrEnum): |
56 | 58 | class IronOSSensorEntityDescription(SensorEntityDescription): |
57 | 59 | """IronOS sensor entity descriptions.""" |
58 | 60 |
|
59 | | - value_fn: Callable[[LiveDataResponse, bool], StateType] |
| 61 | + value_fn: Callable[[LiveDataResponse, bool], StateType | datetime] |
60 | 62 |
|
61 | 63 |
|
62 | 64 | PINECIL_SENSOR_DESCRIPTIONS: tuple[IronOSSensorEntityDescription, ...] = ( |
@@ -116,10 +118,14 @@ class IronOSSensorEntityDescription(SensorEntityDescription): |
116 | 118 | IronOSSensorEntityDescription( |
117 | 119 | key=PinecilSensor.UPTIME, |
118 | 120 | translation_key=PinecilSensor.UPTIME, |
119 | | - native_unit_of_measurement=UnitOfTime.SECONDS, |
120 | | - device_class=SensorDeviceClass.DURATION, |
121 | | - state_class=SensorStateClass.TOTAL_INCREASING, |
122 | | - value_fn=lambda data, _: data.uptime, |
| 121 | + device_class=SensorDeviceClass.UPTIME, |
| 122 | + value_fn=( |
| 123 | + lambda data, _: ( |
| 124 | + (dt_util.utcnow() - timedelta(seconds=data.uptime)) |
| 125 | + if data.uptime is not None |
| 126 | + else None |
| 127 | + ) |
| 128 | + ), |
123 | 129 | entity_category=EntityCategory.DIAGNOSTIC, |
124 | 130 | ), |
125 | 131 | IronOSSensorEntityDescription( |
@@ -200,7 +206,7 @@ class IronOSSensorEntity(IronOSBaseEntity, SensorEntity): |
200 | 206 | coordinator: IronOSLiveDataCoordinator |
201 | 207 |
|
202 | 208 | @property |
203 | | - def native_value(self) -> StateType: |
| 209 | + def native_value(self) -> StateType | datetime: |
204 | 210 | """Return sensor state.""" |
205 | 211 | return self.entity_description.value_fn( |
206 | 212 | self.coordinator.data, self.coordinator.has_tip |
|
0 commit comments