From dde29e9982be5efb66fab1c75fe14234a286207f Mon Sep 17 00:00:00 2001 From: Tim Connors Date: Sat, 23 Dec 2023 21:19:56 +1100 Subject: [PATCH] tasmota: refactor to remove all the duplicated code in switch&dimmer --- plugins/power/tasmota_ | 85 ++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/plugins/power/tasmota_ b/plugins/power/tasmota_ index 2bdc8883..8fe53c3c 100755 --- a/plugins/power/tasmota_ +++ b/plugins/power/tasmota_ @@ -283,35 +283,25 @@ switch() { if [ "$1" = "config" ]; then echo "graph_title Tasmota Switch: $DEVICE" echo "graph_args --base 1000 -l 0" + fi - for v in POWER POWER{1,2,3,4,5,6,7,8,9} ; do - axis=$( echo "$v" | cut -d= -f 1 ) - lab=$( echo "$v" | cut -d= -f 2 ) - get_status 11 ".StatusSTS.$v" - if [ "$res" != U ] || [ -e $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen ] ; then - # if we've ever seen an axis, then we want to keep - # telling munin we are capable of reading that axis, - # even if the device is currently unpowered - mkdir -p $MUNIN_PLUGSTATE/tasmota - touch $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen + for v in POWER POWER{1,2,3,4,5,6,7,8,9} ; do + axis=$( echo "$v" | cut -d= -f 1 ) + lab=$( echo "$v" | cut -d= -f 2 ) + get_status 11 ".StatusSTS.$v" + if [ "$res" != U ] || [ -e $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen ] ; then + # if we've ever seen an axis, then we want to keep + # telling munin we are capable of reading that axis, + # even if the device is currently unpowered + mkdir -p $MUNIN_PLUGSTATE/tasmota + touch $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen + + if [ "$1" = "config" ]; then echo "$axis.label $lab" echo "$axis.type GAUGE" echo "$axis.min 0" - fi - done - else - for v in POWER POWER{1,2,3,4,5,6,7,8,9} ; do - axis=$( echo "$v" | cut -d= -f 1 ) - lab=$( echo "$v" | cut -d= -f 2 ) - get_status 11 ".StatusSTS.$v" - if [ "$res" != U ] || [ -e $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen ] ; then - # if we've ever seen an axis, then we want to keep - # telling munin we are capable of reading that axis, - # even if the device is currently unpowered - mkdir -p $MUNIN_PLUGSTATE/tasmota - touch $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen - + else case "$res" in ON) res=1 @@ -326,50 +316,41 @@ switch() { echo "$axis.value $res" fi - done - fi + fi + done } dimmer() { if [ "$1" = "config" ]; then echo "graph_title Tasmota Dimmer: $DEVICE" echo "graph_args --base 1000 -l 0" + fi - for v in Channel{1,2,3,4} ; do - axis=$( echo "$v" | cut -d= -f 1 ) - lab=$( echo "$v" | cut -d= -f 2 ) - get_status 11 ".StatusSTS.$v" - if [ "$res" != U ] || [ -e $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen ] ; then - # if we've ever seen an axis, then we want to keep - # telling munin we are capable of reading that axis, - # even if the device is currently unpowered - mkdir -p $MUNIN_PLUGSTATE/tasmota - touch $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen + for v in Channel{1,2,3,4} ; do + axis=$( echo "$v" | cut -d= -f 1 ) + lab=$( echo "$v" | cut -d= -f 2 ) + get_status 11 ".StatusSTS.$v" + if [ "$res" != U ] || [ -e $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen ] ; then + # if we've ever seen an axis, then we want to keep + # telling munin we are capable of reading that axis, + # even if the device is currently unpowered + mkdir -p $MUNIN_PLUGSTATE/tasmota + touch $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen + + if [ "$1" = "config" ]; then echo "$axis.label $lab" echo "$axis.type GAUGE" echo "$axis.min 0" - fi - done - else - for v in Channel{1,2,3,4} ; do - axis=$( echo "$v" | cut -d= -f 1 ) - lab=$( echo "$v" | cut -d= -f 2 ) - get_status 11 ".StatusSTS.$v" - if [ "$res" != U ] || [ -e $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen ] ; then - # if we've ever seen an axis, then we want to keep - # telling munin we are capable of reading that axis, - # even if the device is currently unpowered - mkdir -p $MUNIN_PLUGSTATE/tasmota - touch $MUNIN_PLUGSTATE/tasmota/$DEVICE.$FUNCTION.$v.seen - + else if [ "$res" = U ] ; then res=0 # assume 0 power, since if it doesn't have power to get wifi, then it's not going to have power output fi + echo "$axis.value $res" fi - done - fi + fi + done } case "$FUNCTION" in