diff --git a/.gitmodules b/.gitmodules index 98f8bb5..9612d14 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/mqtt b/mqtt new file mode 160000 index 0000000..cf97fa0 --- /dev/null +++ b/mqtt @@ -0,0 +1 @@ +Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec diff --git a/spotify.py b/spotify.py index 752dd8d..7dab3de 100644 --- a/spotify.py +++ b/spotify.py @@ -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__':