abcminiuser / python-elgato-streamdeck

Python library to control the Elgato Stream Deck.
Other
868 stars 128 forks source link

Use set_key_callback_async function - Help Wanted #109

Closed FroztOfficial closed 11 months ago

FroztOfficial commented 1 year ago

Info

Help wanted

Hey, so I just got the Elgato Stream Deck XL, but I have a hard time setting up the async version of the key callback function (set_key_callback_async).

So I was wondering if someone could help me make the following code use the set_key_callback_async function instead of the set_key_callback function?

#!/usr/bin/env python3
import threading
from StreamDeck.DeviceManager import DeviceManager

# Perform action when key is pressed
def key_change_callback(deck, key, state):
    # Print new key state
    print(f"Key {key} - {state}")

    # Actions
    if state == True and key == 31:
        deck.reset()
        deck.close()

def main():
    # Setup Stream Deck
    streamdecks = DeviceManager().enumerate()
    deck = streamdecks[0]
    deck.open()
    deck.reset()
    deck.set_brightness(30)

    # Register callback function for when a key state changes
    deck.set_key_callback(key_change_callback)

    # Close application when Stream Deck handles are closed
    for t in threading.enumerate():
        try:
            t.join()
        except RuntimeError:
            pass

if __name__ == '__main__':
    main()
abcminiuser commented 1 year ago

Hi @FroztOfficial - see https://github.com/abcminiuser/python-homeassistant-streamdeck/blob/master/src/HassClient.py for an example using the asyncio key callbacks. The set_key_callback_async() function needs to bind to an async def python function.