Use native datetime value in ESPHome sensors (#59896)

This commit is contained in:
Franck Nijhof 2021-11-18 15:32:39 +01:00 committed by GitHub
parent ec6a67d17a
commit 4a83ee5dab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
"""Support for esphome sensors."""
from __future__ import annotations
from datetime import datetime
import math
from aioesphomeapi import (
@ -78,14 +79,14 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
return self._static_info.force_update
@esphome_state_property
def native_value(self) -> str | None:
def native_value(self) -> datetime | str | None:
"""Return the state of the entity."""
if math.isnan(self._state.state):
return None
if self._state.missing_state:
return None
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}"
@property