Module mqtt implemented

This commit is contained in:
Dirk Alders 2022-09-13 06:20:31 +01:00
parent 89b113959d
commit a0984df2ab
3 changed files with 13 additions and 19 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "report"]
path = report
url = https://git.mount-mockery.de/pylib/report.git
[submodule "mqtt"]
path = mqtt
url = https://git.mount-mockery.de/pylib/mqtt.git

1
mqtt Submodule

@ -0,0 +1 @@
Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec

View File

@ -1,6 +1,6 @@
import config
import logging
import paho.mqtt.client as paho
import mqtt
import report
import socket
import subprocess
@ -18,6 +18,8 @@ except ImportError:
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
class librespot(object):
ON_CMD = ['play', ]
OFF_CMD = ['pause', 'stop', ]
@ -99,26 +101,14 @@ class librespot(object):
def send_state_msg_mqtt(state):
client= paho.Client(config.APP_NAME)
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
try:
client.connect(config.MQTT_SERVER, 1883)
topic = config.MQTT_TOPIC + "/state"
logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
client.publish(topic, "true" if state else "false")
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending state information")
topic = config.MQTT_TOPIC + "/state"
logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
mc.send(topic, "true" if state else "false")
def send_title_msg_mqtt(title):
client= paho.Client(config.APP_NAME)
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
try:
client.connect(config.MQTT_SERVER, 1883)
topic = config.MQTT_TOPIC + "/title"
logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
client.publish(topic, title)
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending title information")
topic = config.MQTT_TOPIC + "/title"
logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
mc.send(topic, title)
if __name__ == '__main__':