Created
January 28, 2016 16:04
-
-
Save matiaskorhonen/738fd698ac7a7a73ca14 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. Requires `cec-utils` to be installed.
This file contains hidden or 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