Checks for HA API result

This commit is contained in:
2025-12-05 22:33:24 +01:00
parent 257e4c2062
commit 8968a84fcd

View File

@@ -46,16 +46,19 @@ trigger_power_cycle() {
# Turn off
log "Sending turn_off request to Home Assistant..."
curl -s -X POST \
local response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: Bearer $HA_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"entity_id\": \"$entity\"}" \
"$HA_URL/api/services/switch/turn_off" > /dev/null 2>&1
"$HA_URL/api/services/switch/turn_off")
if [[ $? -eq 0 ]]; then
log "Turn off request sent successfully"
local http_code=$(echo "$response" | tail -n 1)
local body=$(echo "$response" | head -n -1)
if [[ "$http_code" =~ ^[2][0-9]{2}$ ]]; then
log "Turn off request sent successfully (HTTP $http_code)"
else
log "ERROR: Failed to send turn_off request"
log "ERROR: Failed to send turn_off request (HTTP $http_code). Response: $body"
fi
# Wait 10 seconds
@@ -64,17 +67,20 @@ trigger_power_cycle() {
# Turn on
log "Sending turn_on request to Home Assistant..."
curl -s -X POST \
local response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: Bearer $HA_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"entity_id\": \"$entity\"}" \
"$HA_URL/api/services/switch/turn_on" > /dev/null 2>&1
"$HA_URL/api/services/switch/turn_on")
if [[ $? -eq 0 ]]; then
log "Turn on request sent successfully"
local http_code=$(echo "$response" | tail -n 1)
local body=$(echo "$response" | head -n -1)
if [[ "$http_code" =~ ^[2][0-9]{2}$ ]]; then
log "Turn on request sent successfully (HTTP $http_code)"
log "Power cycle completed for $entity"
else
log "ERROR: Failed to send turn_on request"
log "ERROR: Failed to send turn_on request (HTTP $http_code). Response: $body"
fi
# Reset state