From 0e710099e066e7fab69e1cc2decf6c98fb077833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Kut=C3=BD?= <6du1ro.n@gmail.com> Date: Mon, 8 Jan 2018 17:11:45 +0100 Subject: [PATCH] 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 --- homeassistant/components/prometheus.py | 69 +++++++------------------- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 21 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/prometheus.py b/homeassistant/components/prometheus.py index 0ecfa50ee63..f9629ca726a 100644 --- a/homeassistant/components/prometheus.py +++ b/homeassistant/components/prometheus.py @@ -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) diff --git a/requirements_all.txt b/requirements_all.txt index bc37b9de96e..4b076b1e1f2 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 4ab838211a5..2cb3dc22821 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -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