summaryrefslogtreecommitdiff
path: root/functions.sh
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2025-02-02 22:05:59 +0100
committerTom Smeding <tom@tomsmeding.com>2025-02-02 22:05:59 +0100
commit6b2a60ee925d62ee53be9c2b064cca12d01dbdd6 (patch)
tree21cfe8297bf179ae5511f9240349d3e22a7c7e45 /functions.sh
InitialHEADmaster
Diffstat (limited to 'functions.sh')
-rw-r--r--functions.sh46
1 files changed, 46 insertions, 0 deletions
diff --git a/functions.sh b/functions.sh
new file mode 100644
index 0000000..2c21a41
--- /dev/null
+++ b/functions.sh
@@ -0,0 +1,46 @@
+# This file is meant to be `source`d.
+
+function read_vars() {
+ local key value
+ while read -r line; do
+ key=${line%%=*}
+ value=${line#*=}
+ if [[ $key = ip ]]; then bridge_ip=$value
+ elif [[ $key = bridgeid ]]; then bridge_id=$value
+ elif [[ $key = username ]]; then bridge_username=$value
+ fi
+ done <my_bridge.txt
+}
+
+# arguments:
+# - API v2 path: "resource/light" for /clip/v2/resource/light
+# - (optional) HTTP method (default GET)
+# - (optional) data to send as HTTP body
+function hue_request() {
+ local path=$1 method=GET body=''
+ [[ -z $path ]] && { echo >&2 "functions.sh(hue_request): path required"; exit 1; }
+ [[ $# -ge 2 ]] && method=$2
+ [[ $# -ge 3 ]] && body=$3
+ [[ $# -ge 4 ]] && { echo >&2 "functions.sh(hue_request): too many arguments"; exit 1; }
+
+ local data_args=()
+ if [[ -n $body ]]; then
+ data_args=( -d "$body" )
+ fi
+
+ json=$(
+ curl -s --cacert signify_root_cert.pem \
+ --resolve "$bridge_id:443:$bridge_ip" -H "hue-application-key: $bridge_username" \
+ -X "$method" "${data_args[@]}" \
+ https://"$bridge_id"/clip/v2/"$path"
+ )
+
+ errors=$(jq -r 'if has("errors") then .errors.[] | "- " + .description else empty end' <<<"$json")
+ if [[ -n $errors ]]; then
+ echo >&2 "Errors from Hue bridge:"
+ echo >&2 "$errors"
+ exit 1
+ fi
+
+ echo "$json"
+}