core/homeassistant/components/influxdb.py

114 lines
3.3 KiB
Python
Raw Normal View History

2015-11-21 18:01:47 +00:00
"""
homeassistant.components.influx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InfluxDB component which allows you to send data to an Influx database.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/influx/
Configuration:
influx:
host: localhost
port: 8086
dbname: home_assistant
dbuser: DB_USER
dbuser_password: DB_USER_PASSWORD
"""
import logging
import homeassistant.util as util
from homeassistant.helpers import validate_config
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF,
STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN)
from homeassistant.components.sun import (STATE_ABOVE_HORIZON,
STATE_BELOW_HORIZON)
2015-11-21 18:01:47 +00:00
_LOGGER = logging.getLogger(__name__)
DOMAIN = "influx"
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'
REQUIREMENTS = ['influxdb==2.10.0']
CONF_HOST = 'host'
CONF_PORT = 'port'
CONF_DB_NAME = 'database'
CONF_USERNAME = 'username'
CONF_PASSWORD = 'password'
def setup(hass, config):
""" Setup the Influx component. """
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)
dbname = util.convert(conf.get(CONF_DB_NAME), str, DEFAULT_DATABASE)
username = util.convert(conf.get(CONF_USERNAME), str)
password = util.convert(conf.get(CONF_PASSWORD), str)
try:
influx = InfluxDBClient(host=host, port=port, username=username,
password=password, database=dbname)
databases = [i['name'] for i in influx.get_list_database()]
except exceptions.InfluxDBClientError:
_LOGGER.error("Database host is not accessible. "
2015-11-21 18:01:47 +00:00
"Please check your entries in the configuration file.")
return False
if dbname not in databases:
_LOGGER.error("Database %s doesn't exist", dbname)
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')
2015-12-06 17:45:58 +00:00
if state is None:
return
2015-12-06 17:45:58 +00:00
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON):
_state = 1
2015-12-06 17:45:58 +00:00
elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN,
STATE_BELOW_HORIZON):
_state = 0
else:
_state = state.state
2015-12-06 17:45:58 +00:00
measurement = state.attributes.get('unit_of_measurement', state.domain)
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,
'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:
_LOGGER.exception('Error saving event to Influx')
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