diff options
author | Tom Smeding <tom@tomsmeding.com> | 2025-02-02 22:05:59 +0100 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2025-02-02 22:05:59 +0100 |
commit | 6b2a60ee925d62ee53be9c2b064cca12d01dbdd6 (patch) | |
tree | 21cfe8297bf179ae5511f9240349d3e22a7c7e45 /set.sh |
Diffstat (limited to 'set.sh')
-rwxr-xr-x | set.sh | 64 |
1 files changed, 64 insertions, 0 deletions
@@ -0,0 +1,64 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$(realpath "$0")")" + +source functions.sh + +read_vars + +if [[ ! -f lights.txt ]]; then + echo >&2 'First run:' + echo >&2 '$ ./get_lights.sh >lights.txt' + exit 1 +fi + +if [[ $# -eq 0 || ($1 = "-h" || $1 = "--help") ]]; then + echo >&2 "Usage: $0 <light name> [commands...]" + echo >&2 "The light name is case-insensitive and may be any unambiguous substring of the full name." + echo >&2 "Commands:" + echo >&2 "- on: turn on the light" + echo >&2 "- off: turn off the light" + echo >&2 "- switch <n>: turn on if n=1, off if n=0" + echo >&2 "- br <n>: set brightness; n in [0, 100]" + [[ $# -eq 0 ]] && exit 1 + exit 0 +fi + +namesubstr=$1 + +matches=$(cut -d' ' -f2- lights.txt | grep -niF "$namesubstr" | cut -d: -f1 || true) +[[ -z $matches ]] && { echo >&2 "Light not found"; exit 1; } +[[ $(wc -l <<<"$matches") -gt 1 ]] && { echo >&2 "Multiple lights matched, name must be unambiguous"; exit 1; } +lightid=$(sed -n "$matches s/ .*//p" <lights.txt) + +shift + +commandstr="{}" +while [[ $# -gt 0 ]]; do + case "$1" in + on) commandstr="$commandstr * {on:{on:true}}"; shift ;; + off) commandstr="$commandstr * {on:{on:false}}"; shift ;; + + switch) + [[ $# -lt 2 ]] && { echo >&2 "Not enough arguments to 'switch' command"; exit 1; } + if [[ $2 = '1' ]]; then commandstr="$commandstr * {on:{on:true}}" + elif [[ $2 = '0' ]]; then commandstr="$commandstr * {on:{on:false}}" + else echo >&2 "Invalid argument to 'switch' command"; exit 1 + fi + shift 2 + ;; + + br) + [[ $# -lt 2 ]] && { echo >&2 "Not enough arguments to 'br' command"; exit 1; } + commandstr="$commandstr * {dimming:{brightness:$2}}" + shift 2 + ;; + + *) + echo >&2 "Unrecognised command: '$1'" + exit 1 + esac +done + +hue_request resource/light/"$lightid" PUT "$(jq "$commandstr" <<<0)" >/dev/null |