diff options
Diffstat (limited to 'set.sh')
-rwxr-xr-x | set.sh | 87 |
1 files changed, 52 insertions, 35 deletions
@@ -14,7 +14,10 @@ if [[ ! -f lights.txt ]]; then fi if [[ $# -eq 0 || ($1 = "-h" || $1 = "--help") ]]; then - echo >&2 "Usage: $0 <light name> [commands...]" + echo >&2 "Usage:" + echo >&2 " $0 -s <scene>" + echo >&2 " $0 -off" + echo >&2 " $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" @@ -25,44 +28,58 @@ if [[ $# -eq 0 || ($1 = "-h" || $1 = "--help") ]]; then exit 0 fi -namesubstr=$1 +if [[ $1 = "-s" ]]; then + namesubstr=$2 + scenes=$(hue_request resource/scene | jq -r '.data.[] | .id + " " + .metadata.name') + matches=$(cut -d' ' -f2- <<<"$scenes" | grep -niF "$namesubstr" | cut -d: -f1 || true) + [[ $(wc -l <<<"$matches") -gt 1 ]] && { echo >&2 "Multiple scenes matched, name must be unambiguous"; exit 1; } + sceneid=$(sed -n "$matches s/ .*//p" <<<"$scenes") + hue_request resource/scene/"$sceneid" PUT '{"recall":{"action":"active"}}' >/dev/null -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) +elif [[ $1 = "-off" ]]; then + roomid=$(hue_request resource/room | jq -r '.data[0].services.[] | select(.rtype == "grouped_light").rid') + hue_request resource/grouped_light/"$roomid" PUT '{"on":{"on":false}}' >/dev/null -shift +else + namesubstr=$1 -commandstr="{}" -while [[ $# -gt 0 ]]; do - case "$1" in - on) commandstr="$commandstr * {on:{on:true}}"; shift ;; - off) commandstr="$commandstr * {on:{on:false}}"; shift ;; + 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) - 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 - ;; + shift - br) - [[ $# -lt 2 ]] && { echo >&2 "Not enough arguments to 'br' command"; exit 1; } - if [[ $2 -gt 0 ]]; then - commandstr="$commandstr * {on:{on:true}, dimming:{brightness:$2}}" - else - commandstr="$commandstr * {on:{on:false}}" - fi - shift 2 - ;; + commandstr="{}" + while [[ $# -gt 0 ]]; do + case "$1" in + on) commandstr="$commandstr * {on:{on:true}}"; shift ;; + off) commandstr="$commandstr * {on:{on:false}}"; shift ;; - *) - echo >&2 "Unrecognised command: '$1'" - exit 1 - esac -done + 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 + ;; -hue_request resource/light/"$lightid" PUT "$(jq "$commandstr" <<<0)" >/dev/null + br) + [[ $# -lt 2 ]] && { echo >&2 "Not enough arguments to 'br' command"; exit 1; } + if [[ $2 -gt 0 ]]; then + commandstr="$commandstr * {on:{on:true}, dimming:{brightness:$2}}" + else + commandstr="$commandstr * {on:{on:false}}" + fi + shift 2 + ;; + + *) + echo >&2 "Unrecognised command: '$1'" + exit 1 + esac + done + + hue_request resource/light/"$lightid" PUT "$(jq "$commandstr" <<<0)" >/dev/null +fi |