SHARE
TWEET

Untitled

a guest Apr 19th, 2019 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. IP=MYIP
  4. declare -A devices=(
  5.  ["tv"]="2"
  6.  ["garage_light"]="1"
  7. )
  8.  
  9. stdbuf -oL mosquitto_sub -v -h $IP -t 'cmnd/+/POWER'|
  10.   while IFS= read -r line
  11.   do
  12.     device=$(echo $line | cut -d'/' -f2)
  13.     state=$(echo $line | cut -d' ' -f2)
  14.     echo "received request to turn $state $device"
  15.     # check if it is a valid signal
  16.     if [ ${devices[$device]+check} ] ; then
  17.       # send home assistant status on
  18.       echo "seems legit"
  19.       if [ "$state" == "OFF" ] ; then
  20.         codesend ${devices[$device]} off
  21.         mosquitto_pub -h $IP -t stat/${device}/POWER -m "OFF"
  22.       elif [ "$state" == "ON" ] ; then
  23.         codesend ${devices[$device]} on
  24.         mosquitto_pub -h $IP -t stat/${device}/POWER -m "ON"
  25.       else
  26.         echo "unknown state $state"
  27.       fi
  28.     else
  29.       echo "unknown device $device"
  30.     fi
  31.   done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top