Checks for HA API result

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

View File

@@ -40,46 +40,52 @@ check_target() {
}
trigger_power_cycle() {
local entity="$1"
local entity="$1"
log "ALERT: Triggering power cycle for entity: $entity"
log "ALERT: Triggering power cycle for entity: $entity"
# Turn off
log "Sending turn_off request to Home Assistant..."
curl -s -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
# Turn off
log "Sending turn_off request to Home Assistant..."
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")
if [[ $? -eq 0 ]]; then
log "Turn off request sent successfully"
else
log "ERROR: Failed to send turn_off request"
fi
local http_code=$(echo "$response" | tail -n 1)
local body=$(echo "$response" | head -n -1)
# Wait 10 seconds
log "Waiting 10 seconds before power-on..."
sleep 10
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 (HTTP $http_code). Response: $body"
fi
# Turn on
log "Sending turn_on request to Home Assistant..."
curl -s -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
# Wait 10 seconds
log "Waiting 10 seconds before power-on..."
sleep 10
if [[ $? -eq 0 ]]; then
log "Turn on request sent successfully"
log "Power cycle completed for $entity"
else
log "ERROR: Failed to send turn_on request"
fi
# Turn on
log "Sending turn_on request to Home Assistant..."
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")
# Reset state
ERROR_START_TIME=""
IN_GRACE_PERIOD=false
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 (HTTP $http_code). Response: $body"
fi
# Reset state
ERROR_START_TIME=""
IN_GRACE_PERIOD=false
}
log "=== Thinkcentre Monitor Started ==="