Handle timestamp sensors in Prometheus integration (#98001)

This commit is contained in:
Russell Cloran 2023-09-02 01:20:36 -07:00 committed by Bram Kragten
parent 1f0b3c4e33
commit f573c1e27c
2 changed files with 31 additions and 1 deletions

View File

@ -19,6 +19,7 @@ from homeassistant.components.climate import (
from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.humidifier import ATTR_AVAILABLE_MODES, ATTR_HUMIDITY
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
ATTR_DEVICE_CLASS,
@ -44,6 +45,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from homeassistant.helpers.entity_values import EntityValues
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.dt import as_timestamp
from homeassistant.util.unit_conversion import TemperatureConverter
_LOGGER = logging.getLogger(__name__)
@ -147,6 +149,7 @@ class PrometheusMetrics:
self._sensor_metric_handlers = [
self._sensor_override_component_metric,
self._sensor_override_metric,
self._sensor_timestamp_metric,
self._sensor_attribute_metric,
self._sensor_default_metric,
self._sensor_fallback_metric,
@ -292,7 +295,10 @@ class PrometheusMetrics:
def state_as_number(state):
"""Return a state casted to a float."""
try:
value = state_helper.state_as_number(state)
if state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP:
value = as_timestamp(state.state)
else:
value = state_helper.state_as_number(state)
except ValueError:
_LOGGER.debug("Could not convert %s to float", state)
value = 0
@ -576,6 +582,14 @@ class PrometheusMetrics:
return f"sensor_{metric}_{unit}"
return None
@staticmethod
def _sensor_timestamp_metric(state, unit):
"""Get metric for timestamp sensors, which have no unit of measurement attribute."""
metric = state.attributes.get(ATTR_DEVICE_CLASS)
if metric == SensorDeviceClass.TIMESTAMP:
return f"sensor_{metric}_seconds"
return None
def _sensor_override_metric(self, state, unit):
"""Get metric from override in configuration."""
if self._override_metric:

View File

@ -232,6 +232,12 @@ async def test_sensor_device_class(client, sensor_entities) -> None:
'friendly_name="Radio Energy"} 14.0' in body
)
assert (
'sensor_timestamp_seconds{domain="sensor",'
'entity="sensor.timestamp",'
'friendly_name="Timestamp"} 1.691445808136036e+09' in body
)
@pytest.mark.parametrize("namespace", [""])
async def test_input_number(client, input_number_entities) -> None:
@ -1049,6 +1055,16 @@ async def sensor_fixture(
set_state_with_entry(hass, sensor_11, 50)
data["sensor_11"] = sensor_11
sensor_12 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_12",
original_device_class=SensorDeviceClass.TIMESTAMP,
suggested_object_id="Timestamp",
original_name="Timestamp",
)
set_state_with_entry(hass, sensor_12, "2023-08-07T15:03:28.136036-0700")
data["sensor_12"] = sensor_12
await hass.async_block_till_done()
return data