mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Merge pull request #1465 from bestlibre/influxdb_blacklist
Possibility to blacklist events for influxdb
This commit is contained in:
commit
931dc27300
@ -31,8 +31,10 @@ CONF_USERNAME = 'username'
|
|||||||
CONF_PASSWORD = 'password'
|
CONF_PASSWORD = 'password'
|
||||||
CONF_SSL = 'ssl'
|
CONF_SSL = 'ssl'
|
||||||
CONF_VERIFY_SSL = 'verify_ssl'
|
CONF_VERIFY_SSL = 'verify_ssl'
|
||||||
|
CONF_BLACKLIST = 'blacklist'
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-many-locals
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup the InfluxDB component."""
|
"""Setup the InfluxDB component."""
|
||||||
from influxdb import InfluxDBClient, exceptions
|
from influxdb import InfluxDBClient, exceptions
|
||||||
@ -52,6 +54,7 @@ def setup(hass, config):
|
|||||||
ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL)
|
ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL)
|
||||||
verify_ssl = util.convert(conf.get(CONF_VERIFY_SSL), bool,
|
verify_ssl = util.convert(conf.get(CONF_VERIFY_SSL), bool,
|
||||||
DEFAULT_VERIFY_SSL)
|
DEFAULT_VERIFY_SSL)
|
||||||
|
blacklist = conf.get(CONF_BLACKLIST, [])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
influx = InfluxDBClient(host=host, port=port, username=username,
|
influx = InfluxDBClient(host=host, port=port, username=username,
|
||||||
@ -67,7 +70,8 @@ def setup(hass, config):
|
|||||||
def influx_event_listener(event):
|
def influx_event_listener(event):
|
||||||
"""Listen for new messages on the bus and sends them to Influx."""
|
"""Listen for new messages on the bus and sends them to Influx."""
|
||||||
state = event.data.get('new_state')
|
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
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -86,6 +86,7 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
'host': 'host',
|
'host': 'host',
|
||||||
'username': 'user',
|
'username': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
|
'blacklist': ['fake.blacklisted']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.hass = mock.MagicMock()
|
self.hass = mock.MagicMock()
|
||||||
@ -169,3 +170,33 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
self.mock_client.write_points.side_effect = \
|
self.mock_client.write_points.side_effect = \
|
||||||
influx_client.exceptions.InfluxDBClientError('foo')
|
influx_client.exceptions.InfluxDBClientError('foo')
|
||||||
self.handler_method(event)
|
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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user