Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.14k stars 587 forks source link

Spoon Suggestion: Elgato Key Light control #2896

Open garymh opened 3 years ago

garymh commented 3 years ago

This would be a great compliment to the already awesome Stream Deck module 😄

According to https://github.com/adamesch/elgato-key-light-api:

The Key Light devices do not natively support third-party integration, but they can be controlled by directly interfacing with the lights' built-in API.

It looks like this can be done with GET/POST requests?

cmsj commented 3 years ago

If it can be done with simple HTTP, it ought to be possible to whip up a Spoon that talks to the light with hs.http.

gwbrown commented 3 years ago

It can be done using simple HTTP, and discovery uses Bonjour (per this repo), so it should be possible to write a spoon that does both discovery (with hs.bonjour) and control (with hs.http).

I'll likely do so myself soon, and I'll post here if/when that happens.

fischa commented 3 years ago

I put a quick and dirty function with keybinding together to get a single elgato key light air to toggle on and off and keep the settings (brightness + temperature). URL is hardcoded so feel free to build on top of that:

local function toggle_key_light()
    local url = "http://elgato-key-light-air-1.local.:9123/elgato/lights"
    local status_code, response_body, response_header = hs.http.get(url)
    local response = hs.json.decode(response_body)
    --tprint(response)

    if response["lights"][1]["on"] == 0 then
        print("light is off: ", response["lights"][1]["on"])
        response["lights"][1]["on"] = 1
    else
        print("light is on: ", response["lights"][1]["on"])
        response["lights"][1]["on"] = 0
    end
    print("light is?: ", response["lights"][1]["on"])
    --tprint(response)
    print(hs.json.encode(response))
    hs.http.doRequest(url, "PUT", hs.json.encode(response))

end
hs.hotkey.bind({ "ctrl", "alt", "cmd" }, "l", "toggle key light", function() toggle_key_light() end)
evantravers commented 3 years ago

I've built a tiny spoon for it… all it does is turn it on and off: https://github.com/evantravers/hammerspoon-config/tree/master/Spoons/ElgatoKey.spoon