currently playing information added to mqtt

This commit is contained in:
Dirk Alders 2022-07-25 21:11:50 +02:00
parent 30e68a738d
commit b1a57836fb

32
mpd.py
View File

@ -16,17 +16,24 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('librespot')
class mpc(object): class mpc(object):
PLAYING_TXT = 'playing' PLAYING_TXT = 'playing'
def __init__(self, state_callback): def __init__(self, state_callback, title_callback):
logger.info("Starting MPD monitor...") logger.info("Starting MPD monitor...")
self.__state_callback__ = state_callback self.__state_callback__ = state_callback
self.__title_callback__ = title_callback
self.__state__ = None self.__state__ = None
self.__title__ = None
self.set_state(False) self.set_state(False)
self.set_title("")
def run(self): def run(self):
while True: while True:
output = subprocess.check_output(["mpc", "status"]) output = subprocess.check_output(["mpc", "status"])
out_txt = output.decode('UTF-8') out_txt = output.decode('UTF-8')
self.set_state(self.PLAYING_TXT in out_txt) self.set_state(self.PLAYING_TXT in out_txt)
if self.__state__:
self.set_title(out_txt.split("\n")[0])
else:
self.set_title("")
time.sleep(.2) time.sleep(.2)
def set_state(self, target_state): def set_state(self, target_state):
@ -35,18 +42,33 @@ class mpc(object):
logger.info('MPD state changed to %s', self.__state__) logger.info('MPD state changed to %s', self.__state__)
self.__state_callback__(self.__state__) self.__state_callback__(self.__state__)
def set_title(self, title):
if self.__title__ != title:
logger.info('MPD is playing "%s"', title)
self.__title_callback__(title)
self.__title__ = title
def send_msg_mqtt(state):
def send_state_msg_mqtt(state):
client= paho.Client("mpd") client= paho.Client("mpd")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883) client.connect(config.MQTT_SERVER, 1883)
logger.info("Sending MPD status information to mqtt %s = %s", config.MQTT_TOPIC, str(state)) topic = config.MQTT_TOPIC + "/state"
client.publish(config.MQTT_TOPIC, "true" if state else "false") logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
client.publish(topic, "true" if state else "false")
def send_title_msg_mqtt(title):
client= paho.Client("mpd")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883)
topic = config.MQTT_TOPIC + "/title"
logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
client.publish(topic, title)
if __name__ == '__main__': if __name__ == '__main__':
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT) report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
mpd = mpc(send_msg_mqtt) mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
mpd.run() mpd.run()
import paho.mqtt.client as paho import paho.mqtt.client as paho