mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Export climate status and target temperature to Prometheus (#10919)
* Export climate metrics to Prometheus. This adds climate_state and temperature_c metrics for each climate device. * Add more climate states to state_as_number
This commit is contained in:
parent
19a97580fc
commit
31cedf83c7
@ -14,7 +14,8 @@ from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components import recorder
|
||||
from homeassistant.const import (
|
||||
CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE, TEMP_CELSIUS,
|
||||
EVENT_STATE_CHANGED, TEMP_FAHRENHEIT, CONTENT_TYPE_TEXT_PLAIN)
|
||||
EVENT_STATE_CHANGED, TEMP_FAHRENHEIT, CONTENT_TYPE_TEXT_PLAIN,
|
||||
ATTR_TEMPERATURE, ATTR_UNIT_OF_MEASUREMENT)
|
||||
from homeassistant import core as hacore
|
||||
from homeassistant.helpers import state as state_helper
|
||||
from homeassistant.util.temperature import fahrenheit_to_celsius
|
||||
@ -159,6 +160,26 @@ class Metrics(object):
|
||||
value = state_helper.state_as_number(state)
|
||||
metric.labels(**self._labels(state)).set(value)
|
||||
|
||||
def _handle_climate(self, state):
|
||||
temp = state.attributes.get(ATTR_TEMPERATURE)
|
||||
if temp:
|
||||
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
if unit == TEMP_FAHRENHEIT:
|
||||
temp = fahrenheit_to_celsius(temp)
|
||||
metric = self._metric(
|
||||
'temperature_c', self.prometheus_client.Gauge,
|
||||
'Temperature in degrees Celsius')
|
||||
metric.labels(**self._labels(state)).set(temp)
|
||||
|
||||
metric = self._metric(
|
||||
'climate_state', self.prometheus_client.Gauge,
|
||||
'State of the thermostat (0/1)')
|
||||
try:
|
||||
value = state_helper.state_as_number(state)
|
||||
metric.labels(**self._labels(state)).set(value)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def _handle_sensor(self, state):
|
||||
_sensor_types = {
|
||||
TEMP_CELSIUS: (
|
||||
@ -199,7 +220,7 @@ class Metrics(object):
|
||||
),
|
||||
}
|
||||
|
||||
unit = state.attributes.get('unit_of_measurement')
|
||||
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
metric = _sensor_types.get(unit)
|
||||
|
||||
if metric is not None:
|
||||
|
@ -21,7 +21,8 @@ from homeassistant.components.climate import (
|
||||
ATTR_HUMIDITY, ATTR_OPERATION_MODE, ATTR_SWING_MODE,
|
||||
SERVICE_SET_AUX_HEAT, SERVICE_SET_AWAY_MODE, SERVICE_SET_HOLD_MODE,
|
||||
SERVICE_SET_FAN_MODE, SERVICE_SET_HUMIDITY, SERVICE_SET_OPERATION_MODE,
|
||||
SERVICE_SET_SWING_MODE, SERVICE_SET_TEMPERATURE)
|
||||
SERVICE_SET_SWING_MODE, SERVICE_SET_TEMPERATURE, STATE_HEAT, STATE_COOL,
|
||||
STATE_IDLE)
|
||||
from homeassistant.components.climate.ecobee import (
|
||||
ATTR_FAN_MIN_ON_TIME, SERVICE_SET_FAN_MIN_ON_TIME,
|
||||
ATTR_RESUME_ALL, SERVICE_RESUME_PROGRAM)
|
||||
@ -210,10 +211,11 @@ def state_as_number(state):
|
||||
Raises ValueError if this is not possible.
|
||||
"""
|
||||
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON,
|
||||
STATE_OPEN, STATE_HOME):
|
||||
STATE_OPEN, STATE_HOME, STATE_HEAT, STATE_COOL):
|
||||
return 1
|
||||
elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN,
|
||||
STATE_BELOW_HORIZON, STATE_CLOSED, STATE_NOT_HOME):
|
||||
STATE_BELOW_HORIZON, STATE_CLOSED, STATE_NOT_HOME,
|
||||
STATE_IDLE):
|
||||
return 0
|
||||
|
||||
return float(state.state)
|
||||
|
Loading…
x
Reference in New Issue
Block a user