add target high/low temperatures to prometheus integration

This commit is contained in:
Matt Zimmerman
2021-05-04 08:02:33 -07:00
parent f1f293de02
commit dcec106b55

View File

@@ -10,6 +10,8 @@ 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_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_ACTIONS, CURRENT_HVAC_ACTIONS,
) )
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
@@ -315,28 +317,43 @@ class PrometheusMetrics:
value = self.state_as_number(state) value = self.state_as_number(state)
metric.labels(**self._labels(state)).set(value) metric.labels(**self._labels(state)).set(value)
def _handle_climate(self, state): def _handle_climate_temp(self, state, attr, metric_name, metric_description):
temp = state.attributes.get(ATTR_TEMPERATURE) temp = state.attributes.get(attr)
if temp: if temp:
if self._climate_units == TEMP_FAHRENHEIT: if self._climate_units == TEMP_FAHRENHEIT:
temp = fahrenheit_to_celsius(temp) temp = fahrenheit_to_celsius(temp)
metric = self._metric( metric = self._metric(
"climate_target_temperature_celsius", metric_name,
self.prometheus_cli.Gauge, self.prometheus_cli.Gauge,
"Target temperature in degrees Celsius", metric_description,
) )
metric.labels(**self._labels(state)).set(temp) metric.labels(**self._labels(state)).set(temp)
current_temp = state.attributes.get(ATTR_CURRENT_TEMPERATURE) def _handle_climate(self, state):
if current_temp: self._handle_climate_temp(
if self._climate_units == TEMP_FAHRENHEIT: state,
current_temp = fahrenheit_to_celsius(current_temp) ATTR_TEMPERATURE,
metric = self._metric( "climate_target_temperature_celsius",
"climate_current_temperature_celsius", "Target temperature in degrees Celsius",
self.prometheus_cli.Gauge, )
"Current temperature in degrees Celsius", self._handle_climate_temp(
) state,
metric.labels(**self._labels(state)).set(current_temp) ATTR_CURRENT_TEMPERATURE,
"climate_current_temperature_celsius",
"Current temperature in degrees Celsius",
)
self._handle_climate_temp(
state,
ATTR_TARGET_TEMP_HIGH,
"climate_target_temperature_high_c",
"Target high temperature in degrees Celsius",
)
self._handle_climate_temp(
state,
ATTR_TARGET_TEMP_LOW,
"climate_target_temperature_low_c",
"Target low temperature in degrees Celsius",
)
current_action = state.attributes.get(ATTR_HVAC_ACTION) current_action = state.attributes.get(ATTR_HVAC_ACTION)
if current_action: if current_action: