Merge pull request #1465 from bestlibre/influxdb_blacklist

Possibility to blacklist events for influxdb
This commit is contained in:
Paulus Schoutsen 2016-03-05 00:57:26 -08:00
commit 931dc27300
2 changed files with 36 additions and 1 deletions

View File

@ -31,8 +31,10 @@ CONF_USERNAME = 'username'
CONF_PASSWORD = 'password'
CONF_SSL = 'ssl'
CONF_VERIFY_SSL = 'verify_ssl'
CONF_BLACKLIST = 'blacklist'
# pylint: disable=too-many-locals
def setup(hass, config):
"""Setup the InfluxDB component."""
from influxdb import InfluxDBClient, exceptions
@ -52,6 +54,7 @@ def setup(hass, config):
ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL)
verify_ssl = util.convert(conf.get(CONF_VERIFY_SSL), bool,
DEFAULT_VERIFY_SSL)
blacklist = conf.get(CONF_BLACKLIST, [])
try:
influx = InfluxDBClient(host=host, port=port, username=username,
@ -67,7 +70,8 @@ def setup(hass, config):
def influx_event_listener(event):
"""Listen for new messages on the bus and sends them to Influx."""
state = event.data.get('new_state')
if state is None or state.state in (STATE_UNKNOWN, ''):
if state is None or state.state in (STATE_UNKNOWN, '') \
or state.entity_id in blacklist:
return
try:

View File

@ -86,6 +86,7 @@ class TestInfluxDB(unittest.TestCase):
'host': 'host',
'username': 'user',
'password': 'pass',
'blacklist': ['fake.blacklisted']
}
}
self.hass = mock.MagicMock()
@ -169,3 +170,33 @@ class TestInfluxDB(unittest.TestCase):
self.mock_client.write_points.side_effect = \
influx_client.exceptions.InfluxDBClientError('foo')
self.handler_method(event)
@mock.patch('influxdb.InfluxDBClient')
def test_event_listener_blacklist(self, mock_influx):
self._setup(mock_influx)
for entity_id in ('ok', 'blacklisted'):
state = mock.MagicMock(state=1,
domain='fake',
entity_id='fake.{}'.format(entity_id),
object_id=entity_id,
attributes={})
event = mock.MagicMock(data={'new_state': state},
time_fired=12345)
body = [{
'measurement': 'fake.{}'.format(entity_id),
'tags': {
'domain': 'fake',
'entity_id': entity_id,
},
'time': 12345,
'fields': {
'value': 1,
},
}]
self.handler_method(event)
if entity_id == 'ok':
self.mock_client.write_points.assert_called_once_with(body)
else:
self.assertFalse(self.mock_client.write_points.called)
self.mock_client.write_points.reset_mock()