Giter Club home page Giter Club logo

kasa's Introduction

Kasa Smart

This UNOFFICIAL repository contains information about TP-Link's Kasa Smart API and a C-language tool for interacting with Kasa devices via the local control protocol (not via the cloud service).

GitHub issues and pull requests are very welcome to help refine and expand this knowledge base.

Compiling

The tool should compile and run on just about any POSIX-compliant system. Linux and macOS are known to be supported. Windows users are probably out of luck — sorry, not sorry.

$ cc -O3 -o kasa kasa.c

Usage

$ echo '{"system":{"get_sysinfo":{}}}' | ./kasa <device-addr>

The tool establishes a TCP connection on port 9999 to the Kasa device specified on the command line by host name or IP address. Each line that is written to the tool's standard input is sent as a separate request to the connected Kasa device, and the tool writes each response received from the device as a separate line to its standard output.

Tip: You can pipe the output of the tool into jq to extract the information that interests you.

Example in Bash

#!/bin/bash

if [[ ! -v 1 ]] ; then
	echo 'Toggles the relay state of a Kasa plug/switch.'
	echo
	echo "usage: ${0} <device-addr> [...]"
	exit 1
fi >&2

kasa_call() {
	local - ; set -o pipefail
	echo "${2:?}" | "${KASA:-./kasa}" "${1:?}" |
		jq -r "if [ .[][].err_code != 0 ] | any then halt_error else ${3:-empty} end"
}

for each ; do
	{ read -r alias ; read -r relay_state ; } < <(
		kasa_call "${each}" '{"system":{"get_sysinfo":{}}}' \
			'.system.get_sysinfo | ( .alias, .relay_state )')
	case "${relay_state}" in
		0)
			echo "${alias} is switched off; switching on."
			kasa_call "${each}" '{"system":{"set_relay_state":{"state":1}}}'
			;;
		1)
			echo "${alias} is switched on; switching off."
			kasa_call "${each}" '{"system":{"set_relay_state":{"state":0}}}'
			;;
		*)
			exit "${?}"
			;;
	esac
done

Disclaimer

The information and code contained in this repository is provided “as is,” without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the information and code contained in this repository or the use or other dealings therewith.

The information contained in this repository may be incorrect or incomplete. Any use of what you find here is undertaken solely at your own risk.

Kasa is a registered trademark of the TP-Link Research America Corporation. TP-Link have not endorsed or contributed to this repository and are not affiliated with it in any way.

kasa's People

Contributors

whitslack avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.