From d11f2b5151cf7c5e74727a1b013171628d69af89 Mon Sep 17 00:00:00 2001 From: Renat Nurgaliyev Date: Wed, 19 Jan 2022 14:49:29 +0100 Subject: [PATCH] Add HVAC modes of climate entities to Prometheus exporter (#62144) --- homeassistant/components/prometheus/__init__.py | 15 +++++++++++++++ tests/components/prometheus/test_init.py | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 995c65d7583..d3d0a834257 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -11,6 +11,7 @@ from homeassistant import core as hacore from homeassistant.components.climate.const import ( ATTR_CURRENT_TEMPERATURE, ATTR_HVAC_ACTION, + ATTR_HVAC_MODES, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW, CURRENT_HVAC_ACTIONS, @@ -393,6 +394,20 @@ class PrometheusMetrics: 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): humidifier_target_humidity_percent = state.attributes.get(ATTR_HUMIDITY) if humidifier_target_humidity_percent: diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 6b634701ff2..edbb770eb7d 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -419,7 +419,7 @@ async def test_battery(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( hass, "conversation", @@ -459,6 +459,20 @@ async def test_climate(hass, hass_client): '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): """Test prometheus metrics for battery."""