From ee7751ffd5c100e1520186202900ccf5575109d5 Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Thu, 16 May 2024 18:47:25 +1000 Subject: [PATCH] Powering off system when closing the last remaining zone and it being the constant zone now works correctly. --- aircon.sh | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/aircon.sh b/aircon.sh index 3385ffa..ae0f82f 100755 --- a/aircon.sh +++ b/aircon.sh @@ -497,12 +497,15 @@ function generate_actions() { # populates global: nairconcommands & airconcmd # pass xx - if we're closing the constant zone, and it's NOT the myzone, # just turn the whole system off (since it will probably be opened. # again anyway). + # if [[ -z $powerchange && $airconmode != "off" ]]; then for x in ${!zname[@]}; do - if [[ ${zaction[$x]} == *close_vent* && $constant == ${zname[$x]} ]]; then - if [[ $airconmyzoneid != ${zid[$x]} ]]; then - zaction[$x]="power_off" - [[ -z $powerchange ]] && powerchange="power_off" + if [[ ${zaction[$x]} == *close_vent* ]]; then + if [[ $constant == ${zname[$x]} ]]; then + if [[ $airconmyzoneid != ${zid[$x]} ]]; then + zaction[$x]="power_off" + [[ -z $powerchange ]] && powerchange="power_off" + fi fi fi done @@ -619,9 +622,22 @@ function generate_actions() { # populates global: nairconcommands & airconcmd gen_aircon_command ${x} ${zaction[$x]} fi done - if [[ -n $globaction ]]; then - gen_aircon_command -1 ${globaction} + + # if we are closing the constant zone and taking no other action, + # just turn the system off. + if [[ $nairconcommands -eq 1 ]]; then + if [[ ${airconjcmd[0]} == *lose* ]]; then + if [[ ${airconcmdzone[0]} == $constant ]]; then + globprob="constant_close" + globaction="power_off" + [[ -z $powerchange ]] && powerchange="power_off" + fi + fi fi + if [[ -n $globaction ]]; then + gen_aircon_command -1 ${globaction} + fi + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}" } @@ -1542,6 +1558,15 @@ function get_aircon_info() { [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Querying aircon... ${PLAIN}" url="${AIRCON_URL}/getSystemData" jsoninfo=$(curl -s "$url" 2>/dev/null) + if [[ $? -ne 0 ]]; then + error "get_aircon_info() failed to reach ${AIRCON_URL}/getSystemData endpoint" + exit 1; + fi + echo "$jsoninfo" | jq . &>/dev/null + if [[ $? -ne 0 ]]; then + error "${AIRCON_URL}/getSystemData returned bad json: $jsoninfo" + exit 1; + fi zones=$(echo "$jsoninfo" | $JQ -r '.aircons.ac1.zones[] | [ .name, .state, .setTemp, .measuredTemp, .number ] | @csv' | tr -d '" ') state=$(echo "$jsoninfo" | $JQ -r '.aircons.ac1.info.state' | tr -d '" ') if [[ $state == "off" ]]; then @@ -1555,6 +1580,10 @@ url="${AIRCON_URL}/getSystemData" IFS=',' read -ra tok <<< "$line" addzone "${tok[0]}" "${tok[1]}" "${tok[2]}" "${tok[3]}" "${tok[4]}" done + if [[ $nzones -eq 0 ]]; then + error "get_aircon_info() found no zones" + exit 1; + fi airconmyzone=$(getnamefromid $airconmyzoneid) [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}" profile "query aircon" @@ -1674,6 +1703,8 @@ function describe_globprob() { echo "$limit or more zones are ${globprob/_/ }" elif [[ $globprob == "not_needed" ]]; then echo "The system is on but all zones at optimal temperature" + elif [[ $globprob == "constant_close" ]]; then + echo "All other zones are already closed" else echo "_unknown_globprob_:$globprob" fi