aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/scene
blob: f8ea9532657bf614e001143147b1c16defa32702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/sh

CACHE_FILE="$XDG_CACHE_HOME/hass-scenes"
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"
}

[ ! -e "$CACHE_FILE" -o "$1" = "update" ] && update_cache

entity="$(cat "$CACHE_FILE" |\
	rofi -dmenu |\
	sed 's/^.*(\(.*\))$/\1/' |\
	jq --raw-input)"
jq -n --argjson entity "$entity" '{"entity_id": $entity}' |\
	curl -s -X POST -d @- \
		-H "Authorization: Bearer $HASS_TOKEN" \
		-H "Content-Type: application/json" \
		"$HASS_SERVER/api/services/scene/turn_on" > /dev/null