diff --git a/.coveragerc b/.coveragerc index 46a945e760b..849f52c7ff7 100644 --- a/.coveragerc +++ b/.coveragerc @@ -52,6 +52,7 @@ omit = homeassistant/components/discovery.py homeassistant/components/downloader.py homeassistant/components/ifttt.py + homeassistant/components/influx.py homeassistant/components/keyboard.py homeassistant/components/light/hue.py homeassistant/components/light/mqtt.py diff --git a/homeassistant/components/influx.py b/homeassistant/components/influx.py new file mode 100644 index 00000000000..e8b458892cc --- /dev/null +++ b/homeassistant/components/influx.py @@ -0,0 +1,120 @@ +""" +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) + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "influx" +DEPENDENCIES = [] + +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 + + 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. " + "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 + + def event_listener(event): + """ Listen for new messages on the bus and sends them to Influx. """ + event_data = event.as_dict() + + if event_data['event_type'] is not EVENT_STATE_CHANGED: + return + + state = event_data['data']['new_state'] + + if state.state == STATE_ON or state.state == STATE_LOCKED or \ + state.state == STATE_ABOVE_HORIZON: + _state = 1 + elif state.state == STATE_OFF or state.state == STATE_UNLOCKED or \ + state.state == STATE_UNKNOWN or \ + state.state == STATE_BELOW_HORIZON: + _state = 0 + else: + _state = state.state + + try: + measurement = state.attributes['unit_of_measurement'] + except KeyError: + measurement = '{}'.format(state.domain) + + json_body = [ + { + 'measurement': measurement, + 'tags': { + 'domain': state.domain, + 'entity_id': state.object_id, + }, + 'time': event_data['time_fired'], + 'fields': { + 'value': _state, + } + } + ] + + if json_body: + try: + influx.write_points(json_body) + except exceptions.InfluxDBClientError: + _LOGGER.exception('Error saving event to Influx') + + hass.bus.listen(EVENT_STATE_CHANGED, event_listener) + + return True diff --git a/requirements_all.txt b/requirements_all.txt index 0d07c3ef742..7bb549ca579 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -26,6 +26,9 @@ https://github.com/nkgilley/python-ecobee-api/archive/92a2f330cbaf601d0618456fdd # homeassistant.components.ifttt pyfttt==0.3 +# homeassistant.components.influx +influxdb==2.10.0 + # homeassistant.components.isy994 PyISY==1.0.5