infrapuzzle/esphome/send_message.sh

36 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Exit on error
set -e
CDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Read the token from the home-assistant secrets folder
TOKEN_FILE="${CDIR}/../k8s/home-assistant/tmp_long_lived_token.secret.yml"
if [ ! -f "$TOKEN_FILE" ]; then
echo "Error: Home Assistant token file not found at $TOKEN_FILE."
exit 1
fi
# Extract the token (grab the line that is not a comment)
TOKEN=$(grep -v '^#' "$TOKEN_FILE" | tr -d '\n' | tr -d '\r' | xargs)
if [ -z "$TOKEN" ]; then
echo "Error: Token extracted from $TOKEN_FILE is empty."
exit 1
fi
# Get the message from arguments
MESSAGE="${1:-"Hello from command line!"}"
echo "Sending message: '$MESSAGE' to Home Assistant (https://hass.moritzgraf.de)..."
# Call the Home Assistant REST API to set the value of the input_text helper
curl -s -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"entity_id\": \"input_text.epaper_message\", \"value\": \"$MESSAGE\"}" \
https://hass.moritzgraf.de/api/services/input_text/set_value
echo -e "\nDone!"