Add option for individual outlet control on HS300 and HS303 power strips
This pull requests adds some documentation and basic code to modify the commands and show how to control individual outlets on a TP-Link Kasa power strip.
Overview
As it is, this script can control power strips as a single entity. All switches on/off, timer/schedule set for all outlets, etc. By adding a specially formatted piece of data you can control outlets individually.
Existing commands:
-
{"system":{"set_relay_state":{"state":1}}} -
{"system":{"set_relay_state":{"state":0}}}
New commands for HS300/303
-
{"context":{"child_ids":["8006...E101"]}, "system":{"set_relay_state":{"state":1}}} -
{"context":{"child_ids":["8006...E101"]}, "system":{"set_relay_state":{"state":0}}}
child_ids
Child IDs are 42 character long Hexadecemal strings. You can get these values by running the info command on the power strip.
For instance, I have the HS303 power strip and when I run {"system":{"get_sysinfo":{}}} it will return the following JSON:
{
"system": {
"get_sysinfo": {
"sw_ver": "1.0.9 Build 191031 Rel.095941",
"hw_ver": "1.0",
"model": "KP303(US)",
"deviceId": "8006243B...AAE1",
.
.
.
"led_off": 0,
"children": [
{
"id": "8006243B...AAE100",
"state": 1,
"alias": "Do not turn off!!!",
"on_time": -3372260,
"next_action": {
"type": -1
}
},
{
"id": "8006243B...AAE101",
"state": 0,
"alias": "Plug 2",
"on_time": 545,
"next_action": {
"type": -1
}
},
{
"id": "8006243B...AAE102",
"state": 1,
"alias": "Heat Pad",
"on_time": 36062,
"next_action": {
"type": -1
}
}
],
"child_num": 3,
"ntc_state": 0,
"err_code": 0
}
}
}
Some lines have been omitted to shorten the length
If you look under "system" > "get_sysinfo" > "children" you can see the ID and alias for each of the outlets on your power strip.
These child IDs should be just the deviceId + a 2 digit number representing the position of the outlet on your device (00 - 06 or 00 - 02 depending on which one you have)
Multiple Control
You can actually control multiple outlets at once by specifying multiple IDs in the command JSON array.
Only outlet 1: {"context":{"child_ids":["8006...E101"]}, "system":{"set_relay_state":{"state":0}}}
Only outlet 1 and 2: {"context":{"child_ids":["8006...E101","8006...E102"]}, "system":{"set_relay_state":{"state":0}}}
@AMDHome, Have you been able to set multiple child_ids at the same time or does it just change the state of the first child_id in the list?
I haven't used it in a while since the project I used this for ended, but if you craft the json by hand using the -j option you can add as many outlets as the strip has to the "child_id" array.
If you are just using the -o flag that I implemented here as proof of concept, then no. You can only change one outlet at a time with the -o flag.
You could change the code to allow for multiple -o flags and put them all in the "child_id" array in the json, but I was lazy when writing it and didn't do that