Extending from this Reddit thread and linked artcle, I found updated information on the TP-Link API and how to use it. I only have used it with my single smart bulb, but it might be helpful for other TP-Link products. The guide should be intermediate level, but it contains a lot of verbiage; just expect to tinker a bit. It took me a while to figure this out from scratch, but it shouldn't take longer than 30 minutes to your first applet.
Tools Used:
- Android Phone (to capture packets without jailbreak or another device to intercept, I've not looked for other platforms' apps)
- Kasa App
- Packet Capture
- IFTTT Webhooks
Requirements and Cautions:
- Understand you are sharing your API token to your Kasa account and could result in someone changing your lights or worse if they got ahold of it
- Understand that a packet sniffer could theoretically send personal information to another server
- Understand that you are using TP-Link's private API without their consent
- You must have Kasa's "Remote Control" feature enabled for the devices you are going to automate
Your API Token and Device IDs:
- Install Packet Capture (make sure it has access to your SSL packets by adding the certificate it requests you to add if you face any issues, you can remove this later to remove the warning shown in quick settings)
- Start recording your traffic in Packet Capture by pressing the green play button in the app bar
- Navigate to the Kasa app and power on or off the bulb you'd like to use
- Switch back to the Packet Capture app and stop recording, the faster the better to eliminate extra packets
- Search through all packets from Kasa until you find something like the following in your headers:
POST /?token=[YOUR-TOKEN-HERE]&[...]
Host: use1-wap.tplinkcloud.com
- Make sure you can create a URL like the following from your token and host (in case the host changes in the future):
https://use1-wap.tplinkcloud.com/?token=YOUR-TOKEN-HERE
- Also, find a send packet like the following, the important part is your deviceId:
{
"method": "passthrough",
"params": {
"deviceId": "YOUR-ID-HERE",
"requestData": "{\"smartlife.iot.smartbulb.lightingservice\":{\"transition_light_state\":{\"brightness\":100,\"color_temp\":3500,\"ignore_default\":0,\"mode\":\"normal\",\"on_off\":1,\"transition_period\":2500}}}"
}
}
- Save the required packets as text files so you can easily copy them or send them to a desktop
IFTTT Webhooks:
- Create an applet with IFTTT Webhooks as the "That" portion (you can use anything for the "This")
- For the URL use your URL you created like (you may have to change your token in IFTTT if your token expires):
https://use1-wap.tplinkcloud.com/?token=YOUR-TOKEN-HERE
- For the method, use POST and application/json for the content type
- The body should be the JSON object that includes your device ID as well as any thing you want to see changed on the bulb
- Before completing the applet, make sure there aren't two brackets next to each other like
{{
or}}
, separate them with a space or carriage return to not confuse IFTTT
The API Docs:
Formatted as a JSON object, with an escaped object as a string for the requestData
property, it includes the device to be selected as well as all the data sent to that device. Some properties only change while the light is being turned on or is already on. You can emulate anything that can be done in the Kasa app by performing it in Kasa and then checking Packet Capture. If you can contribute to this or provide information on unknowns, please comment!
Root Level:
"method": "passthrough"
- Unknown"params": {}
- An object with your ID and data
Params:
"deviceId": "YOUR-ID-HERE"
- How you tell TP-Link which device to target"requestData": "[...]"
- Data to be sent to the device
Request Data > Transition Light State:
on_off
- Number, state for the device. 0 = off, 1 = on. No color, brightness, etc is require to toggle the light.transition_period
- Number, number of milliseconds to transition the state (on_off); brightness; hue; and saturation.brightness
- Number, range from 0 to 100 for the brightness of the device.color_temp
- Number, range from unknown to unknown for the color temperature in kelvins. Incompatible with hue and saturation.hue
- Number, range from 0 to 360 for the color hue. Incompatible with color_temp, to be used with saturation.saturation
- Number, range from 0 to 100 for the color saturation. Incompatible with color_temp, to be used with hue.ignore_default
- Unknownmode
- Unknown
Links:
Related, check https://github.com/adumont/tplink-cloud-api