softScheck / tplink-smartplug

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

Support for multi device KP200 #88

Open jbuehl opened 3 years ago

jbuehl commented 3 years ago

I just acquired a KP200 wall outlet which has two sockets that can be controlled independently. The TP-Link protocol has been extended to support this device and I was able to discover how it works and share it here.

After configuring the device with the Kasa app on my phone I sent the message {"system":{"get_sysinfo":{}} to get the system information which returned

 {"system":{"get_sysinfo":{   
    "child_num": 2,
    "deviceId": "8006260281786589DEAD9EB7BEEF387B1DCFE19C",
    "alias": "TP-LINK_Smart Plug_2155",
    "children": [
        {   "next_action": {"type": -1},
            "state": 0,
            "on_time": 0,
            "alias": "Barbecue lights",
            "id": "8006260281786589DEAD9EB7BEEF387B1DCFE19C00"},
        {   "next_action": {"type": -1},
            "state": 0,
            "on_time": 0,
            "alias": "Patio lights",
            "id": "8006260281786589DEAD9EB7BEEF387B1DCFE19C01"}],
    "longitude_i": -1194024,
    "latitude_i": 331491,
    "hw_ver": "1.0",
    "mac": "40:3F:8C:E8:21:55",
    "ntc_state": 0,
    "model": "KP200(US)",
    "led_off": 0,
    "oemId": "04F52C33845539AA35826EBC43BF5EDE",
    "sw_ver": "1.0.7 Build 190309 Rel.180427",
    "updating": 0,
    "feature": "TIM",
    "err_code": 0,
    "rssi": -60,
    "hwId": "8BE782A7B45D9C9F19EB2F39A361820E",
    "mic_type": "IOT.SMARTPLUGSWITCH"}
}}

The two sockets are described in the children item. Each one contains an id item that consists of the main deviceId with a 2 digit number appended, as well as an alias and a state. The alias and relay_state items are not present in the main block.

To control an individual socket, the set_relay_state message contains an additional item context which contains the id of the socket to turn on or off.

{"system":{
    "set_relay_state":{"state":1}},
    "context":{"child_ids":["8006260281786589DEAD9EB7BEEF387B1DCFE19C00"]

child_ids specifies a list, so it is possible to control more that one socket at a time.

I hope this is useful. My implementation of this can be found at https://github.com/jbuehl/ha/ha/interfaces/tplinkInterface.py