Add Min and Event Count Metrics To Prometheus (#10530)

* Added min and Events sensor types to prometheus

* Updated prometheus client and fixed invalid swith state

* Added metric to count number of times an automation is triggered

* Removed assumption that may not apply to everybody

* Fixed tests

* Updated requirements_test_all

* Fixed unit tests
This commit is contained in:
Brent Hughes 2017-12-03 16:39:54 -06:00 committed by Martin Hjelmare
parent 3a246df544
commit 879e32f670
4 changed files with 29 additions and 6 deletions

View File

@ -19,7 +19,7 @@ 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.19']
REQUIREMENTS = ['prometheus_client==0.0.21']
_LOGGER = logging.getLogger(__name__)
@ -189,6 +189,14 @@ class Metrics(object):
'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('unit_of_measurement')
@ -212,12 +220,25 @@ class Metrics(object):
self.prometheus_client.Gauge,
'State of the switch (0/1)',
)
value = state_helper.state_as_number(state)
metric.labels(**self._labels(state)).set(value)
try:
value = state_helper.state_as_number(state)
metric.labels(**self._labels(state)).set(value)
except ValueError:
pass
def _handle_zwave(self, state):
self._battery(state)
def _handle_automation(self, state):
metric = self._metric(
'automation_triggered_count',
self.prometheus_client.Counter,
'Count of times an automation has been triggered',
)
metric.labels(**self._labels(state)).inc()
class PrometheusView(HomeAssistantView):
"""Handle Prometheus requests."""

View File

@ -560,7 +560,7 @@ pocketcasts==0.1
proliphix==0.4.1
# homeassistant.components.prometheus
prometheus_client==0.0.19
prometheus_client==0.0.21
# homeassistant.components.sensor.systemmonitor
psutil==5.4.1

View File

@ -113,7 +113,7 @@ pilight==0.1.1
pmsensor==0.4
# homeassistant.components.prometheus
prometheus_client==0.0.19
prometheus_client==0.0.21
# homeassistant.components.zwave
pydispatcher==2.0.5

View File

@ -30,4 +30,6 @@ def test_view(prometheus_client): # pylint: disable=redefined-outer-name
assert len(body) > 3 # At least two comment lines and a metric
for line in body:
if line:
assert line.startswith('# ') or line.startswith('process_')
assert line.startswith('# ') \
or line.startswith('process_') \
or line.startswith('python_info')