fix: Adjust volume via MediaTransport1 interface

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2025-12-30 23:26:36 +00:00
parent 8bc8cf4089
commit ad31ed0f2e

24
main.py
View File

@@ -26,6 +26,7 @@ BLUEZ_SERVICE = 'org.bluez'
ADAPTER_IFACE = 'org.bluez.Adapter1'
DEVICE_IFACE = 'org.bluez.Device1'
MEDIA_PLAYER_IFACE = 'org.bluez.MediaPlayer1'
MEDIA_TRANSPORT_IFACE = 'org.bluez.MediaTransport1'
AGENT_IFACE = 'org.bluez.Agent1'
AGENT_MANAGER_IFACE = 'org.bluez.AgentManager1'
AGENT_PATH = '/io/bluetooth_speaker/agent'
@@ -235,7 +236,7 @@ async def send_media_command(command):
async def adjust_volume(direction, amount):
"""Adjusts the volume of the media player."""
"""Adjusts the volume of the media transport."""
logging.info(f"Attempting to adjust volume {direction} by {amount}")
introspection = await bus.introspect(BLUEZ_SERVICE, '/')
manager_obj = bus.get_proxy_object(BLUEZ_SERVICE, '/', introspection)
@@ -243,34 +244,35 @@ async def adjust_volume(direction, amount):
managed_objects = await manager_iface.call_get_managed_objects()
for path, ifaces in managed_objects.items():
if MEDIA_PLAYER_IFACE in ifaces:
logging.info(f"Found media player: {path}. Adjusting volume...")
if MEDIA_TRANSPORT_IFACE in ifaces:
logging.info(f"Found media transport: {path}. Adjusting volume...")
try:
player_introspection = await bus.introspect(BLUEZ_SERVICE, path)
player_obj = bus.get_proxy_object(BLUEZ_SERVICE, path, player_introspection)
player_props = player_obj.get_interface('org.freedesktop.DBus.Properties')
transport_introspection = await bus.introspect(BLUEZ_SERVICE, path)
transport_obj = bus.get_proxy_object(BLUEZ_SERVICE, path, transport_introspection)
transport_props = transport_obj.get_interface('org.freedesktop.DBus.Properties')
current_volume_variant = await player_props.call_get(MEDIA_PLAYER_IFACE, 'Volume')
current_volume_variant = await transport_props.call_get(MEDIA_TRANSPORT_IFACE, 'Volume')
current_volume = current_volume_variant.value
logging.info(f"Current volume is {current_volume} (0-127)")
logging.info(f"Current volume is {current_volume}")
if direction == 'up':
new_volume = current_volume + amount
else: # direction == 'down'
new_volume = current_volume - amount
# Volume is a uint8, but AVRCP standard is 0-127.
# The Volume on MediaTransport1 is a uint16, but AVRCP uses 0-127.
# We'll clamp to this range and hope BlueZ handles scaling.
new_volume = max(0, min(127, new_volume))
logging.info(f"Setting new volume to {new_volume}")
await player_props.call_set(MEDIA_PLAYER_IFACE, 'Volume', Variant('y', new_volume))
await transport_props.call_set(MEDIA_TRANSPORT_IFACE, 'Volume', Variant('q', new_volume))
logging.info(f"Successfully adjusted volume on {path}")
return
except Exception as e:
logging.error(f"Failed to adjust volume on {path}: {e}")
logging.warning("No active media player found to adjust volume.")
logging.warning("No active media transport found to adjust volume.")
async def process_bluetooth_command(topic, text):