From 48fd8f1f6362839d217dfaf9f68e78b7240171b9 Mon Sep 17 00:00:00 2001 From: Brent Hughes Date: Fri, 2 Dec 2016 01:02:58 -0600 Subject: [PATCH] InfluxDB: Fixed attributes that are lists causing invalid syntax (#4642) * Fixed attributes that are lists cuasing invalid influx syntax * Added bool and fixed mixed data type issue * Fixed changing nearly all data types to float causing some worse influxdb errors. whoops * Added line to end of file --- homeassistant/components/influxdb.py | 6 +++- tests/components/test_influxdb.py | 44 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/influxdb.py b/homeassistant/components/influxdb.py index ebb4169f8e6..167767bc00e 100644 --- a/homeassistant/components/influxdb.py +++ b/homeassistant/components/influxdb.py @@ -120,7 +120,11 @@ def setup(hass, config): for key, value in state.attributes.items(): if key != 'unit_of_measurement': - json_body[0]['fields'][key] = value + if isinstance(value, (str, float, bool)): + json_body[0]['fields'][key] = value + elif isinstance(value, int): + # Prevent column data errors in influxDB. + json_body[0]['fields'][key] = float(value) json_body[0]['tags'].update(tags) diff --git a/tests/components/test_influxdb.py b/tests/components/test_influxdb.py index 1ddac909c63..f7536958283 100644 --- a/tests/components/test_influxdb.py +++ b/tests/components/test_influxdb.py @@ -257,6 +257,50 @@ class TestInfluxDB(unittest.TestCase): self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock() + def test_event_listener_invalid_type(self, mock_client): + """Test the event listener when an attirbute has an invalid type.""" + self._setup() + + valid = { + '1': 1, + '1.0': 1.0, + STATE_ON: 1, + STATE_OFF: 0, + 'foo': 'foo' + } + for in_, out in valid.items(): + attrs = { + 'unit_of_measurement': 'foobars', + 'longitude': '1.1', + 'latitude': '2.2', + 'invalid_attribute': ['value1', 'value2'] + } + state = mock.MagicMock( + state=in_, domain='fake', object_id='entity', attributes=attrs) + event = mock.MagicMock(data={'new_state': state}, time_fired=12345) + body = [{ + 'measurement': 'foobars', + 'tags': { + 'domain': 'fake', + 'entity_id': 'entity', + }, + 'time': 12345, + 'fields': { + 'value': out, + 'longitude': '1.1', + 'latitude': '2.2' + }, + }] + self.handler_method(event) + self.assertEqual( + mock_client.return_value.write_points.call_count, 1 + ) + self.assertEqual( + mock_client.return_value.write_points.call_args, + mock.call(body) + ) + mock_client.return_value.write_points.reset_mock() + def test_event_listener_default_measurement(self, mock_client): """Test the event listener with a default measurement.""" config = {