Skip to content

Instantly share code, notes, and snippets.

@amaisano
Created April 27, 2023 14:25
Show Gist options
  • Save amaisano/054003f0bed771d0ad93acb0a1a3a614 to your computer and use it in GitHub Desktop.
Save amaisano/054003f0bed771d0ad93acb0a1a3a614 to your computer and use it in GitHub Desktop.
Reads the HA websocket API for updates to certain devices from command line
#!/bin/bash
# Requires jq binary
# Requires websocat binary
source ~/.profile
SERVER="wss://XXXXXX.ui.nabu.casa/api/websocket"
ENTITY1="input_boolean.adamo_meeting"
ENTITY2="input_boolean.work_auto_lock"
ENTITY3="binary_sensor.work_headset"
read -r -d '' ASK <<EOF
{"type": "auth", "access_token": "${BEARER}"}
{"id": 1, "type": "subscribe_trigger", "trigger": { "platform": "state", "entity_id": "${ENTITY1}" }}
{"id": 2, "type": "subscribe_trigger", "trigger": { "platform": "state", "entity_id": "${ENTITY2}" }}
{"id": 3, "type": "subscribe_trigger", "trigger": { "platform": "state", "entity_id": "${ENTITY3}" }}
EOF
myfunc() {
local line
while IFS= read -r line; do
json=$(echo "$line" | jq '.event?')
if [[ $json != "null" ]]; then
entity_id=$(echo "$json" | jq -r '.variables.trigger.to_state.entity_id')
to_state=$(echo "$json" | jq -r '.variables.trigger.to_state.state')
echo "$json" | jq '.variables.trigger.to_state'
# BTT meeting context is on customVariable1
customVariable=1
# BTT lock context is on customVariable2
if [[ $entity_id == "input_boolean.work_auto_lock" ]]; then
customVariable=2
elif [[ $entity_id == "binary_sensor.work_headset" ]]; then
customVariable=3
fi
# Sync contextual changes to Better Touch Tool conditions locally on mac
curl -s "http://localhost:57109/set_string_variable/?variableName=customVariable${customVariable}&to=${to_state}" >/dev/null
fi
done
}
while true; do
echo "$ASK"
sleep 1
done | websocat $SERVER | myfunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment