Rewrite influxdb metrics to be more consistent (#4791)

* Updated to make all metrics consistent

* Updated existing test for new format

* Updated checks on lists and dictionarys
This commit is contained in:
Brent Hughes authored and Paulus Schoutsen committed 2017-01-04 22:36:54 +01:00
1 parent ff0788324c
commit 4ef7e08553
2 files changed
+124 -189

No files matched your search

+32 -29
View File
@@ -9,9 +9,8 @@ import logging
import voluptuous as vol
from homeassistant.const import (
EVENT_STATE_CHANGED, STATE_UNAVAILABLE, STATE_UNKNOWN, CONF_HOST,
CONF_PORT, CONF_SSL, CONF_VERIFY_SSL, CONF_USERNAME, CONF_BLACKLIST,
CONF_PASSWORD, CONF_WHITELIST)
EVENT_STATE_CHANGED, CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL,
CONF_USERNAME, CONF_BLACKLIST, CONF_PASSWORD, CONF_WHITELIST)
from homeassistant.helpers import state as state_helper
import homeassistant.helpers.config_validation as cv
@@ -38,7 +37,6 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_DB_NAME, default=DEFAULT_DATABASE): cv.string,
vol.Optional(CONF_PORT): cv.port,
vol.Optional(CONF_SSL): cv.boolean,
vol.Optional(CONF_DEFAULT_MEASUREMENT): cv.string,
vol.Optional(CONF_TAGS, default={}):
vol.Schema({cv.string: cv.string}),
vol.Optional(CONF_WHITELIST, default=[]):
@@ -78,7 +76,6 @@ def setup(hass, config):
blacklist = conf.get(CONF_BLACKLIST)
whitelist = conf.get(CONF_WHITELIST)
tags = conf.get(CONF_TAGS)
default_measurement = conf.get(CONF_DEFAULT_MEASUREMENT)
try:
influx = InfluxDBClient(**kwargs)
@@ -92,51 +89,57 @@ def setup(hass, config):
def influx_event_listener(event):
"""Listen for new messages on the bus and sends them to Influx."""
state = event.data.get('new_state')
if state is None or state.state in (
STATE_UNKNOWN, '', STATE_UNAVAILABLE) or \
state.entity_id in blacklist:
if state is None or state.entity_id in blacklist:
return
if whitelist and state.entity_id not in whitelist:
return
try:
if len(whitelist) > 0 and state.entity_id not in whitelist:
return
_state = state_helper.state_as_number(state)
except ValueError:
_state = state.state
measurement = state.attributes.get('unit_of_measurement')
if measurement in (None, ''):
if default_measurement:
measurement = default_measurement
else:
measurement = state.entity_id
# Create a counter for this state change
json_body = [
{
'measurement': measurement,
'measurement': "hass.state.count",
'tags': {
'domain': state.domain,
'entity_id': state.object_id,
},
'time': event.time_fired,
'fields': {
'value': _state,
'value': 1
}
}
]
for key, value in state.attributes.items():
if key != 'unit_of_measurement':
if isinstance(value, (str, float, bool)) or \
key.endswith('_id'):
json_body[0]['fields'][key] = value
elif isinstance(value, int):
# Prevent column data errors in influxDB.
json_body[0]['fields'][key] = float(value)
json_body[0]['tags'].update(tags)
state_fields = {}
if isinstance(_state, (int, float)):
state_fields['value'] = float(_state)
for key, value in state.attributes.items():
if isinstance(value, (int, float)):
state_fields[key] = float(value)
if state_fields:
json_body.append(
{
'measurement': "hass.state",
'tags': {
'domain': state.domain,
'entity_id': state.object_id
},
'time': event.time_fired,
'fields': state_fields
}
)
json_body[1]['tags'].update(tags)
try:
influx.write_points(json_body)
except exceptions.InfluxDBClientError: