Skip to content

Instantly share code, notes, and snippets.

@coolbho3k
Created July 8, 2023 21:43
Show Gist options
  • Save coolbho3k/0d2b6df26a77f1d252942e48b9a0db20 to your computer and use it in GitHub Desktop.
Save coolbho3k/0d2b6df26a77f1d252942e48b9a0db20 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Written almost entirely by ChatGPT O_O
# Variables
BEARER_TOKEN="your_bearer_token"
SOUNDBAR_DEVICE_ID="your_soundbar_device_id"
PREFERRED_SOUND_MODES=("adaptive sound+" "adaptive sound" "surround" "standard")
VERBOSE=0
SILENT=0
# Parse command line flags
while getopts ":vs" opt; do
case ${opt} in
v ) # Process -v (verbose) flag
VERBOSE=1
;;
s ) # Process -s (silent) flag
SILENT=1
;;
\? ) echo "Usage: cmd [-v] [-s]"
;;
esac
done
# Set curl and stdout options based on silent flag
OUT=/dev/stdout
if [ $SILENT -eq 1 ]; then
CURL_SILENT="-s"
OUT=/dev/null
fi
# Main loop
while true; do
# Tell the sound bar we want to check the sound modes
curl $CURL_SILENT -X POST -H "Authorization: Bearer $BEARER_TOKEN" -H "Content-Type: application/json" \
-d '{"commands":[{"component":"main","capability":"execute","command":"execute","arguments":["/sec/networkaudio/soundmode"]}]}' \
https://api.smartthings.com/v1/devices/$SOUNDBAR_DEVICE_ID/commands > $OUT
# Wait for a moment
sleep 1
# Get the current sound mode and available sound modes
response=$(curl $CURL_SILENT -X GET -H "Authorization: Bearer $BEARER_TOKEN" \
https://api.smartthings.com/v1/devices/$SOUNDBAR_DEVICE_ID/components/main/capabilities/execute/status)
# Extract the current sound mode and available sound modes from the response
current_sound_mode=$(echo $response | jq -r '.data.value.payload."x.com.samsung.networkaudio.soundmode"')
IFS=$'\n' read -r -d '' -a available_sound_modes < <(echo $response | jq -r '.data.value.payload."x.com.samsung.networkaudio.supportedSoundmode"[]' && printf '\0')
# Determine the preferred sound mode
preferred_sound_mode=""
for mode in "${PREFERRED_SOUND_MODES[@]}"; do
if printf '%s\n' "${available_sound_modes[@]}" | grep -q -x "$mode"; then
preferred_sound_mode=$mode
if [ $VERBOSE -eq 1 ]; then echo "Preferred mode $mode is available. Setting it as the preferred sound mode if it isn't already."; fi
break
else
if [ $VERBOSE -eq 1 ]; then echo "Preferred mode $mode is not available, checking the next one."; fi
fi
done
# Switch the sound mode if necessary
if [[ "$current_sound_mode" != "$preferred_sound_mode" ]]; then
curl $CURL_SILENT -X POST -H "Authorization: Bearer $BEARER_TOKEN" -H "Content-Type: application/json" \
-d '{"commands":[{"component":"main","capability":"execute","command":"execute","arguments":["/sec/networkaudio/soundmode",{"x.com.samsung.networkaudio.soundmode":"'"$preferred_sound_mode"'"}]}]}' \
https://api.smartthings.com/v1/devices/$SOUNDBAR_DEVICE_ID/commands > $OUT
echo "Switched from $current_sound_mode to $preferred_sound_mode.";
fi
# Wait for 5 seconds
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment