softScheck / tplink-smartplug

TP-Link WiFi SmartPlug Client and Wireshark Dissector
Apache License 2.0
1.13k stars 296 forks source link

KP400 dual port control #83

Open darouss99 opened 3 years ago

darouss99 commented 3 years ago

Hi,

I have a KP400 smartswitch. This is a outdoor unit with 2 individually controlable output.

Is there a way to control the port 1 and 2 individually? I'm able to do it by the Kasa app but not using the script.

If I use this command, it control both port. "tplink_smartplug.py -t 192.168.0.221 -c on"

Thank you

sylvandb commented 3 years ago

You have to add a "context" section to the json command, specifying the child ID or IDs you wish to control.

I do it with this method, where cmd is the normal command json text, child_id is the uuid of the child (I think the index also works, see the result of info command on your KP400) and this method returns it unaltered if no child_id is specified, or returns the complete context with child_id embedded: def add_child_context(cmd, child_id=None, **kwargs): return '{"context": {"child_ids": ["%s"]}, %s' % (child_id, cmd[1:]) if child_id else cmd

darouss99 commented 3 years ago

Thank you for your answer.

I'm a very beginner at Python. So if I understand correctly I must add that string in tplink_smartplug.py def add_child_context(cmd, child_id=None, **kwargs): return '{"context": {"child_ids": ["%s"]}, %s' % (child_id, cmd[1:]) if child_id else cmd

The command should look like what?

If I do an "Info" on this smart switch this is the answer : (I anonymized it)

python tplink_smartplug.py -t 192.168.0.221 -c info Sent: {"system":{"get_sysinfo":{}}} Received: {"system":{"get_sysinfo":{"sw_ver":"1.0.6 Build 200821 Rel.090909","hw_ver":"2.0","model":"KP400(US)","deviceId":"----a_40_caracters_ID----","oemId":"----a_32_caracters_ID----","hwId":"----a_32_caracters_ID----","rssi":-30,"longitude_i":-996256,"latitude_i":994660,"alias":"TP-LINK_Smart Plug_C69F","status":"new","mic_type":"IOT.SMARTPLUGSWITCH","feature":"TIM","mac":"----the_mac_add----","updating":0,"led_off":0,"children":[{"id":"----a_42_caracters_ID----","state":1,"alias":"Plug1","on_time":15,"next_action":{"type":-1}},{"id":"----a_42_caracters_ID+1----","state":1,"alias":"Plug2","on_time":15,"next_action":{"type":1,"schd_sec":56400,"action":1}}],"child_num":2,"ntc_state":0,"err_code":0}}}

Thank you

sylvandb commented 3 years ago

Sorry, it's a bit more involved than just adding that to the program. I have a fork of this repo that has diverged too much to be easily merged, but perhaps my version would work for you. https://github.com/sylvandb/tplink-smartplug