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 authored and Pascal Vizeli committed 2017-06-14 00:42:55 +02:00
1 parent 09fec29537
commit 1ddcab5e26
2 files changed
+23 -3

No files matched your search

+16 -3
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)