2015-11-21 18:01:47 +00:00
|
|
|
"""
|
2015-12-07 06:33:07 +00:00
|
|
|
homeassistant.components.influxdb
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-11-21 18:01:47 +00:00
|
|
|
InfluxDB component which allows you to send data to an Influx database.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2015-12-07 06:33:07 +00:00
|
|
|
https://home-assistant.io/components/influxdb/
|
2015-11-21 18:01:47 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import homeassistant.util as util
|
|
|
|
from homeassistant.helpers import validate_config
|
2016-02-11 17:13:57 +00:00
|
|
|
from homeassistant.helpers import state as state_helper
|
|
|
|
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_UNKNOWN)
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-12-07 13:28:24 +00:00
|
|
|
DOMAIN = "influxdb"
|
2015-12-04 15:25:34 +00:00
|
|
|
DEPENDENCIES = []
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
DEFAULT_HOST = 'localhost'
|
|
|
|
DEFAULT_PORT = 8086
|
|
|
|
DEFAULT_DATABASE = 'home_assistant'
|
2016-02-02 19:51:18 +00:00
|
|
|
DEFAULT_SSL = False
|
|
|
|
DEFAULT_VERIFY_SSL = False
|
2015-11-21 18:01:47 +00:00
|
|
|
|
2016-02-09 15:32:05 +00:00
|
|
|
REQUIREMENTS = ['influxdb==2.12.0']
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
CONF_HOST = 'host'
|
|
|
|
CONF_PORT = 'port'
|
|
|
|
CONF_DB_NAME = 'database'
|
|
|
|
CONF_USERNAME = 'username'
|
|
|
|
CONF_PASSWORD = 'password'
|
2016-02-02 19:51:18 +00:00
|
|
|
CONF_SSL = 'ssl'
|
|
|
|
CONF_VERIFY_SSL = 'verify_ssl'
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2015-12-07 06:33:07 +00:00
|
|
|
""" Setup the InfluxDB component. """
|
2015-11-21 18:01:47 +00:00
|
|
|
|
2015-11-25 21:47:00 +00:00
|
|
|
from influxdb import InfluxDBClient, exceptions
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
if not validate_config(config, {DOMAIN: ['host']}, _LOGGER):
|
|
|
|
return False
|
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
|
|
|
|
host = conf[CONF_HOST]
|
|
|
|
port = util.convert(conf.get(CONF_PORT), int, DEFAULT_PORT)
|
2015-12-07 13:28:24 +00:00
|
|
|
database = util.convert(conf.get(CONF_DB_NAME), str, DEFAULT_DATABASE)
|
2015-11-21 18:01:47 +00:00
|
|
|
username = util.convert(conf.get(CONF_USERNAME), str)
|
|
|
|
password = util.convert(conf.get(CONF_PASSWORD), str)
|
2016-02-02 19:51:18 +00:00
|
|
|
ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL)
|
|
|
|
verify_ssl = util.convert(conf.get(CONF_VERIFY_SSL), bool,
|
|
|
|
DEFAULT_VERIFY_SSL)
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
try:
|
2015-11-25 21:47:00 +00:00
|
|
|
influx = InfluxDBClient(host=host, port=port, username=username,
|
2016-02-02 19:51:18 +00:00
|
|
|
password=password, database=database,
|
|
|
|
ssl=ssl, verify_ssl=verify_ssl)
|
2016-01-20 21:43:35 +00:00
|
|
|
influx.query("select * from /.*/ LIMIT 1;")
|
|
|
|
except exceptions.InfluxDBClientError as exc:
|
|
|
|
_LOGGER.error("Database host is not accessible due to '%s', please "
|
|
|
|
"check your entries in the configuration file and that"
|
|
|
|
" the database exists and is READ/WRITE.", exc)
|
2015-11-25 21:47:00 +00:00
|
|
|
return False
|
2015-11-21 18:01:47 +00:00
|
|
|
|
2015-12-06 17:45:58 +00:00
|
|
|
def influx_event_listener(event):
|
2015-11-21 18:01:47 +00:00
|
|
|
""" Listen for new messages on the bus and sends them to Influx. """
|
|
|
|
|
2015-12-06 17:45:58 +00:00
|
|
|
state = event.data.get('new_state')
|
2016-02-03 21:54:41 +00:00
|
|
|
if state is None or state.state in (STATE_UNKNOWN, ''):
|
2015-12-06 17:45:58 +00:00
|
|
|
return
|
2015-11-25 21:47:00 +00:00
|
|
|
|
2016-02-11 17:13:57 +00:00
|
|
|
try:
|
|
|
|
_state = state_helper.state_as_number(state)
|
|
|
|
except ValueError:
|
|
|
|
_state = state.state
|
2015-11-25 21:47:00 +00:00
|
|
|
|
2016-02-03 21:54:41 +00:00
|
|
|
measurement = state.attributes.get('unit_of_measurement')
|
|
|
|
if measurement in (None, ''):
|
2016-02-07 03:33:43 +00:00
|
|
|
measurement = state.entity_id
|
2015-11-25 21:47:00 +00:00
|
|
|
|
|
|
|
json_body = [
|
|
|
|
{
|
|
|
|
'measurement': measurement,
|
|
|
|
'tags': {
|
|
|
|
'domain': state.domain,
|
|
|
|
'entity_id': state.object_id,
|
|
|
|
},
|
2015-12-06 17:45:58 +00:00
|
|
|
'time': event.time_fired,
|
2015-11-25 21:47:00 +00:00
|
|
|
'fields': {
|
|
|
|
'value': _state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2015-11-21 18:01:47 +00:00
|
|
|
|
2015-12-06 17:45:58 +00:00
|
|
|
try:
|
|
|
|
influx.write_points(json_body)
|
|
|
|
except exceptions.InfluxDBClientError:
|
2016-01-20 21:43:35 +00:00
|
|
|
_LOGGER.exception('Error saving event "%s" to InfluxDB', json_body)
|
2015-11-21 18:01:47 +00:00
|
|
|
|
2015-12-06 17:45:58 +00:00
|
|
|
hass.bus.listen(EVENT_STATE_CHANGED, influx_event_listener)
|
2015-11-21 18:01:47 +00:00
|
|
|
|
|
|
|
return True
|