2019-04-03 15:40:03 +00:00
|
|
|
"""InfluxDB component which allows you to get data from an Influx database."""
|
2016-10-27 06:48:57 +00:00
|
|
|
from datetime import timedelta
|
2018-02-04 17:23:26 +00:00
|
|
|
import logging
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-02-04 17:23:26 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_UNIT_OF_MEASUREMENT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2017-06-02 07:01:14 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2016-10-27 06:48:57 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-02-04 17:23:26 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
2016-10-27 06:48:57 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import CONF_DB_NAME
|
|
|
|
|
2016-10-27 06:48:57 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOST = "localhost"
|
2016-10-27 06:48:57 +00:00
|
|
|
DEFAULT_PORT = 8086
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_DATABASE = "home_assistant"
|
2016-10-27 06:48:57 +00:00
|
|
|
DEFAULT_SSL = False
|
|
|
|
DEFAULT_VERIFY_SSL = False
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_GROUP_FUNCTION = "mean"
|
|
|
|
DEFAULT_FIELD = "value"
|
2016-10-27 06:48:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_QUERIES = "queries"
|
|
|
|
CONF_GROUP_FUNCTION = "group_function"
|
|
|
|
CONF_FIELD = "field"
|
|
|
|
CONF_MEASUREMENT_NAME = "measurement"
|
|
|
|
CONF_WHERE = "where"
|
2016-10-27 06:48:57 +00:00
|
|
|
|
2018-02-04 17:23:26 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
2016-10-27 06:48:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_QUERY_SCHEME = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_MEASUREMENT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_WHERE): cv.template,
|
|
|
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_DB_NAME, default=DEFAULT_DATABASE): cv.string,
|
|
|
|
vol.Optional(CONF_GROUP_FUNCTION, default=DEFAULT_GROUP_FUNCTION): cv.string,
|
|
|
|
vol.Optional(CONF_FIELD, default=DEFAULT_FIELD): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_QUERIES): [_QUERY_SCHEME],
|
|
|
|
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Inclusive(CONF_USERNAME, "authentication"): cv.string,
|
|
|
|
vol.Inclusive(CONF_PASSWORD, "authentication"): cv.string,
|
|
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the InfluxDB component."""
|
2017-08-08 18:21:33 +00:00
|
|
|
influx_conf = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"host": config[CONF_HOST],
|
|
|
|
"password": config.get(CONF_PASSWORD),
|
|
|
|
"port": config.get(CONF_PORT),
|
|
|
|
"ssl": config.get(CONF_SSL),
|
|
|
|
"username": config.get(CONF_USERNAME),
|
|
|
|
"verify_ssl": config.get(CONF_VERIFY_SSL),
|
2017-08-08 18:21:33 +00:00
|
|
|
}
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
dev = []
|
|
|
|
|
|
|
|
for query in config.get(CONF_QUERIES):
|
|
|
|
sensor = InfluxSensor(hass, influx_conf, query)
|
|
|
|
if sensor.connected:
|
|
|
|
dev.append(sensor)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InfluxSensor(Entity):
|
|
|
|
"""Implementation of a Influxdb sensor."""
|
|
|
|
|
|
|
|
def __init__(self, hass, influx_conf, query):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
from influxdb import InfluxDBClient, exceptions
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-10-27 06:48:57 +00:00
|
|
|
self._name = query.get(CONF_NAME)
|
|
|
|
self._unit_of_measurement = query.get(CONF_UNIT_OF_MEASUREMENT)
|
|
|
|
value_template = query.get(CONF_VALUE_TEMPLATE)
|
|
|
|
if value_template is not None:
|
|
|
|
self._value_template = value_template
|
|
|
|
self._value_template.hass = hass
|
|
|
|
else:
|
|
|
|
self._value_template = None
|
|
|
|
database = query.get(CONF_DB_NAME)
|
|
|
|
self._state = None
|
|
|
|
self._hass = hass
|
2017-06-02 07:01:14 +00:00
|
|
|
|
|
|
|
where_clause = query.get(CONF_WHERE)
|
|
|
|
where_clause.hass = hass
|
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
influx = InfluxDBClient(
|
2019-07-31 19:25:30 +00:00
|
|
|
host=influx_conf["host"],
|
|
|
|
port=influx_conf["port"],
|
|
|
|
username=influx_conf["username"],
|
|
|
|
password=influx_conf["password"],
|
|
|
|
database=database,
|
|
|
|
ssl=influx_conf["ssl"],
|
|
|
|
verify_ssl=influx_conf["verify_ssl"],
|
|
|
|
)
|
2016-10-27 06:48:57 +00:00
|
|
|
try:
|
2019-02-01 17:35:49 +00:00
|
|
|
influx.query("SHOW SERIES LIMIT 1;")
|
2016-10-27 06:48:57 +00:00
|
|
|
self.connected = True
|
2017-08-08 18:21:33 +00:00
|
|
|
self.data = InfluxSensorData(
|
2019-07-31 19:25:30 +00:00
|
|
|
influx,
|
|
|
|
query.get(CONF_GROUP_FUNCTION),
|
|
|
|
query.get(CONF_FIELD),
|
|
|
|
query.get(CONF_MEASUREMENT_NAME),
|
|
|
|
where_clause,
|
|
|
|
)
|
2016-10-27 06:48:57 +00:00
|
|
|
except exceptions.InfluxDBClientError as exc:
|
2019-07-31 19:25:30 +00:00
|
|
|
_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,
|
|
|
|
)
|
2016-10-27 06:48:57 +00:00
|
|
|
self.connected = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-10-27 06:48:57 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the polling state."""
|
2016-10-27 06:48:57 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def update(self):
|
2017-08-08 18:21:33 +00:00
|
|
|
"""Get the latest data from Influxdb and updates the states."""
|
2016-10-27 06:48:57 +00:00
|
|
|
self.data.update()
|
|
|
|
value = self.data.value
|
|
|
|
if value is None:
|
|
|
|
value = STATE_UNKNOWN
|
|
|
|
if self._value_template is not None:
|
|
|
|
value = self._value_template.render_with_possible_json_value(
|
2019-07-31 19:25:30 +00:00
|
|
|
str(value), STATE_UNKNOWN
|
|
|
|
)
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
self._state = value
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class InfluxSensorData:
|
2016-10-27 06:48:57 +00:00
|
|
|
"""Class for handling the data retrieval."""
|
|
|
|
|
2017-06-02 07:01:14 +00:00
|
|
|
def __init__(self, influx, group, field, measurement, where):
|
2016-10-27 06:48:57 +00:00
|
|
|
"""Initialize the data object."""
|
|
|
|
self.influx = influx
|
2017-06-02 07:01:14 +00:00
|
|
|
self.group = group
|
|
|
|
self.field = field
|
|
|
|
self.measurement = measurement
|
|
|
|
self.where = where
|
2016-10-27 06:48:57 +00:00
|
|
|
self.value = None
|
2017-06-02 07:01:14 +00:00
|
|
|
self.query = None
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data with a shell command."""
|
2017-06-02 07:01:14 +00:00
|
|
|
_LOGGER.info("Rendering where: %s", self.where)
|
|
|
|
try:
|
|
|
|
where_clause = self.where.render()
|
|
|
|
except TemplateError as ex:
|
2017-08-08 18:21:33 +00:00
|
|
|
_LOGGER.error("Could not render where clause template: %s", ex)
|
2017-06-02 07:01:14 +00:00
|
|
|
return
|
|
|
|
|
2017-08-08 18:21:33 +00:00
|
|
|
self.query = "select {}({}) as value from {} where {}".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.group, self.field, self.measurement, where_clause
|
|
|
|
)
|
2017-06-02 07:01:14 +00:00
|
|
|
|
2017-04-24 03:41:09 +00:00
|
|
|
_LOGGER.info("Running query: %s", self.query)
|
2016-10-27 06:48:57 +00:00
|
|
|
|
|
|
|
points = list(self.influx.query(self.query).get_points())
|
2017-04-24 03:41:09 +00:00
|
|
|
if not points:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Query returned no points, sensor state set " "to UNKNOWN: %s",
|
|
|
|
self.query,
|
|
|
|
)
|
2016-11-03 02:50:18 +00:00
|
|
|
self.value = None
|
|
|
|
else:
|
2017-05-13 03:47:12 +00:00
|
|
|
if len(points) > 1:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Query returned multiple points, only first " "one shown: %s",
|
|
|
|
self.query,
|
|
|
|
)
|
|
|
|
self.value = points[0].get("value")
|