This is a support package for interacting and obtaining information from tp-link Smart Wi-Fi devices.
- HS105 - Smart Plug
- HS200 - Smart Switch
- KP200 - Smart Outlet
- HS300 - Smart Power-strip
- tp-link - Without the products supplied by this company my home automation would rely on cloud based functions. (Yuck!)
- sausheong - Sausheong's hs1xxplug was what the majority of this project was built from, but failed to fully support the HS105 at the time.
- jaedle - The connector written by jaedle was instrumental in helping me understand how tp-link devices communicated.
- go-smarthome - The go-smarthome library from nik-johnson-net had a better implementation of the communication protocol for kasa devices and was adapted into this library to support the HS300.
To import the library locally:
go get github.com/TheSp1der/tplink
To import the library in your code:
import "github.com/TheSp1der/tplink"
To obtain some basic information from the device use the example code below. To return other objects please examine the SysInfo struct.
d := tplink.Tplink{Host: "light-office.tplink.example.com"}
r, err := d.SystemInfo()
if err != nil {
log.Fatal(err)
}
fmt.Println("MAC: " + r.System.GetSysinfo.Mac)
fmt.Println("Name: " + r.System.GetSysinfo.Alias)
if r.System.GetSysinfo.RelayState == 1 {
fmt.Println("Power: On")
} else {
fmt.Println("Power: Off")
}
Will return data:
MAC: 50:C7:BF:##:##:##
Name: Office Light
Power: On
To turn a device on or off:
// turn on
d := tplink.Tplink{
Host: "light-office.tplink.example.com",
}
if err := d.ChangeState(true); err != nil {
log.Fatal(err)
}
// turn off
d := tplink.Tplink{
Host: "light-office.tplink.example.com",
}
if err := d.ChangeState(false); err != nil {
log.Fatal(err)
}
The SwitchID is typically number starting at 0 for the first outlet/switch on the hardware. The following code identifies the first outlet/switch on the device:
// turn on
d := tplink.Tplink{
Host: "light-office.tplink.example.com",
SwitchID: 1,
}
if err := d.ChangeStateMultiSwitch(true); err != nil {
log.Fatal(err)
}
// turn off
d := tplink.Tplink{
Host: "light-office.tplink.example.com",
SwitchID: 1,
}
if err := d.ChangeStateMultiSwitch(false); err != nil {
log.Fatal(err)
}
The tp-link company and products are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. I claim no ownership or control of the tp-link company, products, name, logos or intellectual property.
BSD 2-Clause License
Copyright (c) 2019, TheSp1der All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.