mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Add timestamp sensor for observation (#121752)
This commit is contained in:
parent
75b1700ed3
commit
e6e748ae0a
@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from types import MappingProxyType
|
||||
from typing import Any
|
||||
|
||||
@ -28,6 +29,7 @@ from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
TimestampDataUpdateCoordinator,
|
||||
)
|
||||
from homeassistant.util.dt import parse_datetime
|
||||
from homeassistant.util.unit_conversion import (
|
||||
DistanceConverter,
|
||||
PressureConverter,
|
||||
@ -137,6 +139,11 @@ SENSOR_TYPES: tuple[NWSSensorEntityDescription, ...] = (
|
||||
native_unit_of_measurement=UnitOfLength.METERS,
|
||||
unit_convert=UnitOfLength.MILES,
|
||||
),
|
||||
NWSSensorEntityDescription(
|
||||
key="timestamp",
|
||||
name="Latest Observation Time",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@ -190,7 +197,7 @@ class NWSSensor(CoordinatorEntity[TimestampDataUpdateCoordinator[None]], SensorE
|
||||
)
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
def native_value(self) -> float | datetime | None:
|
||||
"""Return the state."""
|
||||
if (
|
||||
not (observation := self._nws.observation)
|
||||
@ -223,4 +230,6 @@ class NWSSensor(CoordinatorEntity[TimestampDataUpdateCoordinator[None]], SensorE
|
||||
return round(value, 1)
|
||||
if unit_of_measurement == PERCENTAGE:
|
||||
return round(value)
|
||||
if self.device_class == SensorDeviceClass.TIMESTAMP:
|
||||
return parse_datetime(value)
|
||||
return value
|
||||
|
@ -66,6 +66,7 @@ CLEAR_NIGHT_OBSERVATION = DEFAULT_OBSERVATION.copy()
|
||||
CLEAR_NIGHT_OBSERVATION["iconTime"] = "night"
|
||||
|
||||
SENSOR_EXPECTED_OBSERVATION_METRIC = {
|
||||
"timestamp": "2019-08-12T23:53:00+00:00",
|
||||
"dewpoint": "5",
|
||||
"temperature": "10",
|
||||
"windChill": "5",
|
||||
@ -80,6 +81,7 @@ SENSOR_EXPECTED_OBSERVATION_METRIC = {
|
||||
}
|
||||
|
||||
SENSOR_EXPECTED_OBSERVATION_IMPERIAL = {
|
||||
"timestamp": "2019-08-12T23:53:00+00:00",
|
||||
"dewpoint": str(
|
||||
round(
|
||||
TemperatureConverter.convert(
|
||||
|
Loading…
x
Reference in New Issue
Block a user