feat: Add 'up X' and 'down X' commands to adjust media player volume

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

47
main.py
View File

@@ -234,6 +234,45 @@ async def send_media_command(command):
logging.warning("No active media player found to send command to.") logging.warning("No active media player found to send command to.")
async def adjust_volume(direction, amount):
"""Adjusts the volume of the media player."""
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)
manager_iface = manager_obj.get_interface('org.freedesktop.DBus.ObjectManager')
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...")
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')
current_volume_variant = await player_props.call_get(MEDIA_PLAYER_IFACE, 'Volume')
current_volume = current_volume_variant.value
logging.info(f"Current volume is {current_volume} (0-127)")
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.
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))
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.")
async def process_bluetooth_command(topic, text): async def process_bluetooth_command(topic, text):
global pairing_task global pairing_task
logging.info('Bluetooth command: %s', text) logging.info('Bluetooth command: %s', text)
@@ -247,6 +286,14 @@ async def process_bluetooth_command(topic, text):
elif text in ["play", "pause", "next", "prev"]: elif text in ["play", "pause", "next", "prev"]:
command = "previous" if text == "prev" else text command = "previous" if text == "prev" else text
await send_media_command(command) await send_media_command(command)
elif text.startswith("up ") or text.startswith("down "):
parts = text.split()
if len(parts) == 2 and parts[1].isdigit():
direction = parts[0]
amount = int(parts[1])
await adjust_volume(direction, amount)
else:
logging.warning(f"Invalid volume command format: {text}")
async def process_mqtt(message): async def process_mqtt(message):
text = message.payload.decode() text = message.payload.decode()