Browse Source

Exception handling for MQTT

master
Dirk Alders 2 years ago
parent
commit
826bd78926
1 changed files with 14 additions and 9 deletions
  1. 14
    9
      spotify.py

+ 14
- 9
spotify.py View File

100
 def send_state_msg_mqtt(state):
100
 def send_state_msg_mqtt(state):
101
     client= paho.Client("spotify")
101
     client= paho.Client("spotify")
102
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
102
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
103
-    client.connect(config.MQTT_SERVER, 1883)
104
-    topic = config.MQTT_TOPIC + "/state"
105
-    logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
106
-    client.publish(topic, "true" if state else "false")
103
+    try:
104
+        client.connect(config.MQTT_SERVER, 1883)
105
+        topic = config.MQTT_TOPIC + "/state"
106
+        logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
107
+        client.publish(topic, "true" if state else "false")
108
+    except (socket.timeout, OSError) as e:
109
+        logger.warning("Erro while sending state information")
107
 
110
 
108
 def send_title_msg_mqtt(title):
111
 def send_title_msg_mqtt(title):
109
     client= paho.Client("spotify")
112
     client= paho.Client("spotify")
110
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
113
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
111
-    client.connect(config.MQTT_SERVER, 1883)
112
-    topic = config.MQTT_TOPIC + "/title"
113
-    logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
114
-    client.publish(topic, title)
115
-
114
+    try:
115
+        client.connect(config.MQTT_SERVER, 1883)
116
+        topic = config.MQTT_TOPIC + "/title"
117
+        logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
118
+        client.publish(topic, title)
119
+    except (socket.timeout, OSError) as e:
120
+        logger.warning("Erro while sending title information")
116
 
121
 
117
 
122
 
118
 if __name__ == '__main__': 
123
 if __name__ == '__main__': 

Loading…
Cancel
Save