From 4d5e9f2931660af156a44619a50ed5e93d165737 Mon Sep 17 00:00:00 2001 From: bestlibre Date: Thu, 3 Mar 2016 11:09:37 +0100 Subject: [PATCH 1/2] Possibility to blacklist events Possibility to blacklist some events based on the entity_id --- homeassistant/components/influxdb.py | 4 ++++ tests/components/test_influx.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/homeassistant/components/influxdb.py b/homeassistant/components/influxdb.py index 749dd4bd097..8c7e8c26f5b 100644 --- a/homeassistant/components/influxdb.py +++ b/homeassistant/components/influxdb.py @@ -33,6 +33,7 @@ CONF_SSL = 'ssl' CONF_VERIFY_SSL = 'verify_ssl' +# pylint: disable=too-many-locals def setup(hass, config): """Setup the InfluxDB component.""" from influxdb import InfluxDBClient, exceptions @@ -52,6 +53,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('blacklist', []) try: influx = InfluxDBClient(host=host, port=port, username=username, @@ -69,6 +71,8 @@ def setup(hass, config): state = event.data.get('new_state') if state is None or state.state in (STATE_UNKNOWN, ''): return + if state.entity_id in blacklist: + return try: _state = state_helper.state_as_number(state) diff --git a/tests/components/test_influx.py b/tests/components/test_influx.py index d9bc00b4f60..aa160b69ac4 100644 --- a/tests/components/test_influx.py +++ b/tests/components/test_influx.py @@ -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() From d7094b996a71cc16920819938054596a3f0b3cf0 Mon Sep 17 00:00:00 2001 From: bestlibre Date: Fri, 4 Mar 2016 23:32:24 +0100 Subject: [PATCH 2/2] Use global variable and merge two if in one --- homeassistant/components/influxdb.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/influxdb.py b/homeassistant/components/influxdb.py index 8c7e8c26f5b..082430903d0 100644 --- a/homeassistant/components/influxdb.py +++ b/homeassistant/components/influxdb.py @@ -31,6 +31,7 @@ CONF_USERNAME = 'username' CONF_PASSWORD = 'password' CONF_SSL = 'ssl' CONF_VERIFY_SSL = 'verify_ssl' +CONF_BLACKLIST = 'blacklist' # pylint: disable=too-many-locals @@ -53,7 +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('blacklist', []) + blacklist = conf.get(CONF_BLACKLIST, []) try: influx = InfluxDBClient(host=host, port=port, username=username, @@ -69,9 +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, ''): - return - if state.entity_id in blacklist: + if state is None or state.state in (STATE_UNKNOWN, '') \ + or state.entity_id in blacklist: return try: