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.const import STATE_UNKNOWN
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ REQUIREMENTS = ['influxdb==3.0.0']
|
||||||
_QUERY_SCHEME = vol.Schema({
|
_QUERY_SCHEME = vol.Schema({
|
||||||
vol.Required(CONF_NAME): cv.string,
|
vol.Required(CONF_NAME): cv.string,
|
||||||
vol.Required(CONF_MEASUREMENT_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_UNIT_OF_MEASUREMENT): cv.string,
|
||||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_DB_NAME, default=DEFAULT_DATABASE): cv.string,
|
vol.Optional(CONF_DB_NAME, default=DEFAULT_DATABASE): cv.string,
|
||||||
|
@ -100,11 +101,10 @@ class InfluxSensor(Entity):
|
||||||
database = query.get(CONF_DB_NAME)
|
database = query.get(CONF_DB_NAME)
|
||||||
self._state = None
|
self._state = None
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
formated_query = "select {}({}) as value from {} where {}"\
|
|
||||||
.format(query.get(CONF_GROUP_FUNCTION),
|
where_clause = query.get(CONF_WHERE)
|
||||||
query.get(CONF_FIELD),
|
where_clause.hass = hass
|
||||||
query.get(CONF_MEASUREMENT_NAME),
|
|
||||||
query.get(CONF_WHERE))
|
|
||||||
influx = InfluxDBClient(
|
influx = InfluxDBClient(
|
||||||
host=influx_conf['host'], port=influx_conf['port'],
|
host=influx_conf['host'], port=influx_conf['port'],
|
||||||
username=influx_conf['username'], password=influx_conf['password'],
|
username=influx_conf['username'], password=influx_conf['password'],
|
||||||
|
@ -113,7 +113,11 @@ class InfluxSensor(Entity):
|
||||||
try:
|
try:
|
||||||
influx.query("select * from /.*/ LIMIT 1;")
|
influx.query("select * from /.*/ LIMIT 1;")
|
||||||
self.connected = True
|
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()
|
self.update()
|
||||||
except exceptions.InfluxDBClientError as exc:
|
except exceptions.InfluxDBClientError as exc:
|
||||||
_LOGGER.error("Database host is not accessible due to '%s', please"
|
_LOGGER.error("Database host is not accessible due to '%s', please"
|
||||||
|
@ -157,15 +161,32 @@ class InfluxSensor(Entity):
|
||||||
class InfluxSensorData(object):
|
class InfluxSensorData(object):
|
||||||
"""Class for handling the data retrieval."""
|
"""Class for handling the data retrieval."""
|
||||||
|
|
||||||
def __init__(self, influx, query):
|
def __init__(self, influx, group, field, measurement, where):
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self.influx = influx
|
self.influx = influx
|
||||||
self.query = query
|
self.group = group
|
||||||
|
self.field = field
|
||||||
|
self.measurement = measurement
|
||||||
|
self.where = where
|
||||||
self.value = None
|
self.value = None
|
||||||
|
self.query = None
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data with a shell command."""
|
"""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)
|
_LOGGER.info("Running query: %s", self.query)
|
||||||
|
|
||||||
points = list(self.influx.query(self.query).get_points())
|
points = list(self.influx.query(self.query).get_points())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue