Possibility to blacklist events

Possibility to blacklist some events based on the entity_id
This commit is contained in:
bestlibre 2016-03-03 11:09:37 +01:00 committed by Baptiste Poirriez
parent 1e3199eb8c
commit 4d5e9f2931
2 changed files with 35 additions and 0 deletions

View File

@ -33,6 +33,7 @@ CONF_SSL = 'ssl'
CONF_VERIFY_SSL = 'verify_ssl' CONF_VERIFY_SSL = 'verify_ssl'
# 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 +53,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('blacklist', [])
try: try:
influx = InfluxDBClient(host=host, port=port, username=username, influx = InfluxDBClient(host=host, port=port, username=username,
@ -69,6 +71,8 @@ def setup(hass, config):
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, ''):
return return
if state.entity_id in blacklist:
return
try: try:
_state = state_helper.state_as_number(state) _state = state_helper.state_as_number(state)

View File

@ -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()