Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions homeassistant/components/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import voluptuous as vol

import re

from homeassistant.const import (
EVENT_STATE_CHANGED, STATE_UNAVAILABLE, STATE_UNKNOWN, CONF_HOST,
CONF_PORT, CONF_SSL, CONF_VERIFY_SSL, CONF_USERNAME, CONF_PASSWORD,
Expand Down Expand Up @@ -147,6 +149,8 @@ def influx_event_listener(event):
}
]

percent_remove_regex = re.compile(r'[\d.]+')

@MartinHjelmare MartinHjelmare Jun 5, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should call this something else now, right? non_digit_tail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! Cheers. Have updated that

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 @@ -161,6 +165,11 @@ def influx_event_listener(event):
except (ValueError, TypeError):
new_key = "{}_str".format(key)
json_body[0]['fields'][new_key] = str(value)
if percent_remove_regex.match(
json_body[0]['fields'][new_key]
):
json_body[0]['fields'][key] = float(
non_decimal.sub('', value))

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

Expand Down
19 changes: 16 additions & 3 deletions tests/components/test_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ def test_event_listener(self, mock_client):
attrs = {
'unit_of_measurement': 'foobars',
'longitude': '1.1',
'latitude': '2.2'
'latitude': '2.2',
'battery_level': '99%',
'temperature': '20c',
'last_seen': 'Last seen 23 minutes ago'
}
state = mock.MagicMock(
state=in_, domain='fake', object_id='entity', attributes=attrs)
Expand All @@ -136,7 +139,12 @@ def test_event_listener(self, mock_client):
'fields': {
'state': out,
'longitude': 1.1,
'latitude': 2.2
'latitude': 2.2,
'battery_level_str': '99%',
'battery_level': 99.0,
'temperature_str': '20c',
'temperature': 20.0,
'last_seen_str': 'Last seen 23 minutes ago'
},
}]

Expand All @@ -151,7 +159,12 @@ def test_event_listener(self, mock_client):
'fields': {
'value': out,
'longitude': 1.1,
'latitude': 2.2
'latitude': 2.2,
'battery_level_str': '99%',
'battery_level': 99.0,
'temperature_str': '20c',
'temperature': 20.0,
'last_seen_str': 'Last seen 23 minutes ago'
},
}]
self.handler_method(event)
Expand Down