add 'nomyzone' config option to prevent a zone from ever becoming the "myzone".

This commit is contained in:
Rob Pearce 2021-07-01 16:24:15 +10:00
parent 407af3f3e6
commit 6ff51f038e
1 changed files with 45 additions and 27 deletions

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# TODO: comparison to previous reading # TODO: comparison to previous reading
# TODO: gnuplot of given time range # TODO: gnuplot of given time range
# TODO: ditch pymyair # TODO: ditch pymyair
# TODO: manual controls # TODO: manual controls
@ -84,7 +84,7 @@ function get_midrange() { # outputs midpoint of temperature range for given zon
local min max local min max
min=${zwantmin[$idx]} min=${zwantmin[$idx]}
max=${zwantmax[$idx]} max=${zwantmax[$idx]}
echo "($min + $max) / 2" | bc echo "($min + $max) / 2" | bc
} }
function getoppositestate() { function getoppositestate() {
@ -342,7 +342,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
zaction[$x]="power_off" zaction[$x]="power_off"
if [[ -z $powerchange ]]; then if [[ -z $powerchange ]]; then
powerchange="power_off" powerchange="power_off"
elif [[ ${zaction[$x]} != $powerchange ]]; then elif [[ ${zaction[$x]} != $powerchange ]]; then
zactionfail[$x]=1 zactionfail[$x]=1
fi fi
else else
@ -385,7 +385,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
fi fi
fi fi
[[ ! -z $globsetmode ]] && globaction="power_on" [[ ! -z $globsetmode ]] && globaction="power_on"
else else
if [[ $airconmode == "heat" && $nhot -ge 2 ]]; then if [[ $airconmode == "heat" && $nhot -ge 2 ]]; then
globprob="too_hot" globprob="too_hot"
elif [[ $airconmode == "cool" && $ncold -ge 2 ]]; then elif [[ $airconmode == "cool" && $ncold -ge 2 ]]; then
@ -432,40 +432,48 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
if [[ -z $powerchange ]]; then if [[ -z $powerchange ]]; then
if [[ $airconmode != "off" ]]; then if [[ $airconmode != "off" ]]; then
mzmustmove=0 mzmustmove=0
# get list of all zones which can't be myzone
nomyzone="" nomyzone=""
for x in ${!zname[@]}; do
if [[ ${znomyzone[$x]} -eq 1 ]]; then
nomyzone="$nomyzone ${zname[$x]} "
fi
done
for x in ${!zname[@]}; do for x in ${!zname[@]}; do
if [[ $airconmyzoneid == ${zid[$x]} || ${zaction[$x]} == *set_myzone* ]]; then if [[ $airconmyzoneid == ${zid[$x]} || ${zaction[$x]} == *set_myzone* ]]; then
if [[ ${zstate[$x]} == "close" || ${zaction[$x]} == *close_vent* ]]; then if [[ ${zstate[$x]} == "close" || ${zaction[$x]} == *close_vent* ]]; then
mzmustmove=1 mzmustmove=1
elif [[ ${zaction[@]} == *close:${zname[$x]}* ]]; then elif [[ ${zaction[@]} == *close:${zname[$x]}* ]]; then
mzmustmove=1 mzmustmove=1
elif [[ ${znomyzone[$x]} -eq 1 ]]; then
mzmustmove=1
fi fi
if [[ $mzmustmove -eq 1 ]]; then if [[ $mzmustmove -eq 1 ]]; then
[[ $db -eq 1 ]] && info "myzone must move away from ${zname[$x]}" [[ $db -eq 1 ]] && info "myzone must move away from ${zname[$x]}"
#info "myzone must move away from ${zname[$x]}" #info "myzone must move away from ${zname[$x]}"
zaction[$x]="${zaction[$x]/set_myzone/}" zaction[$x]="${zaction[$x]/set_myzone/}"
nomyzone="$nomyzone ${zname[$x]} " nomyzone="$nomyzone ${zname[$x]} "
[[ $db -eq 1 ]] && info "zaction[${zname[$z]} is '${zaction[$x]}'" [[ $db -eq 1 ]] && info "zaction[${zname[$z]} is '${zaction[$x]}'"
[[ $db -eq 1 ]] && info "nomyzone is '$nomyzone}'" [[ $db -eq 1 ]] && info "nomyzone is '$nomyzone}'"
fi fi
fi fi
done done
if [[ $mzmustmove -eq 1 ]]; then if [[ $mzmustmove -eq 1 ]]; then
[[ $db -eq 1 ]] && info "got mzmustmove == 1" [[ $db -eq 1 ]] && info "got mzmustmove == 1"
[[ $db -eq 1 ]] && info "FINAL nomyzone is '$nomyzone}'" [[ $db -eq 1 ]] && info "FINAL nomyzone is '$nomyzone}'"
# find an alternative # find an alternative
for x in ${!zname[@]}; do for x in ${!zname[@]}; do
# open or opening? set myzone. we'll resolve # open or opening? set myzone. we'll resolve
# duplicates in the next step. # duplicates in the next step.
[[ $db -eq 1 ]] && info " checking if we can move mz to ${zname[$x]}" [[ $db -eq 1 ]] && info " checking if we can move mz to ${zname[$x]}"
poss=0 poss=0
if [[ ${zaction[$x]} != *set_myzone* && $nomyzone != *\ ${zname[$x]}\ * ]]; then if [[ ${zaction[$x]} != *set_myzone* && $nomyzone != *\ ${zname[$x]}\ * ]]; then
[[ $db -eq 1 ]] && info " not already moving mz here + not in nomyzone list" [[ $db -eq 1 ]] && info " not already moving mz here + not in nomyzone list"
if [[ ${zstate[$x]} == "open" || ${zaction[$x]} == *open_vent* ]]; then if [[ ${zstate[$x]} == "open" || ${zaction[$x]} == *open_vent* ]]; then
[[ $db -eq 1 ]] && info " is open or opening -> OK" [[ $db -eq 1 ]] && info " is open or opening -> OK"
poss=1 poss=1
elif [[ ${zaction[@]} == *open:${zname[$x]}* ]]; then elif [[ ${zaction[@]} == *open:${zname[$x]}* ]]; then
[[ $db -eq 1 ]] && info " is opening via adjacency-> OK" [[ $db -eq 1 ]] && info " is opening via adjacency-> OK"
poss=1 poss=1
fi fi
if [[ $poss -eq 1 ]]; then if [[ $poss -eq 1 ]]; then
@ -525,7 +533,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
# gen_aircon_command zone_idx "command1 command2 etc" # gen_aircon_command zone_idx "command1 command2 etc"
function gen_aircon_command() { function gen_aircon_command() {
local idx allactions this toadd num doneset othername otherid otheridx db donemode str local idx allactions this toadd num doneset othername otherid otheridx db donemode str
idx=$1 idx=$1
shift shift
@ -633,6 +641,13 @@ function addnoop() {
zignore[$idx]=1 zignore[$idx]=1
} }
function addnomyzone() {
local idx
idx=$(getidxfromname "$1")
znomyzone[$idx]=1
}
function addforcevent() { function addforcevent() {
local x idx local x idx
idx=$nforcevents idx=$nforcevents
@ -798,7 +813,7 @@ function conderror() {
echo -e "${RED} Start: $2" echo -e "${RED} Start: $2"
echo -e "${RED} End: $3" echo -e "${RED} End: $3"
} }
function load_config() { function load_config() {
local line rv timestr ttok stype ok starth endh nowh local line rv timestr ttok stype ok starth endh nowh
local cond allconds x local cond allconds x
@ -905,8 +920,10 @@ function load_config() {
fi fi
elif [[ ${tok[0]} == "adj" ]]; then elif [[ ${tok[0]} == "adj" ]]; then
addadj ${tok[1]} ${tok[@]:2} addadj ${tok[1]} ${tok[@]:2}
elif [[ ${tok[0]} == "nomyzone" || ${tok[0]} == "nomy" ]]; then
addnnomyzone ${tok[1]}
elif [[ ${tok[0]} == "noop" || ${tok[0]} == "ignore" ]]; then elif [[ ${tok[0]} == "noop" || ${tok[0]} == "ignore" ]]; then
addnoop ${tok[1]} addnoop ${tok[1]}
elif [[ ${tok[0]} == "force" ]]; then elif [[ ${tok[0]} == "force" ]]; then
if [[ $VALID_ZONE_STATES == *\ ${tok[2]}\ * ]]; then if [[ $VALID_ZONE_STATES == *\ ${tok[2]}\ * ]]; then
addforcevent ${tok[1]} ${tok[2]} addforcevent ${tok[1]} ${tok[2]}
@ -945,7 +962,7 @@ function canping() {
ip=${ip%% *} ip=${ip%% *}
if [[ -z $ip ]]; then if [[ -z $ip ]]; then
thisrv=1 thisrv=1
else else
#arp -d $ip >/dev/null 2>&1 #arp -d $ip >/dev/null 2>&1
#ping -c1 -w1 -n -q $ip >/dev/null 2>&1 & #ping -c1 -w1 -n -q $ip >/dev/null 2>&1 &
#sleep 0.3 #sleep 0.3
@ -981,6 +998,7 @@ function addzone() {
zownerperson[$nzones]=$(getownerperson "$name") zownerperson[$nzones]=$(getownerperson "$name")
#info "zone $name owner is [${zowner[$nzones]}]" #info "zone $name owner is [${zowner[$nzones]}]"
zignore[$nzones]=0 zignore[$nzones]=0
znomyzone[$nzones]=0
zpri[$nzones]=0 zpri[$nzones]=0
wanttemp=$(gettemprange "$name") wanttemp=$(gettemprange "$name")
if [[ $wanttemp == "n/a" ]]; then if [[ $wanttemp == "n/a" ]]; then
@ -992,13 +1010,13 @@ function addzone() {
zwantmin[$nzones]="${wanttemp%-*}" zwantmin[$nzones]="${wanttemp%-*}"
zwantmax[$nzones]="${wanttemp#*-}" zwantmax[$nzones]="${wanttemp#*-}"
fi fi
nzones=$((nzones + 1)) nzones=$((nzones + 1))
} }
function getcol() { function getcol() {
local col local col
case $1 in case $1 in
"off") col="$GREY";; "off") col="$GREY";;
"cool") col="$CYAN";; "cool") col="$CYAN";;
"heat") col="$RED";; "heat") col="$RED";;
@ -1045,7 +1063,7 @@ function gen_config() {
echo "# @hhmm-hhmm Restrict between certain 24 hour times" >> $CONFIGFILE echo "# @hhmm-hhmm Restrict between certain 24 hour times" >> $CONFIGFILE
echo "# @Mon-Wed Restrict between certain weekdays" >> $CONFIGFILE echo "# @Mon-Wed Restrict between certain weekdays" >> $CONFIGFILE
echo "# @Mon Restrict to one weekdays only" >> $CONFIGFILE echo "# @Mon Restrict to one weekdays only" >> $CONFIGFILE
echo "# @Mon;hhmm-hhmm Restrict both day and time" >> $CONFIGFILE echo "# @Mon;hhmm-hhmm Restrict both day and time" >> $CONFIGFILE
echo "# For example:" >> $CONFIGFILE echo "# For example:" >> $CONFIGFILE
echo "# Keep zone closed on Tuesdays and Wednesdays" >> $CONFIGFILE echo "# Keep zone closed on Tuesdays and Wednesdays" >> $CONFIGFILE
@ -1210,7 +1228,7 @@ function show_proposed_commands() {
count=0 count=0
for x in ${!airconcmd[@]}; do for x in ${!airconcmd[@]}; do
fullcmd="myair $AIRCON_IP ${airconcmd[$x]}" fullcmd="myair $AIRCON_IP ${airconcmd[$x]}"
printf -- "$cmdformat" "$fullcmd" printf -- "$cmdformat" "$fullcmd"
[[ ! -z ${airconcomment[$x]} ]] && printf " $GREEN$BOLD# $PLAIN$GREEN%s$PLAIN\n" "${airconcomment[$x]}" || echo [[ ! -z ${airconcomment[$x]} ]] && printf " $GREEN$BOLD# $PLAIN$GREEN%s$PLAIN\n" "${airconcomment[$x]}" || echo
count=$((count + 1)) count=$((count + 1))
done done
@ -1221,7 +1239,7 @@ function run_commands() {
local x local x
for x in ${!airconcmd[@]}; do for x in ${!airconcmd[@]}; do
[[ ! -z ${airconcomment[$x]} ]] && action "${airconcomment[$x]}" [[ ! -z ${airconcomment[$x]} ]] && action "${airconcomment[$x]}"
echo RUNNING myair $AIRCON_IP ${airconcmd[$x]} echo RUNNING myair $AIRCON_IP ${airconcmd[$x]}
myair $AIRCON_IP ${airconcmd[$x]} >/dev/null 2>&1 myair $AIRCON_IP ${airconcmd[$x]} >/dev/null 2>&1
influx_insert "INSERT aircon action=\"${airconcomment[$x]}\",comment=\"${airconproblem[$x]}\"" influx_insert "INSERT aircon action=\"${airconcomment[$x]}\",comment=\"${airconproblem[$x]}\""
done done
@ -1278,7 +1296,7 @@ function influx_insert() { # $1 == cmd
function influx_init() { # $1 == dbname function influx_init() { # $1 == dbname
local dbfound local dbfound
influx=$(which influx 2>/dev/null) influx=$(which influx 2>/dev/null)
influxdb="$1" influxdb="$1"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
error "influx executable not found in path." error "influx executable not found in path."
return 1 return 1
@ -1435,7 +1453,7 @@ fi
if [[ -e $CONFIGFILE ]]; then if [[ -e $CONFIGFILE ]]; then
if ! load_config; then if ! load_config; then
error "Config load failed" error "Config load failed"
exit 1 exit 1
fi fi
else else
@ -1463,7 +1481,7 @@ if [[ $showwho -eq 1 ]]; then
printf "$TFORMAT" "Person" "Availability" printf "$TFORMAT" "Person" "Availability"
for x in ${!pname[@]}; do for x in ${!pname[@]}; do
if canping "${pdev[$x]}"; then if canping "${pdev[$x]}"; then
thisform="$HFORMAT" thisform="$HFORMAT"
str="At home" str="At home"
else else
thisform="$AFORMAT" thisform="$AFORMAT"
@ -1497,7 +1515,7 @@ if [[ $logmode -eq 1 ]]; then
exit 0 exit 0
elif [[ $logmode -eq 2 ]]; then elif [[ $logmode -eq 2 ]]; then
now=$(date +'%d/%m/%Y %H:%M:%S') now=$(date +'%d/%m/%Y %H:%M:%S')
rv=0 rv=0
failedcmds="" failedcmds=""
influx_clear influx_clear