Load json for mqtt callbacks

This commit is contained in:
Sebastian Lohff 2019-08-11 01:13:03 +02:00
parent 41dfa6b262
commit 357906fcd1
1 changed files with 11 additions and 0 deletions

View File

@ -1,3 +1,9 @@
try:
import json
except ImportError:
import ujson as json
def sensor(cls): def sensor(cls):
Sensor.register(cls) Sensor.register(cls)
@ -60,6 +66,11 @@ class Sensor(object):
@classmethod @classmethod
def run_mqtt_callback(cls, mqtt_topic, data): def run_mqtt_callback(cls, mqtt_topic, data):
try:
data = json.loads(data)
except ValueError:
print("Could not parse data {}".format(data))
if mqtt_topic in cls._mqtt_callbacks: if mqtt_topic in cls._mqtt_callbacks:
cls._mqtt_callbacks[mqtt_topic](data) cls._mqtt_callbacks[mqtt_topic](data)
else: else: