Browse Source

currently playing information added to mqtt

master
Dirk Alders 2 years ago
parent
commit
b1a57836fb
1 changed files with 27 additions and 5 deletions
  1. 27
    5
      mpd.py

+ 27
- 5
mpd.py View File

16
 class mpc(object):
16
 class mpc(object):
17
     PLAYING_TXT = 'playing'
17
     PLAYING_TXT = 'playing'
18
 
18
 
19
-    def __init__(self, state_callback):
19
+    def __init__(self, state_callback, title_callback):
20
         logger.info("Starting MPD monitor...")
20
         logger.info("Starting MPD monitor...")
21
         self.__state_callback__ = state_callback
21
         self.__state_callback__ = state_callback
22
+        self.__title_callback__ = title_callback
22
         self.__state__ = None
23
         self.__state__ = None
24
+        self.__title__ = None
23
         self.set_state(False)
25
         self.set_state(False)
26
+        self.set_title("")
24
 
27
 
25
     def run(self):
28
     def run(self):
26
         while True:
29
         while True:
27
             output = subprocess.check_output(["mpc", "status"])
30
             output = subprocess.check_output(["mpc", "status"])
28
             out_txt = output.decode('UTF-8')
31
             out_txt = output.decode('UTF-8')
29
             self.set_state(self.PLAYING_TXT in out_txt)
32
             self.set_state(self.PLAYING_TXT in out_txt)
33
+            if self.__state__:
34
+                self.set_title(out_txt.split("\n")[0])
35
+            else:
36
+                self.set_title("")
30
             time.sleep(.2)
37
             time.sleep(.2)
31
 
38
 
32
     def set_state(self, target_state):
39
     def set_state(self, target_state):
35
             logger.info('MPD state changed to %s', self.__state__)
42
             logger.info('MPD state changed to %s', self.__state__)
36
             self.__state_callback__(self.__state__)
43
             self.__state_callback__(self.__state__)
37
 
44
 
45
+    def set_title(self, title):
46
+        if self.__title__ != title:
47
+            logger.info('MPD is playing "%s"', title)
48
+            self.__title_callback__(title)
49
+        self.__title__ = title
38
 
50
 
39
-def send_msg_mqtt(state):
51
+
52
+def send_state_msg_mqtt(state):
53
+    client= paho.Client("mpd")
54
+    client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
55
+    client.connect(config.MQTT_SERVER, 1883)
56
+    topic = config.MQTT_TOPIC + "/state"
57
+    logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
58
+    client.publish(topic, "true" if state else "false")
59
+
60
+def send_title_msg_mqtt(title):
40
     client= paho.Client("mpd")
61
     client= paho.Client("mpd")
41
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
62
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
42
     client.connect(config.MQTT_SERVER, 1883)
63
     client.connect(config.MQTT_SERVER, 1883)
43
-    logger.info("Sending MPD status information to mqtt %s = %s", config.MQTT_TOPIC, str(state))
44
-    client.publish(config.MQTT_TOPIC, "true" if state else "false")
64
+    topic = config.MQTT_TOPIC + "/title"
65
+    logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
66
+    client.publish(topic, title)
45
 
67
 
46
 
68
 
47
 if __name__ == '__main__': 
69
 if __name__ == '__main__': 
48
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
70
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
49
     
71
     
50
-    mpd = mpc(send_msg_mqtt)
72
+    mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
51
     mpd.run()
73
     mpd.run()
52
 import paho.mqtt.client as paho
74
 import paho.mqtt.client as paho

Loading…
Cancel
Save