diff options
Diffstat (limited to 'scripts/mqtt_listener.py')
-rw-r--r-- | scripts/mqtt_listener.py | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/scripts/mqtt_listener.py b/scripts/mqtt_listener.py index 981c40a..3ee688d 100644 --- a/scripts/mqtt_listener.py +++ b/scripts/mqtt_listener.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -''' Listen for mqtt-events, and trigger for some ''' +""" Listen for mqtt-events, and trigger for some """ import json import os @@ -8,41 +8,45 @@ from datetime import datetime import common import paho.mqtt.client as mqtt -mqtt_server = os.environ['el_mqtt_server'] -mqtt_port = int(os.environ['el_mqtt_port']) -keepalive = int(os.environ['el_mqtt_keepalive']) -mqtt_topic = os.environ['el_mqtt_topic'] -mqtt_user = os.environ['el_mqtt_user'] -mqtt_pass = os.environ['el_mqtt_pass'] - -tempsensors = [ 'Bad Temp', - 'Barnerom Temp', - 'Isobod Temp', - 'Kjøkken Temp Matskap', - 'Kontor Temp', - 'Loft Temp', - 'Soverom Temp', - 'Stue Temp Display', - 'Stue Temp Stuebord', - 'Stue Temp Teleskap', - 'Toalett motion', - 'Utebod Temp', - 'Vaskerom Temp'] +mqtt_server = os.environ["el_mqtt_server"] +mqtt_port = int(os.environ["el_mqtt_port"]) +keepalive = int(os.environ["el_mqtt_keepalive"]) +mqtt_topic = os.environ["el_mqtt_topic"] +mqtt_user = os.environ["el_mqtt_user"] +mqtt_pass = os.environ["el_mqtt_pass"] + +tempsensors = [ + "Bad Temp", + "Barnerom Temp", + "Isobod Temp", + "Kjøkken Temp Matskap", + "Kontor Temp", + "Loft Temp", + "Soverom Temp", + "Stue Temp Display", + "Stue Temp Stuebord", + "Stue Temp Teleskap", + "Toalett motion", + "Utebod Temp", + "Vaskerom Temp", +] + # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): - print("Connected with result code "+str(rc)) + print("Connected with result code " + str(rc)) # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe(mqtt_topic) + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): - name = msg.topic.split('/')[1] + name = msg.topic.split("/")[1] data = json.loads(msg.payload) - if name == 'HAN' and 'current' in data: + if name == "HAN" and "current" in data: sql = """INSERT INTO mqtt_han (name, current, @@ -51,21 +55,21 @@ def on_message(client, userdata, msg): linkquality, time) VALUES(%s,%s,%s,%s,%s,%s)""" - values = (name, - data['current'], - data['power'], - data['voltage'], - data['linkquality'], - datetime.utcnow()) + values = ( + name, + data["current"], + data["power"], + data["voltage"], + data["linkquality"], + datetime.utcnow(), + ) common.dbi(sql, values, verbose=True) if name in tempsensors: - if 'temperature' in data: - common.statein(name, data['temperature'], 'temperature', '°C', verbose=True) - if 'humidity' in data: - common.statein(name, data['humidity'], 'humidity', '%', verbose=True) - - + if "temperature" in data: + common.statein(name, data["temperature"], "temperature", "°C", verbose=True) + if "humidity" in data: + common.statein(name, data["humidity"], "humidity", "%", verbose=True) # mqtt |