From 6b2a60ee925d62ee53be9c2b064cca12d01dbdd6 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 2 Feb 2025 22:05:59 +0100 Subject: Initial --- set.sh | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 set.sh (limited to 'set.sh') diff --git a/set.sh b/set.sh new file mode 100755 index 0000000..3cd3551 --- /dev/null +++ b/set.sh @@ -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 [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 : turn on if n=1, off if n=0" + echo >&2 "- br : 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" &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 -- cgit v1.2.3-70-g09d2