8000 Update InfluxDB to handle datetime objects and multiple decimal points by philhawthorne · Pull Request #8080 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update InfluxDB to handle datetime objects and multiple decimal points #8080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 20, 2017
13 changes: 8 additions & 5 deletions homeassistant/components/influxdb.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
}),
}, extra=vol.ALLOW_EXTRA)

RE_DIGIT_TAIL = re.compile(r'^[^\.]*\d+\.?\d+[^\.]*$')
RE_DECIMAL = re.compile(r'[^\d.]+')


def setup(hass, config):
"""Set up the InfluxDB component."""
Expand Down Expand Up @@ -149,8 +152,6 @@ def influx_event_listener(event):
}
]

non_digit_tail = re.compile(r'[\d.]+')
non_decimal = re.compile(r'[^\d.]+')
for key, value in state.attributes.items():
if key != 'unit_of_measurement':
# If the key is already in fields
Expand All @@ -164,10 +165,12 @@ def influx_event_listener(event):
json_body[0]['fields'][key] = float(value)
except (ValueError, TypeError):
new_key = "{}_str".format(key)
json_body[0]['fields'][new_key] = str(value)
if non_digit_tail.match(json_body[0]['fields'][new_key]):
new_value = str(value)
json_body[0]['fields'][new_key] = new_value

if RE_DIGIT_TAIL.match(new_value):
json_body[0]['fields'][key] = float(
non_decimal.sub('', value))
RE_DECIMAL.sub('', new_value))

json_body[0]['tags'].update(tags)

Expand Down
17 changes: 14 additions & 3 deletions tests/components/test_influxdb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""The tests for the InfluxDB component."""
import unittest
import datetime
from unittest import mock

import influxdb as influx_client
Expand Down Expand Up @@ -123,7 +124,9 @@ def test_event_listener(self, mock_client):
'latitude': '2.2',
'battery_level': '99%',
'temperature': '20c',
'last_seen': 'Last seen 23 minutes ago'
'last_seen': 'Last seen 23 minutes ago',
'updated_at': datetime.datetime(2017, 1, 1, 0, 0),
'multi_periods': '0.120.240.2023873'
}
state = mock.MagicMock(
state=in_, domain='fake', object_id='entity', attributes=attrs)
Expand All @@ -144,7 +147,11 @@ def test_event_listener(self, mock_client):
'battery_level': 99.0,
'temperature_str': '20c',
'temperature': 20.0,
'last_seen_str': 'Last seen 23 minutes ago'
'last_seen_str': 'Last seen 23 minutes ago',
'last_seen': 23.0,
'updated_at_str': '2017-01-01 00:00:00',
'updated_at': 20170101000000,
'multi_periods_str': '0.120.240.2023873'
},
}]

Expand All @@ -164,7 +171,11 @@ def test_event_listener(self, mock_client):
'battery_level': 99.0,
'temperature_str': '20c',
'temperature': 20.0,
'last_seen_str': 'Last seen 23 minutes ago'
'last_seen_str': 'Last seen 23 minutes ago',
'last_seen': 23.0,
'updated_at_str': '2017-01-01 00:00:00',
'updated_at': 20170101000000,
'multi_periods_str': '0.120.240.2023873'
},
}]
self.handler_method(event)
Expand Down
0