Created
January 28, 2016 16:04
-
-
Save matiaskorhonen/db0bc5b91a10842b47b9 to your computer and use it in GitHub Desktop.
A quick script to control the power state of a TV attached to a Raspberry Pi over HDMI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
iso8601_date () { | |
date +%Y-%m-%dT%H:%M:%S%z | |
} | |
turn_on () { | |
echo 'on 0' | cec-client -s -d 1 RPI | |
} | |
turn_off () { | |
echo 'standby 0' | cec-client -s -d 1 RPI | |
} | |
args="$1" | |
case $args in | |
on) | |
echo "[$(iso8601_date)]: Turning the TV on" | |
turn_on | |
;; | |
off) | |
echo "[$(iso8601_date)]: Turning the TV off" | |
turn_off | |
;; | |
auto) | |
echo "[$(iso8601_date)]: Checking the TV power state" | |
if [[ $(echo 'pow 0' | cec-client -s -d 1 RPI) =~ "power status: standby" ]]; then | |
echo "[$(iso8601_date)]: The TV appears to be off, turning it on now" | |
turn_on | |
else | |
echo "[$(iso8601_date)]: The TV appears to be on, turning it off now" | |
turn_off | |
fi | |
;; | |
*) | |
echo "Usage:" | |
echo " toggle-tv-power on|off|auto" | |
exit 1 | |
esac | |
echo "---" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment