Skip to content

Instantly share code, notes, and snippets.

@victorbrca
Created January 25, 2018 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorbrca/8cf004f43b2a432024f6edb239b50434 to your computer and use it in GitHub Desktop.
Save victorbrca/8cf004f43b2a432024f6edb239b50434 to your computer and use it in GitHub Desktop.
Turns off wemo devices when computer monitor is off
#!/bin/bash
################################################################################
################################################################################
# Name: desktop-led.sh
# Usage:
# Description: Turns off Desktop lights when monitors is off
# Created:
# Copyright 2014, Victor Mendonca - http://victormendonca.com
# - https://github.com/victorbrca
# License: Released under the terms of the GNU GPL license v3
################################################################################
################################################################################
#-------------------------------------------------------------------------------
# Sets variables
#-------------------------------------------------------------------------------
# Create the folder ${HOME}/bin/var/log
runfolder="${HOME}/bin"
log=${runfolder}/var/log/desktop-led.log
# Download wemo_control.sh from my gists
if [[ ! -x "${runfolder}/devices/wemo_control.sh" ]] ; then
echo "We need ${runfolder}/devices/wemo_control.sh for this script"
exit 1
fi
# Change below to the IP address of your Wemo devices
devices="192.168.1.3 192.168.1.2"
#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------
_getMonitorStatus () {
monitor_status=$(xset -q | grep Monitor | awk '{print $3}')
if [[ ! "$monitor_status" ]] ; then
echo "Could not set status"
exit 1
fi
}
_turnOffLights () {
for device in $devices ; do
./wemo_control.sh "$device" off
done
}
_turnOnLights () {
for device in $devices ; do
./wemo_control.sh "$device" on
done
}
_actOnStatus () {
case $monitor_status in
On) _turnOnLights ;;
Off) _turnOffLights ;;
esac
}
#-------------------------------------------------------------------------------
# Starts script
#-------------------------------------------------------------------------------
while true ; do
_getMonitorStatus
if [[ "$current_status" ]] ; then
if [[ "$current_status" != "$monitor_status" ]] ; then
_actOnStatus
fi
else
_actOnStatus
fi
current_status="$monitor_status"
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment