From bb65a7c6b6b3bb5a8e40207467d2edc7c3f1cb0b Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Tue, 8 Jun 2021 07:55:11 +0200 Subject: [PATCH] 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 --- serial2mqtt.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/serial2mqtt.py b/serial2mqtt.py index 2a19e6e..3f17661 100755 --- a/serial2mqtt.py +++ b/serial2mqtt.py @@ -30,14 +30,15 @@ def parse_sensor_values(data): """ Input line: ,, Output structure: - data = { + { 'humidity': , 'temperature': , '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):