50 lines
911 B
Bash
Executable file
50 lines
911 B
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
URL="https://hass.hodgson.one"
|
|
TOKEN="$(pass websites/homeassistant-token)"
|
|
|
|
get(){
|
|
curl --silent --header "Authorization: Bearer $TOKEN" "$URL$1"
|
|
}
|
|
|
|
post(){
|
|
curl --silent --header "content-type: application/json" --header "Authorization: Bearer $TOKEN" --data-binary @- "$URL$1"
|
|
}
|
|
|
|
listEntities(){
|
|
get '/api/states' |
|
|
jq -r '.[] | [.entity_id, .attributes.friendly_name // .entity_id, .state] | @tsv'
|
|
}
|
|
|
|
listServices(){
|
|
get '/api/services'
|
|
}
|
|
|
|
selectEntity(){
|
|
listEntities | fzf | cut -d $'\t' -f 1
|
|
}
|
|
|
|
|
|
toggleEntity(){
|
|
local entity;
|
|
if [ -n "$1" ]; then
|
|
entity="$1"
|
|
else
|
|
entity="$(cat -)"
|
|
fi
|
|
|
|
local domain="${entity%%.*}"
|
|
|
|
echo "{\"entity_id\":\"$entity\"}" | post /api/services/$domain/toggle
|
|
|
|
}
|
|
|
|
if [ -n "$1" ]; then
|
|
case "$1" in
|
|
"--listEntities") listEntities ;;
|
|
"--listServices") listServices ;;
|
|
*) toggleEntity "$1"
|
|
esac
|
|
else
|
|
selectEntity | toggleEntity
|
|
fi
|