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
This commit is contained in:
Brent Hughes 2016-12-02 01:02:58 -06:00 committed by Paulus Schoutsen
parent 83a108b20a
commit 48fd8f1f63
2 changed files with 49 additions and 1 deletions

View File

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

View File

@ -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 = {