Add Prometheus fan_mode and climate_mode metrics (#120267)

Co-authored-by: Anton Tolchanov <1687799+knyar@users.noreply.github.com>
This commit is contained in:
Elliott Balsley 2024-07-07 10:39:54 -07:00 committed by GitHub
parent 26bc37195e
commit cd72bdd851
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 0 deletions

View file

@ -16,6 +16,8 @@ import voluptuous as vol
from homeassistant import core as hacore
from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE,
ATTR_FAN_MODE,
ATTR_FAN_MODES,
ATTR_HVAC_ACTION,
ATTR_HVAC_MODES,
ATTR_TARGET_TEMP_HIGH,
@ -556,6 +558,34 @@ class PrometheusMetrics:
float(mode == current_mode)
)
preset_mode = state.attributes.get(ATTR_PRESET_MODE)
available_preset_modes = state.attributes.get(ATTR_PRESET_MODES)
if preset_mode and available_preset_modes:
preset_metric = self._metric(
"climate_preset_mode",
prometheus_client.Gauge,
"Preset mode enum",
["mode"],
)
for mode in available_preset_modes:
preset_metric.labels(**dict(self._labels(state), mode=mode)).set(
float(mode == preset_mode)
)
fan_mode = state.attributes.get(ATTR_FAN_MODE)
available_fan_modes = state.attributes.get(ATTR_FAN_MODES)
if fan_mode and available_fan_modes:
fan_mode_metric = self._metric(
"climate_fan_mode",
prometheus_client.Gauge,
"Fan mode enum",
["mode"],
)
for mode in available_fan_modes:
fan_mode_metric.labels(**dict(self._labels(state), mode=mode)).set(
float(mode == fan_mode)
)
def _handle_humidifier(self, state: State) -> None:
humidifier_target_humidity_percent = state.attributes.get(ATTR_HUMIDITY)
if humidifier_target_humidity_percent:

View file

@ -31,8 +31,11 @@ from homeassistant.components import (
)
from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE,
ATTR_FAN_MODE,
ATTR_FAN_MODES,
ATTR_HUMIDITY,
ATTR_HVAC_ACTION,
ATTR_HVAC_MODES,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
)
@ -405,6 +408,18 @@ async def test_climate(
'entity="climate.fritzdect",'
'friendly_name="Fritz!DECT"} 0.0' in body
)
assert (
'climate_preset_mode{domain="climate",'
'entity="climate.ecobee",'
'friendly_name="Ecobee",'
'mode="away"} 1.0' in body
)
assert (
'climate_fan_mode{domain="climate",'
'entity="climate.ecobee",'
'friendly_name="Ecobee",'
'mode="auto"} 1.0' in body
)
@pytest.mark.parametrize("namespace", [""])
@ -1414,6 +1429,11 @@ async def climate_fixture(
ATTR_TARGET_TEMP_LOW: 21,
ATTR_TARGET_TEMP_HIGH: 24,
ATTR_HVAC_ACTION: climate.HVACAction.COOLING,
ATTR_HVAC_MODES: ["off", "heat", "cool", "heat_cool"],
ATTR_PRESET_MODE: "away",
ATTR_PRESET_MODES: ["away", "home", "sleep"],
ATTR_FAN_MODE: "auto",
ATTR_FAN_MODES: ["auto", "on"],
}
set_state_with_entry(
hass, climate_2, climate.HVACAction.HEATING, climate_2_attributes