Module mqtt implemented

This commit is contained in:
Dirk Alders 2022-09-13 06:15:49 +01:00
parent 62ac64ddfc
commit 5140a7c51f
3 changed files with 13 additions and 20 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

View File

@ -1,6 +1,6 @@
import config
import logging
import paho.mqtt.client as paho
import mqtt
import report
import socket
import subprocess
@ -14,6 +14,8 @@ except ImportError:
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('librespot')
mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
class mpc(object):
PLAYING_TXT = 'playing'
@ -51,27 +53,15 @@ class mpc(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 MPD 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 mpd state information")
topic = config.MQTT_TOPIC + "/state"
logger.info("Sending MPD 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 MPD title information to mqtt %s = \"%s\"", topic, title)
client.publish(topic, title)
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending mpd title information")
topic = config.MQTT_TOPIC + "/title"
logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
mc.send(topic, title)
if __name__ == '__main__':
@ -79,4 +69,3 @@ if __name__ == '__main__':
mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
mpd.run()
import paho.mqtt.client as paho

1
mqtt Submodule

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