Last active
January 4, 2023 03:41
-
-
Save kevinlekiller/68326adf7fd79bd8cf806eea807404ba to your computer and use it in GitHub Desktop.
Simple bash script to control TPLink Kasa dimmers (HS220 for example).
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
#!/bin/bash | |
# GNU GPLv2 | |
# Simple bash script to control TPLink Kasa dimmers (HS220 for example). | |
# Turn on or off Kasa dimmer: KASA_HOST=192.168.1.2 ./dimmer | |
# Set brightness of the dimmer: KASA_HOST=192.168.1.2 ./dimmer [1-100] | |
KASA_HOST=${KASA_HOST:-192.168.1.144} | |
if ! which kasa &> /dev/null; then | |
echo "ERROR: The kasa executable is required, see https://github.com/python-kasa/python-kasa" | |
exit 1 | |
fi | |
if [[ -n $1 ]]; then | |
if [[ $1 =~ ^[0-9]* ]] && [[ $1 -gt 0 ]] && [[ $1 -le 100 ]]; then | |
kasa --host "$KASA_HOST" brightness "$1" &> /dev/null & | |
exit 0 | |
fi | |
echo "ERROR: Brightness must be a number in the range of 1 to 100." | |
exit 1 | |
fi | |
state=$(kasa --host "$KASA_HOST" state | grep -m1 "Device state: O" | cut -d: -f2) | |
if [[ ! $state =~ (ON|OFF) ]]; then | |
echo "ERROR: Problem querying the dimmer." | |
exit 1 | |
fi | |
[[ $state =~ OFF ]] && state=on || state=off | |
kasa --host "$KASA_HOST" "$state" &> /dev/null & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment