Allow exporting of `update` domain to Prometheus (#99400)

pull/99864/head^2
Sam Crang 2023-09-08 04:43:47 +01:00 committed by GitHub
parent b76ba002e2
commit b2b57c5f87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -671,6 +671,15 @@ class PrometheusMetrics:
metric.labels(**self._labels(state)).set(self.state_as_number(state))
def _handle_update(self, state):
metric = self._metric(
"update_state",
self.prometheus_cli.Gauge,
"Update state, indicating if an update is available (0/1)",
)
value = self.state_as_number(state)
metric.labels(**self._labels(state)).set(value)
class PrometheusView(HomeAssistantView):
"""Handle Prometheus requests."""

View File

@ -24,6 +24,7 @@ from homeassistant.components import (
prometheus,
sensor,
switch,
update,
)
from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE,
@ -572,6 +573,23 @@ async def test_counter(client, counter_entities) -> None:
)
@pytest.mark.parametrize("namespace", [""])
async def test_update(client, update_entities) -> None:
"""Test prometheus metrics for update."""
body = await generate_latest_metrics(client)
assert (
'update_state{domain="update",'
'entity="update.firmware",'
'friendly_name="Firmware"} 1.0' in body
)
assert (
'update_state{domain="update",'
'entity="update.addon",'
'friendly_name="Addon"} 0.0' in body
)
@pytest.mark.parametrize("namespace", [""])
async def test_renaming_entity_name(
hass: HomeAssistant,
@ -1591,6 +1609,36 @@ async def counter_fixture(
return data
@pytest.fixture(name="update_entities")
async def update_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate update entities."""
data = {}
update_1 = entity_registry.async_get_or_create(
domain=update.DOMAIN,
platform="test",
unique_id="update_1",
suggested_object_id="firmware",
original_name="Firmware",
)
set_state_with_entry(hass, update_1, STATE_ON)
data["update_1"] = update_1
update_2 = entity_registry.async_get_or_create(
domain=update.DOMAIN,
platform="test",
unique_id="update_2",
suggested_object_id="addon",
original_name="Addon",
)
set_state_with_entry(hass, update_2, STATE_OFF)
data["update_2"] = update_2
await hass.async_block_till_done()
return data
def set_state_with_entry(
hass: HomeAssistant,
entry: er.RegistryEntry,