KP303 wifi outlet strip not working with mycodo

I’m having trouble getting a KP303 wifi outlet strip working with Mycodo.

First, I have used the Kasa app to get the power strip on my wifi. It’s connected, I can see the IP address from my router, and control the outlets with the app.

I successfully add it as an output in Mycodo, but when changing the default IP in the configuration, I get a red error message:

Error: Modify Output: Could not connect to Daemon: receiving: timeout

This is what the Mycodo logs show after trying to do this:

2021-10-11 15:06:26,636 - INFO - mycodo.outputs.kp303_552b40a3 - Initialized in 18367.8 ms
2021-10-11 15:06:44,692 - ERROR - mycodo.outputs.kp303_1dc6637a - Output was unable to be setup: Unable to query the device: 
2021-10-11 15:06:46,722 - ERROR - mycodo.outputs.kp303_552b40a3 - Output was unable to be setup: Unable to query the device: 
2021-10-11 15:07:31,404 - ERROR - mycodo.outputs.kp303_552b40a3 - Output was unable to be setup: Unable to query the device: 
2021-10-11 15:07:46,484 - ERROR - mycodo.outputs.kp303_1dc6637a - Output was unable to be setup: Unable to query the device: 

After some playing around I found I could get it working from the commend line. I SSH’d into the pi, and ran the following:

$ sudo pip install "python-kasa"
(install progress text omitted)
$ kasa discover
Discovering devices on 255.255.255.255 for 3 seconds
== TP-LINK_Power Strip_A8C4 - KP303(US) ==
        Host: 192.168.1.16
        Device state: ON

        == Plugs ==
        * Socket 'Plug 1' state: ON on_since: 2021-10-10 15:36:31.410375
        * Socket 'Plug 2' state: ON on_since: 2021-10-10 14:34:51.411362
        * Socket 'Plug 3' state: ON on_since: 2021-10-10 14:34:51.412301

        == Generic information ==
        Time:         2021-10-11 15:15:15
        Hardware:     2.0
        Software:     1.0.3 Build 201015 Rel.173920
        MAC (rssi):   C0:06:C3:3D:A8:C4 (-35)
        Location:     {location info removed}

        == Device specific information ==
        LED state: True
        Childs count: 3
        On since: 2021-10-10 15:36:31.453909

$ kasa --host 192.168.1.16 --strip --plug off
Turning off TP-LINK_Power Strip_A8C4

Is there anything else I can try here?

Please refresh your output page then take a screenshot of your output’s settings.

Have you tried restarting the Pi?

Just did so- for the first time the status of the plugs is showing up. Previously they were all listed as unconfigured.

However when I try to toggle any of the plugs, I get this:
image

Hmm, after playing around one of them did switch state when I hit the button! I got this message:
image

But it seems very hit and miss- five or six of the red error message for every green one.

The python-kasa library has been updated recently and this may have fixed some issues with newer firmwares. My KP303 is working great, but this may fix your issues. Go ahead and upgrade to master and see if it fixes anything.

For reference, the original output module was running python-kasa version 0.4.0.dev3 and the current is 0.4.0.

Upgrade to master seemed to go well. Once it finished I restarted the pi for good measure. Also unplugged the power strip and moved it to another room, in case that helped any ghosts lose it.

Unfortunately no change- it can intermittently switch the plugs on or off, but fails far more often than it succeeds.

Turning things on and off from the command line (as described in my first post) is still working, if that helps narrow things down.

Please enable the Log Level: Debug output option and check the daemon log for a line indicating your device’s sw_ver and hw_ver.

This is the log line from a user experiencing the same issue, from discord:

'sw_ver': '1.0.3 Build 201015 Rel.173920', 'hw_ver': '2.0'

and mine, that doesn’t experience the issue:

'sw_ver': '1.0.10 Build 200828 Rel.162602', 'hw_ver': '1.0'

'sw_ver': '1.0.3 Build 201015 Rel.173920', 'hw_ver': '2.0'

Try downgrading your python-kasa library with the following commands, then restart the daemon and see if that fixes the issue.

cd ~/Mycodo && ./env/bin/pip uninstall python-kasa && ./env/bin/pip install python-kasa==0.4.0.dev4

I discovered that if you installed python-kasa to your system, this is likely the root of the issue. You will need to first uninstall your system package with the commands below, then run the above commands for good measure, so Mycodo can use the virtualenv version it has installed. To uninstall your system package, use this command:

sudo pip uninstall python-kasa

Explanation: version 0.4.0.dev4 and lower of python-kasa works with the module, however 0.4.0.dev5 and 0.4.0 causes it to break. If you install python-kasa to your system, it pulls the latest version (0.4.0) to install. Since Mycodo’s virtualenv is set to use system packages first before virtualenv packages, by installing a system package (0.4.0), you are preventing it from using the version the output module installed (0.4.0.dev4).

After uninstalling system package, and downgrading library to dev2 version, it’s not working at all. None of the plug status indicators ever change from “unconfigured”.

2021-10-12 03:21:16,867 - DEBUG - mycodo.outputs.kp303_fb4a7106 - output_on_off(on, 0, sec, 0.0, 0.0, True)
2021-10-12 03:21:16,869 - ERROR - mycodo.outputs.kp303_fb4a7106 - Cannot manipulate Output fb4a7106-e124-49f6-8035-291d57dc2b04: Output not set up.

After uninstalling dev2 and reinstalling dev4, (and after removing the system package in the step previous) it’s working! Thanks so much for the help.

1 Like

Thought I’d add an update for anybody fighting the same problem here.

I decided I’d start from a fresh Mycodo install, in case my experimenting had screwed anything else up. Found that the default python-kasa version that gets installed when first adding the power strip output (0.4.0.dev3) doesn’t work with my power strip, which is:

'sw_ver': '1.0.3 Build 201015 Rel.173920', 'hw_ver': '2.0'

Changing to version 0.4.0.dev4 by following Kyle’s instructions above fixed the issue.

Thanks for the update. I put a comment in the KP303 module file in Mycodo not to update past dev4 (as a personal note if I ever modify the module) because it will break functionality for some hardware versions. A new module will likely need to be created for the latest library in order for it to work, however I’m not familiar enough with asyncio to know how to properly do that.