Add a Prometheus metric for HVAC action (#31945)

pull/35173/head
Anton Tolchanov 2020-06-03 10:53:21 +01:00 committed by GitHub
parent 37832d5e81
commit d14112748c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 4 deletions

View File

@ -84,6 +84,17 @@ CURRENT_HVAC_IDLE = "idle"
CURRENT_HVAC_FAN = "fan"
# A list of possible HVAC actions.
CURRENT_HVAC_ACTIONS = [
CURRENT_HVAC_OFF,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_COOL,
CURRENT_HVAC_DRY,
CURRENT_HVAC_IDLE,
CURRENT_HVAC_FAN,
]
ATTR_AUX_HEAT = "aux_heat"
ATTR_CURRENT_HUMIDITY = "current_humidity"
ATTR_CURRENT_TEMPERATURE = "current_temperature"

View File

@ -7,7 +7,11 @@ import prometheus_client
import voluptuous as vol
from homeassistant import core as hacore
from homeassistant.components.climate.const import ATTR_CURRENT_TEMPERATURE
from homeassistant.components.climate.const import (
ATTR_CURRENT_TEMPERATURE,
ATTR_HVAC_ACTION,
CURRENT_HVAC_ACTIONS,
)
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import (
ATTR_DEVICE_CLASS,
@ -166,9 +170,10 @@ class PrometheusMetrics:
except ValueError:
pass
def _metric(self, metric, factory, documentation, labels=None):
if labels is None:
labels = ["entity", "friendly_name", "domain"]
def _metric(self, metric, factory, documentation, extra_labels=None):
labels = ["entity", "friendly_name", "domain"]
if extra_labels is not None:
labels.extend(extra_labels)
try:
return self._metrics[metric]
@ -303,6 +308,16 @@ class PrometheusMetrics:
)
metric.labels(**self._labels(state)).set(current_temp)
current_action = state.attributes.get(ATTR_HVAC_ACTION)
if current_action:
metric = self._metric(
"climate_action", self.prometheus_cli.Gauge, "HVAC action", ["action"],
)
for action in CURRENT_HVAC_ACTIONS:
metric.labels(**dict(self._labels(state), action=action)).set(
float(action == current_action)
)
def _handle_sensor(self, state):
unit = self._unit_string(state.attributes.get(ATTR_UNIT_OF_MEASUREMENT))