# Bluetooth Audio This repos supports incomming bt audio connections. It also sends the bt audio state via MQTT to smarthome.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

bt-audioparser.py 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import config
  2. import dbus
  3. import dbus.mainloop.glib
  4. from gi.repository import GLib
  5. import logging
  6. import mqtt
  7. import report
  8. import sys
  9. import time
  10. try:
  11. from config import APP_NAME as ROOT_LOGGER_NAME
  12. except ImportError:
  13. ROOT_LOGGER_NAME = 'root'
  14. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
  15. player_state = False
  16. current_title = ''
  17. def send_state_mqtt(state):
  18. topic = config.MQTT_TOPIC + "/state"
  19. logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
  20. mc.send(topic, "true" if state else "false")
  21. def send_title_mqtt(title):
  22. topic = config.MQTT_TOPIC + "/title"
  23. logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
  24. mc.send(topic, title)
  25. def on_property_changed(interface, changed, invalidated):
  26. global player_state
  27. global current_title
  28. #
  29. if interface != 'org.bluez.MediaPlayer1':
  30. return
  31. for prop, value in changed.items():
  32. if prop == 'Status':
  33. logger.debug("BT-PLAYER status changed to %s", repr(value))
  34. player_state = value == "playing"
  35. send_state_mqtt(player_state)
  36. send_title_mqtt(current_title if player_state else '')
  37. elif prop == 'Track':
  38. logger.debug("BT-PLAYER track information changed to %s", repr(value))
  39. if 'Title' in value:
  40. current_title = value.get('Title', '')
  41. if player_state:
  42. send_title_mqtt(current_title)
  43. def on_mqtt_ctrl(client, userdata, message):
  44. try:
  45. if message.topic == config.MQTT_TOPIC + "/PLAY":
  46. player_iface.Play()
  47. elif message.topic == config.MQTT_TOPIC + "/PAUSE":
  48. player_iface.Pause()
  49. elif message.topic == config.MQTT_TOPIC + "/TRACK_PREV":
  50. player_iface.Previous()
  51. elif message.topic == config.MQTT_TOPIC + "/TRACK_NEXT":
  52. player_iface.Next()
  53. else:
  54. logger.info(message.topic)
  55. except AttributeError:
  56. logger.warning("Player control impossible due to no player available.")
  57. if __name__ == "__main__":
  58. report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
  59. #
  60. mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
  61. mc.add_callback(config.MQTT_TOPIC + '/#', on_mqtt_ctrl)
  62. #
  63. send_state_mqtt(player_state)
  64. send_title_mqtt(current_title)
  65. #
  66. while True:
  67. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  68. bus = dbus.SystemBus()
  69. obj = bus.get_object('org.bluez', "/")
  70. mgr = dbus.Interface(obj, 'org.freedesktop.DBus.ObjectManager')
  71. player_iface = None
  72. transport_prop_iface = None
  73. for path, ifaces in mgr.GetManagedObjects().items():
  74. if 'org.bluez.MediaPlayer1' in ifaces:
  75. player_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.bluez.MediaPlayer1')
  76. elif 'org.bluez.MediaTransport1' in ifaces:
  77. transport_prop_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Properties')
  78. if player_iface and transport_prop_iface:
  79. break
  80. time.sleep(0.1)
  81. bus.add_signal_receiver(on_property_changed, bus_name='org.bluez', signal_name='PropertiesChanged', dbus_interface='org.freedesktop.DBus.Properties')
  82. GLib.MainLoop().run()