Module mpd_state -> mqtt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import config
  2. import logging
  3. import paho.mqtt.client as paho
  4. import report
  5. import subprocess
  6. import time
  7. try:
  8. from config import APP_NAME as ROOT_LOGGER_NAME
  9. except ImportError:
  10. ROOT_LOGGER_NAME = 'root'
  11. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('librespot')
  12. class mpc(object):
  13. PLAYING_TXT = 'playing'
  14. def __init__(self, state_callback):
  15. logger.info("Starting MPD monitor...")
  16. self.__state_callback__ = state_callback
  17. self.__state__ = None
  18. self.set_state(False)
  19. def run(self):
  20. while True:
  21. output = subprocess.check_output(["mpc", "status"])
  22. out_txt = output.decode('UTF-8')
  23. self.set_state(self.PLAYING_TXT in out_txt)
  24. time.sleep(.2)
  25. def set_state(self, target_state):
  26. if self.__state__ != target_state:
  27. self.__state__ = target_state
  28. logger.info('MPD state changed to %s', self.__state__)
  29. self.__state_callback__(self.__state__)
  30. def send_msg_mqtt(state):
  31. client= paho.Client("mpd")
  32. client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
  33. client.connect(config.MQTT_SERVER, 1883)
  34. logger.info("Sending MPD status information to mqtt %s = %s", config.MQTT_TOPIC, str(state))
  35. client.publish(config.MQTT_TOPIC, "true" if state else "false")
  36. if __name__ == '__main__':
  37. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  38. mpd = mpc(send_msg_mqtt)
  39. mpd.run()
  40. import paho.mqtt.client as paho