diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 1818f308239..c96ed2e4ed3 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -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.""" diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 07a666946fb..f24782b98d4 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -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,