aboutsummaryrefslogtreecommitdiff
path: root/home/dot_local/share/bin/executable_scene
diff options
context:
space:
mode:
Diffstat (limited to 'home/dot_local/share/bin/executable_scene')
-rw-r--r--home/dot_local/share/bin/executable_scene56
1 files changed, 56 insertions, 0 deletions
diff --git a/home/dot_local/share/bin/executable_scene b/home/dot_local/share/bin/executable_scene
new file mode 100644
index 0000000..c7faff6
--- /dev/null
+++ b/home/dot_local/share/bin/executable_scene
@@ -0,0 +1,56 @@
+#!/bin/sh
+CACHE_FILE="$XDG_CACHE_HOME/hass-scenes"
+TAB="$(printf '\t')"
+
+update_cache() {
+ rm -f "$CACHE_FILE"
+ curl -s -X GET \
+ -H "Authorization: Bearer $HASS_TOKEN" \
+ -H "Content-Type: application/json" \
+ "$HASS_SERVER/api/states" |\
+ jq --raw-output '
+ .[] |
+ select(.entity_id | startswith("scene.")) |
+ "\(.attributes.friendly_name)\t\(.entity_id)"
+ ' > "$CACHE_FILE"
+}
+
+# update cache if it does not exist
+[ ! -e "$CACHE_FILE" ] && update_cache
+
+# update the cache if requested explicitly
+[ "$1" = "--update" ] && {
+ update_cache
+ exit
+}
+
+# update the cache if it is older than
+MAX_CACHE_AGE=$(( 60 * 60 * 24 )) # one day
+[ $(( $(date +%s) - $MAX_CACHE_AGE )) -gt $(date -r "$CACHE_FILE" +%s) ] && update_cache
+
+if [ $# -eq 0 ] ; then
+ # pick a scene using rofi in dmenu mode
+ scene="$(cat "$CACHE_FILE" |\
+ rofi \
+ -dmenu \
+ -display-columns 1 \
+ -display-column-separator "$TAB" |\
+ cut -d"$TAB" -f2 |\
+ jq --raw-input)"
+else
+ # non interactive scene pick
+ scene="$(cat "$CACHE_FILE" |\
+ grep -w "$*" | head -n1 |\
+ cut -d"$TAB" -f2 |\
+ jq --raw-input)"
+fi
+
+# exit if no scene was selected / found
+[ -z "$scene" ] && exit 0
+
+jq -n --argjson scene "$scene" '{"entity_id": $scene}' |\
+ curl -s -X POST -d @- \
+ -H "Authorization: Bearer $HASS_TOKEN" \
+ -H "Content-Type: application/json" \
+ "$HASS_SERVER/api/services/scene/turn_on" > /dev/null
+