# Bluetooth Audio This repos supports incomming bt audio connections. It also sends the bt audio state via MQTT to smarthome.
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.

bt-audioparser.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import config
  2. import logging
  3. import mqtt
  4. import report
  5. import select
  6. from systemd import journal
  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('main')
  12. PLAY = "New A2DP transport"
  13. STOP = "Closing A2DP transport"
  14. def send_state_msg_mqtt(mc, state):
  15. topic = config.MQTT_TOPIC + "/state"
  16. logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
  17. mc.send(topic, "true" if state else "false")
  18. def send_title_msg_mqtt(mc, title):
  19. topic = config.MQTT_TOPIC + "/title"
  20. logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
  21. mc.send(topic, title)
  22. if __name__ == "__main__":
  23. report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
  24. mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
  25. j = journal.Reader()
  26. j.log_level(journal.LOG_INFO)
  27. j.add_match(_SYSTEMD_UNIT="bluealsa.service")
  28. j.seek_tail()
  29. j.get_next()
  30. playing = False
  31. send_state_msg_mqtt(mc, playing)
  32. while True:
  33. while j.get_next():
  34. for entry in j:
  35. if entry['MESSAGE'] != "":
  36. if playing:
  37. if STOP in entry['MESSAGE']:
  38. playing = False
  39. send_state_msg_mqtt(mc, playing)
  40. else:
  41. if PLAY in entry['MESSAGE']:
  42. playing = True
  43. send_state_msg_mqtt(mc, playing)
  44. logger.debug(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])