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