diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-03-02 13:15:10 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-03-02 13:15:10 +0100 |
commit | b97262217965d93d3f78951d64a5b0cd40f7a3cd (patch) | |
tree | 8943c5b1de9a0341db4da34916446c6b23a32f67 | |
parent | 8daf66550149a80a623abdae9eca09c42588eede (diff) |
improve rofi hass scene script
-rw-r--r-- | .config/rofi/config.rasi##template | 4 | ||||
-rwxr-xr-x | .local/share/bin/scene | 40 |
2 files changed, 36 insertions, 8 deletions
diff --git a/.config/rofi/config.rasi##template b/.config/rofi/config.rasi##template index bbbb646..680326f 100644 --- a/.config/rofi/config.rasi##template +++ b/.config/rofi/config.rasi##template @@ -64,3 +64,7 @@ element-text { background-color: inherit; text-color: inherit; } + +prompt { + enabled: false; +} diff --git a/.local/share/bin/scene b/.local/share/bin/scene index f8ea953..3a140fa 100755 --- a/.local/share/bin/scene +++ b/.local/share/bin/scene @@ -1,24 +1,48 @@ #!/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 ".[] | \"\\(.attributes.friendly_name) (\\(.entity_id))\"" |\ - grep '(scene\..*)$' > "$CACHE_FILE" + 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 } -[ ! -e "$CACHE_FILE" -o "$1" = "update" ] && update_cache +# 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 -entity="$(cat "$CACHE_FILE" |\ - rofi -dmenu |\ - sed 's/^.*(\(.*\))$/\1/' |\ +# 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)" -jq -n --argjson entity "$entity" '{"entity_id": $entity}' |\ + +# exit if no scene was selected +[ -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 + |