Created
March 28, 2025 15:44
-
-
Save zrzka/38e43b242fb8aa5af7fced4682d90781 to your computer and use it in GitHub Desktop.
A simple script to control the Elgato Key Light
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
set -e | |
set -u | |
set -o pipefail | |
WIFI=$(nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d':' -f2) | |
if [[ "${WIFI}" != "WTF" ]]; then | |
echo "Not in the office, WiFi network ${WIFI}" | |
exit 0 | |
fi | |
# Use "avahi-browse -arplt" to list & resolve devices on your local | |
# network. | |
HOSTNAME=elgato-key-light-4853.local | |
URL="http://${HOSTNAME}:9123/elgato/lights" | |
TIMEOUT=5 | |
# What to do by default | |
on_or_off=1 | |
brightness=15 | |
temperature=249 | |
# Parse arguments and update what to do | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
# One-off & exit | |
s|status) curl "${URL}" --header "Accept: application/json" --max-time ${TIMEOUT}; exit 0 ;; | |
# Tweak parameters | |
1|on) on_or_off=1 ;; | |
0|off) on_or_off=0 ;; | |
-b|--brightness) brightness="$2"; shift ;; | |
-t|--temperature) temperature="$2"; shift ;; | |
*) echo "No idea: $1"; exit 1 ;; | |
esac | |
shift | |
done | |
# State looks like this: | |
# | |
# {"numberOfLights":1,"lights":[{"on":0,"brightness":66,"temperature":222}]} | |
# | |
# on = 0|1 | |
# brightness = 0..100 | |
# temperature = 143 - 344 (divide by 0.05 to get it in Kelvins) | |
# Get it -> modify it -> put it back | |
curl -s "${URL}" --header "Accept: application/json" --max-time ${TIMEOUT} | | |
jq ".lights[] |= (.on = $on_or_off | .brightness = $brightness | .temperature = $temperature)" | | |
curl -s -X PUT "${URL}" --header "Accept: application/json" -d @- -o /dev/null --max-time ${TIMEOUT} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment