Add HA connection validation and improved error debugging
- Add test_ha_connection() function to validate API access at startup - Improve error logging with response body details - Add debug logs showing exact API endpoints being called - Capture stderr from curl to catch connection errors
This commit is contained in:
@@ -33,6 +33,20 @@ log() {
|
|||||||
echo "[$timestamp] $message" | tee -a "$LOG_FILE"
|
echo "[$timestamp] $message" | tee -a "$LOG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_ha_connection() {
|
||||||
|
# Test Home Assistant connectivity at startup
|
||||||
|
log "Testing Home Assistant connection..."
|
||||||
|
local http_code=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/" 2>/dev/null)
|
||||||
|
|
||||||
|
if [[ "$http_code" == "200" ]]; then
|
||||||
|
log "Home Assistant API connection successful"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log "ERROR: Home Assistant API returned HTTP $http_code"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
check_target() {
|
check_target() {
|
||||||
# Use curl to get HTTP response code without following redirects
|
# Use curl to get HTTP response code without following redirects
|
||||||
local response_code=$(curl -s -o /dev/null -w "%{http_code}" -m 5 "$TARGET_URL" 2>/dev/null)
|
local response_code=$(curl -s -o /dev/null -w "%{http_code}" -m 5 "$TARGET_URL" 2>/dev/null)
|
||||||
@@ -65,11 +79,12 @@ trigger_power_cycle() {
|
|||||||
|
|
||||||
# Turn off
|
# Turn off
|
||||||
log "Sending turn_off request to Home Assistant ($turn_off_service)..."
|
log "Sending turn_off request to Home Assistant ($turn_off_service)..."
|
||||||
|
log "DEBUG: POST to $HA_URL/api/services/$turn_off_service with entity_id=$entity"
|
||||||
local response=$(curl -s -w "\n%{http_code}" -X POST \
|
local response=$(curl -s -w "\n%{http_code}" -X POST \
|
||||||
-H "Authorization: Bearer $HA_TOKEN" \
|
-H "Authorization: Bearer $HA_TOKEN" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"entity_id\": \"$entity\"}" \
|
-d "{\"entity_id\": \"$entity\"}" \
|
||||||
"$HA_URL/api/services/$turn_off_service")
|
"$HA_URL/api/services/$turn_off_service" 2>&1)
|
||||||
|
|
||||||
local http_code=$(echo "$response" | tail -n 1)
|
local http_code=$(echo "$response" | tail -n 1)
|
||||||
local body=$(echo "$response" | head -n -1)
|
local body=$(echo "$response" | head -n -1)
|
||||||
@@ -77,7 +92,8 @@ trigger_power_cycle() {
|
|||||||
if [[ "$http_code" =~ ^[2][0-9]{2}$ ]]; then
|
if [[ "$http_code" =~ ^[2][0-9]{2}$ ]]; then
|
||||||
log "Turn off request sent successfully (HTTP $http_code)"
|
log "Turn off request sent successfully (HTTP $http_code)"
|
||||||
else
|
else
|
||||||
log "ERROR: Failed to send turn_off request (HTTP $http_code). Response: $body"
|
log "ERROR: Failed to send turn_off request (HTTP $http_code)"
|
||||||
|
log "ERROR: Response body: $body"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Wait 10 seconds
|
# Wait 10 seconds
|
||||||
@@ -86,11 +102,12 @@ trigger_power_cycle() {
|
|||||||
|
|
||||||
# Turn on
|
# Turn on
|
||||||
log "Sending turn_on request to Home Assistant ($turn_on_service)..."
|
log "Sending turn_on request to Home Assistant ($turn_on_service)..."
|
||||||
|
log "DEBUG: POST to $HA_URL/api/services/$turn_on_service with entity_id=$entity"
|
||||||
local response=$(curl -s -w "\n%{http_code}" -X POST \
|
local response=$(curl -s -w "\n%{http_code}" -X POST \
|
||||||
-H "Authorization: Bearer $HA_TOKEN" \
|
-H "Authorization: Bearer $HA_TOKEN" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"entity_id\": \"$entity\"}" \
|
-d "{\"entity_id\": \"$entity\"}" \
|
||||||
"$HA_URL/api/services/$turn_on_service")
|
"$HA_URL/api/services/$turn_on_service" 2>&1)
|
||||||
|
|
||||||
local http_code=$(echo "$response" | tail -n 1)
|
local http_code=$(echo "$response" | tail -n 1)
|
||||||
local body=$(echo "$response" | head -n -1)
|
local body=$(echo "$response" | head -n -1)
|
||||||
@@ -99,7 +116,8 @@ trigger_power_cycle() {
|
|||||||
log "Turn on request sent successfully (HTTP $http_code)"
|
log "Turn on request sent successfully (HTTP $http_code)"
|
||||||
log "Power cycle completed for $entity"
|
log "Power cycle completed for $entity"
|
||||||
else
|
else
|
||||||
log "ERROR: Failed to send turn_on request (HTTP $http_code). Response: $body"
|
log "ERROR: Failed to send turn_on request (HTTP $http_code)"
|
||||||
|
log "ERROR: Response body: $body"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Reset state
|
# Reset state
|
||||||
@@ -115,6 +133,12 @@ log " HA_ENTITY: $HA_ENTITY"
|
|||||||
log " GRACE_PERIOD: ${GRACE_PERIOD}s ($(( GRACE_PERIOD / 60 )) minutes)"
|
log " GRACE_PERIOD: ${GRACE_PERIOD}s ($(( GRACE_PERIOD / 60 )) minutes)"
|
||||||
log " CHECK_INTERVAL: ${CHECK_INTERVAL}s"
|
log " CHECK_INTERVAL: ${CHECK_INTERVAL}s"
|
||||||
|
|
||||||
|
# Test HA connection before entering main loop
|
||||||
|
if ! test_ha_connection; then
|
||||||
|
log "FATAL: Cannot connect to Home Assistant. Exiting."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Main monitoring loop
|
# Main monitoring loop
|
||||||
while true; do
|
while true; do
|
||||||
RESPONSE_CODE=$(check_target)
|
RESPONSE_CODE=$(check_target)
|
||||||
|
|||||||
Reference in New Issue
Block a user