Add timestamp sensor for observation (#121752)

This commit is contained in:
Sean Chen 2024-07-19 12:50:38 -05:00 committed by GitHub
parent 75b1700ed3
commit e6e748ae0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime
from types import MappingProxyType from types import MappingProxyType
from typing import Any from typing import Any
@ -28,6 +29,7 @@ from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
TimestampDataUpdateCoordinator, TimestampDataUpdateCoordinator,
) )
from homeassistant.util.dt import parse_datetime
from homeassistant.util.unit_conversion import ( from homeassistant.util.unit_conversion import (
DistanceConverter, DistanceConverter,
PressureConverter, PressureConverter,
@ -137,6 +139,11 @@ SENSOR_TYPES: tuple[NWSSensorEntityDescription, ...] = (
native_unit_of_measurement=UnitOfLength.METERS, native_unit_of_measurement=UnitOfLength.METERS,
unit_convert=UnitOfLength.MILES, 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 @property
def native_value(self) -> float | None: def native_value(self) -> float | datetime | None:
"""Return the state.""" """Return the state."""
if ( if (
not (observation := self._nws.observation) not (observation := self._nws.observation)
@ -223,4 +230,6 @@ class NWSSensor(CoordinatorEntity[TimestampDataUpdateCoordinator[None]], SensorE
return round(value, 1) return round(value, 1)
if unit_of_measurement == PERCENTAGE: if unit_of_measurement == PERCENTAGE:
return round(value) return round(value)
if self.device_class == SensorDeviceClass.TIMESTAMP:
return parse_datetime(value)
return value return value

View File

@ -66,6 +66,7 @@ CLEAR_NIGHT_OBSERVATION = DEFAULT_OBSERVATION.copy()
CLEAR_NIGHT_OBSERVATION["iconTime"] = "night" CLEAR_NIGHT_OBSERVATION["iconTime"] = "night"
SENSOR_EXPECTED_OBSERVATION_METRIC = { SENSOR_EXPECTED_OBSERVATION_METRIC = {
"timestamp": "2019-08-12T23:53:00+00:00",
"dewpoint": "5", "dewpoint": "5",
"temperature": "10", "temperature": "10",
"windChill": "5", "windChill": "5",
@ -80,6 +81,7 @@ SENSOR_EXPECTED_OBSERVATION_METRIC = {
} }
SENSOR_EXPECTED_OBSERVATION_IMPERIAL = { SENSOR_EXPECTED_OBSERVATION_IMPERIAL = {
"timestamp": "2019-08-12T23:53:00+00:00",
"dewpoint": str( "dewpoint": str(
round( round(
TemperatureConverter.convert( TemperatureConverter.convert(