Query in InfluxDB sensor is now templatable (#7634)
This commit is contained in:
parent
cefacf9ce4
commit
613da308f2
1 changed files with 30 additions and 9 deletions
|
@ -16,6 +16,7 @@ from homeassistant.const import (CONF_HOST, CONF_PORT, CONF_USERNAME,
|
|||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -41,7 +42,7 @@ REQUIREMENTS = ['influxdb==3.0.0']
|
|||
_QUERY_SCHEME = vol.Schema({
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_MEASUREMENT_NAME): cv.string,
|
||||
vol.Required(CONF_WHERE): 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,
|
||||
|
@ -100,11 +101,10 @@ class InfluxSensor(Entity):
|
|||
database = query.get(CONF_DB_NAME)
|
||||
self._state = None
|
||||
self._hass = hass
|
||||
formated_query = "select {}({}) as value from {} where {}"\
|
||||
.format(query.get(CONF_GROUP_FUNCTION),
|
||||
query.get(CONF_FIELD),
|
||||
query.get(CONF_MEASUREMENT_NAME),
|
||||
query.get(CONF_WHERE))
|
||||
|
||||
where_clause = query.get(CONF_WHERE)
|
||||
where_clause.hass = hass
|
||||
|
||||
influx = InfluxDBClient(
|
||||
host=influx_conf['host'], port=influx_conf['port'],
|
||||
username=influx_conf['username'], password=influx_conf['password'],
|
||||
|
@ -113,7 +113,11 @@ class InfluxSensor(Entity):
|
|||
try:
|
||||
influx.query("select * from /.*/ LIMIT 1;")
|
||||
self.connected = True
|
||||
self.data = InfluxSensorData(influx, formated_query)
|
||||
self.data = InfluxSensorData(influx,
|
||||
query.get(CONF_GROUP_FUNCTION),
|
||||
query.get(CONF_FIELD),
|
||||
query.get(CONF_MEASUREMENT_NAME),
|
||||
where_clause)
|
||||
self.update()
|
||||
except exceptions.InfluxDBClientError as exc:
|
||||
_LOGGER.error("Database host is not accessible due to '%s', please"
|
||||
|
@ -157,15 +161,32 @@ class InfluxSensor(Entity):
|
|||
class InfluxSensorData(object):
|
||||
"""Class for handling the data retrieval."""
|
||||
|
||||
def __init__(self, influx, query):
|
||||
def __init__(self, influx, group, field, measurement, where):
|
||||
"""Initialize the data object."""
|
||||
self.influx = influx
|
||||
self.query = query
|
||||
self.group = group
|
||||
self.field = field
|
||||
self.measurement = measurement
|
||||
self.where = where
|
||||
self.value = None
|
||||
self.query = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Get the latest data with a shell command."""
|
||||
_LOGGER.info("Rendering where: %s", self.where)
|
||||
try:
|
||||
where_clause = self.where.render()
|
||||
except TemplateError as ex:
|
||||
_LOGGER.error('Could not render where clause template: %s', ex)
|
||||
return
|
||||
|
||||
self.query = "select {}({}) as value from {} where {}"\
|
||||
.format(self.group,
|
||||
self.field,
|
||||
self.measurement,
|
||||
where_clause)
|
||||
|
||||
_LOGGER.info("Running query: %s", self.query)
|
||||
|
||||
points = list(self.influx.query(self.query).get_points())
|
||||
|
|
Loading…
Add table
Reference in a new issue