Add HVAC modes of climate entities to Prometheus exporter (#62144)

This commit is contained in:
Renat Nurgaliyev 2022-01-19 14:49:29 +01:00 committed by GitHub
parent 8e835df8d4
commit d11f2b5151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -11,6 +11,7 @@ from homeassistant import core as hacore
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
ATTR_CURRENT_TEMPERATURE, ATTR_CURRENT_TEMPERATURE,
ATTR_HVAC_ACTION, ATTR_HVAC_ACTION,
ATTR_HVAC_MODES,
ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW, ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_ACTIONS, CURRENT_HVAC_ACTIONS,
@ -393,6 +394,20 @@ class PrometheusMetrics:
float(action == current_action) float(action == current_action)
) )
current_mode = state.state
available_modes = state.attributes.get(ATTR_HVAC_MODES)
if current_mode and available_modes:
metric = self._metric(
"climate_mode",
self.prometheus_cli.Gauge,
"HVAC mode",
["mode"],
)
for mode in available_modes:
metric.labels(**dict(self._labels(state), mode=mode)).set(
float(mode == current_mode)
)
def _handle_humidifier(self, state): def _handle_humidifier(self, state):
humidifier_target_humidity_percent = state.attributes.get(ATTR_HUMIDITY) humidifier_target_humidity_percent = state.attributes.get(ATTR_HUMIDITY)
if humidifier_target_humidity_percent: if humidifier_target_humidity_percent:

View File

@ -419,7 +419,7 @@ async def test_battery(hass, hass_client):
async def test_climate(hass, hass_client): async def test_climate(hass, hass_client):
"""Test prometheus metrics for battery.""" """Test prometheus metrics for climate."""
assert await async_setup_component( assert await async_setup_component(
hass, hass,
"conversation", "conversation",
@ -459,6 +459,20 @@ async def test_climate(hass, hass_client):
'friendly_name="Ecobee"} 24.0' in body 'friendly_name="Ecobee"} 24.0' in body
) )
assert (
'climate_mode{domain="climate",'
'entity="climate.heatpump",'
'friendly_name="HeatPump",'
'mode="heat"} 1.0' in body
)
assert (
'climate_mode{domain="climate",'
'entity="climate.heatpump",'
'friendly_name="HeatPump",'
'mode="off"} 0.0' in body
)
async def test_humidifier(hass, hass_client): async def test_humidifier(hass, hass_client):
"""Test prometheus metrics for battery.""" """Test prometheus metrics for battery."""