From 8968a84fcd10ee22d417dc50937696ad79bad15f Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Fri, 5 Dec 2025 22:33:24 +0100 Subject: [PATCH] Checks for HA API result --- thinkcenter_monitor.sh | 86 ++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/thinkcenter_monitor.sh b/thinkcenter_monitor.sh index ecb07a4..0938892 100644 --- a/thinkcenter_monitor.sh +++ b/thinkcenter_monitor.sh @@ -40,46 +40,52 @@ check_target() { } trigger_power_cycle() { - local entity="$1" - - 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 - - if [[ $? -eq 0 ]]; then - log "Turn off request sent successfully" - else - log "ERROR: Failed to send turn_off request" - fi - - # Wait 10 seconds - log "Waiting 10 seconds before power-on..." - sleep 10 - - # 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 - - 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 - - # Reset state - ERROR_START_TIME="" - IN_GRACE_PERIOD=false + local entity="$1" + + log "ALERT: Triggering power cycle for entity: $entity" + + # 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") + + 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 (HTTP $http_code). Response: $body" + fi + + # Wait 10 seconds + log "Waiting 10 seconds before power-on..." + sleep 10 + + # 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") + + 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 ==="