Exception handling for MQTT

This commit is contained in:
Dirk Alders 2022-08-24 13:59:59 +01:00
parent d8096b0f31
commit 826bd78926

View File

@ -100,19 +100,24 @@ class librespot(object):
def send_state_msg_mqtt(state):
client= paho.Client("spotify")
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")
def send_title_msg_mqtt(title):
client= paho.Client("spotify")
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")
if __name__ == '__main__':