mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use native datetime value in ESPHome sensors (#59896)
This commit is contained in:
parent
ec6a67d17a
commit
4a83ee5dab
@ -1,6 +1,7 @@
|
|||||||
"""Support for esphome sensors."""
|
"""Support for esphome sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from aioesphomeapi import (
|
from aioesphomeapi import (
|
||||||
@ -78,14 +79,14 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
|
|||||||
return self._static_info.force_update
|
return self._static_info.force_update
|
||||||
|
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
def native_value(self) -> str | None:
|
def native_value(self) -> datetime | str | None:
|
||||||
"""Return the state of the entity."""
|
"""Return the state of the entity."""
|
||||||
if math.isnan(self._state.state):
|
if math.isnan(self._state.state):
|
||||||
return None
|
return None
|
||||||
if self._state.missing_state:
|
if self._state.missing_state:
|
||||||
return None
|
return None
|
||||||
if self.device_class == DEVICE_CLASS_TIMESTAMP:
|
if self.device_class == DEVICE_CLASS_TIMESTAMP:
|
||||||
return dt.utc_from_timestamp(self._state.state).isoformat()
|
return dt.utc_from_timestamp(self._state.state)
|
||||||
return f"{self._state.state:.{self._static_info.accuracy_decimals}f}"
|
return f"{self._state.state:.{self._static_info.accuracy_decimals}f}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
x
Reference in New Issue
Block a user