mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Prometheus metrics naming based on device_class and unit_of_measurement (#24103)
* - Change how we extract the metrics for sensors - Add component filtering as seen in influxdb - Add metric override as seen in influxdb - Add more unit tests with actual device data * Extract sensor metric logic to separate handlers * Update prometheus dependency * Format using black * Format using black * Fix flake8 * Move sensor metric handler list to init * Use f strings instead of .format
This commit is contained in:
committed by
Martin Hjelmare
parent
2e4905981e
commit
e9705af055
@@ -2,17 +2,46 @@
|
||||
import asyncio
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import ENERGY_KILO_WATT_HOUR, DEVICE_CLASS_POWER
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components import climate, sensor
|
||||
from homeassistant.components.demo.sensor import DemoSensor
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.prometheus as prometheus
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def prometheus_client(loop, hass, hass_client):
|
||||
async def prometheus_client(loop, hass, hass_client):
|
||||
"""Initialize an hass_client with Prometheus component."""
|
||||
assert loop.run_until_complete(
|
||||
async_setup_component(hass, prometheus.DOMAIN, {prometheus.DOMAIN: {}})
|
||||
await async_setup_component(hass, prometheus.DOMAIN, {prometheus.DOMAIN: {}})
|
||||
|
||||
await setup.async_setup_component(
|
||||
hass, sensor.DOMAIN, {"sensor": [{"platform": "demo"}]}
|
||||
)
|
||||
return loop.run_until_complete(hass_client())
|
||||
|
||||
await setup.async_setup_component(
|
||||
hass, climate.DOMAIN, {"climate": [{"platform": "demo"}]}
|
||||
)
|
||||
|
||||
sensor1 = DemoSensor("Television Energy", 74, None, ENERGY_KILO_WATT_HOUR, None)
|
||||
sensor1.hass = hass
|
||||
sensor1.entity_id = "sensor.television_energy"
|
||||
await sensor1.async_update_ha_state()
|
||||
|
||||
sensor2 = DemoSensor(
|
||||
"Radio Energy", 14, DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR, None
|
||||
)
|
||||
sensor2.hass = hass
|
||||
sensor2.entity_id = "sensor.radio_energy"
|
||||
await sensor2.async_update_ha_state()
|
||||
|
||||
sensor3 = DemoSensor("Electricity price", 0.123, None, "SEK/kWh", None)
|
||||
sensor3.hass = hass
|
||||
sensor3.entity_id = "sensor.electricity_price"
|
||||
await sensor3.async_update_ha_state()
|
||||
|
||||
return await hass_client()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -25,11 +54,52 @@ def test_view(prometheus_client): # pylint: disable=redefined-outer-name
|
||||
body = yield from resp.text()
|
||||
body = body.split("\n")
|
||||
|
||||
assert len(body) > 3 # At least two comment lines and a metric
|
||||
for line in body:
|
||||
if line:
|
||||
assert (
|
||||
line.startswith("# ")
|
||||
or line.startswith("process_")
|
||||
or line.startswith("python_info")
|
||||
)
|
||||
assert len(body) > 3
|
||||
|
||||
assert "# HELP python_info Python platform information" in body
|
||||
assert (
|
||||
"# HELP python_gc_objects_collected_total "
|
||||
"Objects collected during gc" in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'temperature_c{domain="sensor",'
|
||||
'entity="sensor.outside_temperature",'
|
||||
'friendly_name="Outside Temperature"} 15.6' in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'battery_level_percent{domain="sensor",'
|
||||
'entity="sensor.outside_temperature",'
|
||||
'friendly_name="Outside Temperature"} 12.0' in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'current_temperature_c{domain="climate",'
|
||||
'entity="climate.heatpump",'
|
||||
'friendly_name="HeatPump"} 25.0' in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'humidity_percent{domain="sensor",'
|
||||
'entity="sensor.outside_humidity",'
|
||||
'friendly_name="Outside Humidity"} 54.0' in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'sensor_unit_kwh{domain="sensor",'
|
||||
'entity="sensor.television_energy",'
|
||||
'friendly_name="Television Energy"} 74.0' in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'power_kwh{domain="sensor",'
|
||||
'entity="sensor.radio_energy",'
|
||||
'friendly_name="Radio Energy"} 14.0' in body
|
||||
)
|
||||
|
||||
assert (
|
||||
'sensor_unit_sek_per_kwh{domain="sensor",'
|
||||
'entity="sensor.electricity_price",'
|
||||
'friendly_name="Electricity price"} 0.123' in body
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user