Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement