Keyboard volume control

ioioio
Posts: 7
Joined: Sun Jan 04, 2026 9:53 pm

Re: Keyboard volume control

Post by ioioio »

hudiy wrote: Mon Sep 08, 2025 6:23 pm If you already have a working script that communicates with Hudiy via the API, then you just need to call (depending on the signal you expect from the device):

Code: Select all

def trigger_action(self, client, action):
    dispatch_action = hudiy_api.DispatchAction()
    dispatch_action.action = action
    client.send(hudiy_api.MESSAGE_DISPATCH_ACTION, 0,
                dispatch_action.SerializeToString())
                
self.trigger_action(client, "output_volume_up")
self.trigger_action(client, "output_volume_down")
self.trigger_action(client, "toggle_output_muted")
Just like in the example.

For your use case, action can be:
  • output_volume_up
  • output_volume_down
  • toggle_output_muted
https://github.com/wiboma/hudiy/blob/ma ... ed-actions

You can share your current script, and we’ll try to modify/refactor it if you’d like.
Hallo would you mind provide a simple example (full working python file) that will trigger a volume change, something as basic as possible, as I do not seem to be able to get started, thanks
hudiy
Site Admin
Posts: 440
Joined: Mon Jul 14, 2025 7:42 pm

Re: Keyboard volume control

Post by hudiy »

To control the volume, you need to use the DispatchAction message from the API and dispatch output_volume_up/output_volume_down actions.

Docs for DispatchAction:
https://github.com/wiboma/hudiy/blob/ma ... proto#L723

List of predefined actions:
https://github.com/wiboma/hudiy/blob/ma ... ed-actions

This script dispatches output_volume_up/output_volume_down every 5 seconds. Just checkout the Hudiy repo and save below code to e. g. Volume.py file in https://github.com/wiboma/hudiy/tree/ma ... api/python directory.

You need to install python3-protobuf and python3-websocket dependencies to use the API (sudo apt install -y python3-protobuf python3-websocket)

Code: Select all

#
#  Copyright (C) Hudiy Project - All Rights Reserved
#

import threading
import common.Api_pb2 as hudiy_api
from common.Client import Client, ClientEventHandler


class EventHandler(ClientEventHandler):

    def __init__(self):
        self._volume_up = False
        self._timer = None

    def on_hello_response(self, client, message):
        print(
            "received hello response, result: {}, app version: {}.{}, api version: {}.{}"
            .format(message.result, message.app_version.major,
                    message.app_version.minor, message.api_version.major,
                    message.api_version.minor))

        self.control_volume(client)

    def control_volume(self, client):
        self._volume_up = not self._volume_up

        dispatch_action = hudiy_api.DispatchAction()
        dispatch_action.action = "output_volume_up" if self._volume_up else "output_volume_down"
        client.send(hudiy_api.MESSAGE_DISPATCH_ACTION, 0,
                    dispatch_action.SerializeToString())

        self._timer = threading.Timer(5, self.control_volume, [client])
        self._timer.start()

    def get_timer(self):
        return self._timer


def main():
    client = Client("Volume")
    event_handler = EventHandler()
    client.set_event_handler(event_handler)
    client.connect('127.0.0.1', 44405)

    active = True
    while active:
        try:
            active = client.wait_for_message()
        except KeyboardInterrupt:
            break

    if event_handler.get_timer() is not None:
        event_handler.get_timer().cancel()

    client.disconnect()


if __name__ == "__main__":
    main()
ioioio
Posts: 7
Joined: Sun Jan 04, 2026 9:53 pm

Re: Keyboard volume control

Post by ioioio »

Thank you, I moved this part of the thread to the programming section viewtopic.php?t=170 in case somebody is interested
Post Reply