diff --git a/spotify.py b/spotify.py index ad472eb..13d2a6d 100644 --- a/spotify.py +++ b/spotify.py @@ -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) - 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") + 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) - 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) - + 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__':