Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logging for synthesize.sh #6

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions assist_microphone/scripts/synthesize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set -e

# Take text on stdin and JSON-encode it
text="$(cat | jq -R -s '.')"
bashio::log.info "Text to speech text: ${text}"

# Set the default webhook name if not set in the configuration
if bashio::var.has_value "$(bashio::config 'webhook_id')"; then
Expand All @@ -24,7 +25,6 @@ else
bashio::log.warning "webhook_id is not set. Will set to default"
webhook_id="synthesize-assist-microphone-response"
fi
bashio::log.debug "webhook_id is set to : $(bashio::config 'webhook_id')"

# Check if SUPERVISOR_TOKEN is set
if [ -z "$SUPERVISOR_TOKEN" ]; then
Expand All @@ -41,8 +41,7 @@ ha_ip=$(curl -s -X GET \
if [ -z "$ha_ip" ]; then
bashio::log.error "Failed to get Home Assistant IPv4 address."
exit 1
fi
bashio::log.debug "Home Assistant IPv4 : $ha_ip"
fi

# Determine if the HA host has SSL enabled.
ssl_enabled=$(curl -s -X GET \
Expand All @@ -52,21 +51,20 @@ ssl_enabled=$(curl -s -X GET \
if [ -z "$ssl_enabled" ]; then
bashio::log.error "Failed to determine if SSL is enabled."
exit 1
fi
bashio::log.debug "Home Assistant SSL enabled : $ssl_enabled"
fi

# Construct webhook URL based on SSL state, IP and webhook
if [[ "$ssl_enabled" == "true" ]]; then
webhookurl="https://${ha_ip}:8123/api/webhook/${webhook_id}"
else
webhookurl="http://${ha_ip}:8123/api/webhook/${webhook_id}"
fi
bashio::log.debug "webhookurl set to : $webhookurl"
bashio::log.info "webhookurl set to : $webhookurl"

# Send the text to the Home Assistant Webhook.
bashio::log.debug "Text to speech text: ${text} to $webhookurl"
bashio::log.info "Sending text to webhook..."
json_payload="{\"response\": ${text}}"
bashio::log.debug "Payload: ${json_payload}"
bashio::log.info "Payload: ${json_payload}"
response=$(curl -s -o /dev/null -w "%{http_code}" -k -X POST \
-H "Content-Type: application/json" \
-d "$json_payload" \
Expand All @@ -75,4 +73,4 @@ if [ "$response" -ne 200 ]; then
bashio::log.error "Failed to send text to webhook. HTTP status code: $response"
exit 1
fi
bashio::log.debug "Successfully sent text to webhook."
bashio::log.info "Successfully sent text to webhook."
Loading