Improve parsing when missing values

The sound module may not be available so the parse function fails. This
commit makes the parsing function more generic by giving a list of
metrics to retreive in order.

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-06-08 07:55:11 +02:00
parent 53758081d8
commit bb65a7c6b6
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7

View file

@ -30,14 +30,15 @@ def parse_sensor_values(data):
"""
Input line: <humidity>,<temperature>,<sound>
Output structure:
data = {
{
'humidity': <humidity>,
'temperature': <temperature>,
'sound': <sound>
}
"""
humidity, temperature, sound = data.decode('utf-8').strip().split(',')
return {'humidity': humidity, 'temperature': temperature, 'sound': sound}
metrics = ['humidity', 'temperature', 'sound']
values = data.decode('utf-8').strip().split(',')
return dict(zip(metrics, values))
def connect_mqtt(config):