Make percentage string values as floats/ints in InfluxDB (#7879)

* Make percentage string values as floats in InfluxDB

Currently Z-wave and other compontents report an attributes battery
level as an integer, for example

```yaml
{
"is_awake": false,
"battery_level": 61,
}
```

However, some other components like Vera add the battery level as a
string

```yaml
{
"Vera Device Id": 25,
"device_armed": "False",
"battery_level": "63%",
"device_tripped": "False",
}
```

By removing any % signs in the field, this will send the value to
InfluxDB as an int, which can then be used to plot the data in graphs
correctly, like other percentage fields.

* Add tests and remove all trailing non digits

Adds tests and now removes all trailing non-numeric characters for
better use

* Update variable name for InfluxDB digit checks

Updates the variable used for the regex to remove trailing non digits

* Fix linting errors for InfluxDB component

Fixes a small linting error on the InfluxDB component
This commit is contained in:
Phil Hawthorne 2017-06-14 08:42:55 +10:00 committed by Pascal Vizeli
parent 09fec29537
commit 1ddcab5e26
2 changed files with 23 additions and 3 deletions

View File

@ -6,6 +6,8 @@ https://home-assistant.io/components/influxdb/
"""
import logging
import re
import voluptuous as vol
from homeassistant.const import (
@ -147,6 +149,8 @@ def setup(hass, config):
}
]
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
@ -161,6 +165,9 @@ def setup(hass, config):
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]):
json_body[0]['fields'][key] = float(
non_decimal.sub('', value))
json_body[0]['tags'].update(tags)

View File

@ -120,7 +120,10 @@ class TestInfluxDB(unittest.TestCase):
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)
@ -136,7 +139,12 @@ class TestInfluxDB(unittest.TestCase):
'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'
},
}]
@ -151,7 +159,12 @@ class TestInfluxDB(unittest.TestCase):
'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)