Support pushing all sensors and fix wrong metrics. (#11159)

For example all metrics with unit % match humidity.
This generate correct metrics like this
# HELP nut_ups_battery_charge sensor.nut_ups_battery_charge
# TYPE nut_ups_battery_charge gauge
nut_ups_battery_charge{entity="sensor.nut_ups_battery_charge",friendly_name="NUT UPS Battery Charge"} 98.0
nut_ups_battery_charge{entity="sensor.nut_ups_battery_charge_2",friendly_name="NUT UPS Battery Charge"} 97.0
This commit is contained in:
Michael Kutý 2018-01-08 17:11:45 +01:00 committed by Fabian Affolter
parent b1b0a2589e
commit 0e710099e0
3 changed files with 21 additions and 52 deletions

View file

@ -13,14 +13,14 @@ from aiohttp import web
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,
CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE,
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
REQUIREMENTS = ['prometheus_client==0.0.21']
REQUIREMENTS = ['prometheus_client==0.1.0']
_LOGGER = logging.getLogger(__name__)
@ -181,57 +181,26 @@ class Metrics(object):
pass
def _handle_sensor(self, state):
_sensor_types = {
TEMP_CELSIUS: (
'temperature_c', self.prometheus_client.Gauge,
'Temperature in degrees Celsius',
),
TEMP_FAHRENHEIT: (
'temperature_c', self.prometheus_client.Gauge,
'Temperature in degrees Celsius',
),
'%': (
'relative_humidity', self.prometheus_client.Gauge,
'Relative humidity (0..100)',
),
'lux': (
'light_lux', self.prometheus_client.Gauge,
'Light level in lux',
),
'kWh': (
'electricity_used_kwh', self.prometheus_client.Gauge,
'Electricity used by this device in KWh',
),
'V': (
'voltage', self.prometheus_client.Gauge,
'Currently reported voltage in Volts',
),
'W': (
'electricity_usage_w', self.prometheus_client.Gauge,
'Currently reported electricity draw in Watts',
),
'min': (
'sensor_min', self.prometheus_client.Gauge,
'Time in minutes reported by a sensor'
),
'Events': (
'sensor_event_count', self.prometheus_client.Gauge,
'Number of events for a sensor'
),
}
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
metric = _sensor_types.get(unit)
metric = state.entity_id.split(".")[1]
if metric is not None:
metric = self._metric(*metric)
try:
value = state_helper.state_as_number(state)
if unit == TEMP_FAHRENHEIT:
value = fahrenheit_to_celsius(value)
metric.labels(**self._labels(state)).set(value)
except ValueError:
pass
try:
int(metric.split("_")[-1])
metric = "_".join(metric.split("_")[:-1])
except ValueError:
pass
_metric = self._metric(metric, self.prometheus_client.Gauge,
state.entity_id)
try:
value = state_helper.state_as_number(state)
if unit == TEMP_FAHRENHEIT:
value = fahrenheit_to_celsius(value)
_metric.labels(**self._labels(state)).set(value)
except ValueError:
pass
self._battery(state)

View file

@ -581,7 +581,7 @@ pocketcasts==0.1
proliphix==0.4.1
# homeassistant.components.prometheus
prometheus_client==0.0.21
prometheus_client==0.1.0
# homeassistant.components.sensor.systemmonitor
psutil==5.4.3

View file

@ -115,7 +115,7 @@ pilight==0.1.1
pmsensor==0.4
# homeassistant.components.prometheus
prometheus_client==0.0.21
prometheus_client==0.1.0
# homeassistant.components.canary
py-canary==0.2.3