Skip to content

Commit

Permalink
Add target high/low temperatures to prometheus integration (#50071)
Browse files Browse the repository at this point in the history
* add target high/low temperatures to prometheus integration

* use labels

* Revert "use labels"

This reverts commit 09c56d6.

* fix naming

* tests

* cleanup

* use three separate metrics

* fix descriptions
  • Loading branch information
mdz authored Aug 4, 2021
1 parent c682d5d commit 515a472
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
45 changes: 31 additions & 14 deletions homeassistant/components/prometheus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from homeassistant.components.climate.const import (
ATTR_CURRENT_TEMPERATURE,
ATTR_HVAC_ACTION,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_ACTIONS,
)
from homeassistant.components.http import HomeAssistantView
Expand Down Expand Up @@ -315,28 +317,43 @@ def _handle_lock(self, state):
value = self.state_as_number(state)
metric.labels(**self._labels(state)).set(value)

def _handle_climate(self, state):
temp = state.attributes.get(ATTR_TEMPERATURE)
def _handle_climate_temp(self, state, attr, metric_name, metric_description):
temp = state.attributes.get(attr)
if temp:
if self._climate_units == TEMP_FAHRENHEIT:
temp = fahrenheit_to_celsius(temp)
metric = self._metric(
"climate_target_temperature_celsius",
metric_name,
self.prometheus_cli.Gauge,
"Target temperature in degrees Celsius",
metric_description,
)
metric.labels(**self._labels(state)).set(temp)

current_temp = state.attributes.get(ATTR_CURRENT_TEMPERATURE)
if current_temp:
if self._climate_units == TEMP_FAHRENHEIT:
current_temp = fahrenheit_to_celsius(current_temp)
metric = self._metric(
"climate_current_temperature_celsius",
self.prometheus_cli.Gauge,
"Current temperature in degrees Celsius",
)
metric.labels(**self._labels(state)).set(current_temp)
def _handle_climate(self, state):
self._handle_climate_temp(
state,
ATTR_TEMPERATURE,
"climate_target_temperature_celsius",
"Target temperature in degrees Celsius",
)
self._handle_climate_temp(
state,
ATTR_TARGET_TEMP_HIGH,
"climate_target_temperature_high_celsius",
"Target high temperature in degrees Celsius",
)
self._handle_climate_temp(
state,
ATTR_TARGET_TEMP_LOW,
"climate_target_temperature_low_celsius",
"Target low temperature in degrees Celsius",
)
self._handle_climate_temp(
state,
ATTR_CURRENT_TEMPERATURE,
"climate_current_temperature_celsius",
"Current temperature in degrees Celsius",
)

current_action = state.attributes.get(ATTR_HVAC_ACTION)
if current_action:
Expand Down
18 changes: 18 additions & 0 deletions tests/components/prometheus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ async def test_view_empty_namespace(hass, hass_client):
'friendly_name="HeatPump"} 25.0' in body
)

assert (
'climate_target_temperature_celsius{domain="climate",'
'entity="climate.heatpump",'
'friendly_name="HeatPump"} 20.0' in body
)

assert (
'climate_target_temperature_low_celsius{domain="climate",'
'entity="climate.ecobee",'
'friendly_name="Ecobee"} 21.0' in body
)

assert (
'climate_target_temperature_high_celsius{domain="climate",'
'entity="climate.ecobee",'
'friendly_name="Ecobee"} 24.0' in body
)

assert (
'humidifier_target_humidity_percent{domain="humidifier",'
'entity="humidifier.humidifier",'
Expand Down

0 comments on commit 515a472

Please sign in to comment.