Add counter entities support to Prometheus component (#62410)
This commit is contained in:
parent
911f2751b4
commit
f71d49230d
2 changed files with 35 additions and 1 deletions
|
@ -515,6 +515,15 @@ class PrometheusMetrics:
|
|||
|
||||
metric.labels(**self._labels(state)).inc()
|
||||
|
||||
def _handle_counter(self, state):
|
||||
metric = self._metric(
|
||||
"counter_value",
|
||||
self.prometheus_cli.Gauge,
|
||||
"Value of counter entities",
|
||||
)
|
||||
|
||||
metric.labels(**self._labels(state)).set(self.state_as_number(state))
|
||||
|
||||
|
||||
class PrometheusView(HomeAssistantView):
|
||||
"""Handle Prometheus requests."""
|
||||
|
|
|
@ -7,7 +7,7 @@ import unittest.mock as mock
|
|||
import prometheus_client
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import climate, humidifier, lock, sensor
|
||||
from homeassistant.components import climate, counter, humidifier, lock, sensor
|
||||
from homeassistant.components.demo.binary_sensor import DemoBinarySensor
|
||||
from homeassistant.components.demo.light import DemoLight
|
||||
from homeassistant.components.demo.number import DemoNumber
|
||||
|
@ -662,6 +662,31 @@ async def test_lock(hass, hass_client):
|
|||
)
|
||||
|
||||
|
||||
async def test_counter(hass, hass_client):
|
||||
"""Test prometheus metrics for counter."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"conversation",
|
||||
{},
|
||||
)
|
||||
|
||||
client = await setup_prometheus_client(hass, hass_client, "")
|
||||
|
||||
await async_setup_component(
|
||||
hass, counter.DOMAIN, {"counter": {"counter": {"initial": "2"}}}
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
body = await generate_latest_metrics(client)
|
||||
|
||||
assert (
|
||||
'counter_value{domain="counter",'
|
||||
'entity="counter.counter",'
|
||||
'friendly_name="None"} 2.0' in body
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_client")
|
||||
def mock_client_fixture():
|
||||
"""Mock the prometheus client."""
|
||||
|
|
Loading…
Add table
Reference in a new issue