Skip to content

kasa.exceptions.SmartDeviceException: Communication error on system:set_relay_state #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
OutsourcedGuru opened this issue Jun 9, 2024 · 4 comments

Comments

@OutsourcedGuru
Copy link

kasa, version 0.6.2.1
Exception: Communication error on system:set_relay_state
Python 3.9.7
MacOS 12.6.3

Example code:

import asyncio
from kasa import SmartPlug
from pprint import pformat as pf

charger = "192.168.11.65"
plug = SmartPlug(charger)
# plug.timeout = 20
asyncio.run(plug.update())
# print("Hardware: %s" % pf(plug.hw_info))
# print("Full sysinfo: %s" % pf(plug.sys_info))
if (plug.is_on):
    asyncio.run(plug.turn_off())
else:
    print("Plug was already OFF")

If the plug is off then it simply displays the print() statement as expected. Uncommenting out the two print() statements before this works as expected. If the plug is off then the turn_off() method throws the exception as indicated. Adding a timeout of 20 seconds doesn't change this condition.

Things work fine using the kasa CLI.

@OutsourcedGuru
Copy link
Author

I've included here the output from those two print statements so that you can identify the hardware.

Hardware: {'dev_name': 'Smart Wi-Fi Plug Mini',
 'fwId': '00000000000000000000000000000000',
 'hwId': 'redacted',
 'hw_ver': '1.0',
 'mac': 'redacted',
 'oemId': 'redacted',
 'sw_ver': '1.5.6 Build 191114 Rel.104204',
 'type': 'IOT.SMARTPLUGSWITCH'}
Full sysinfo: {'active_mode': 'none',
 'alias': 'Charger',
 'dev_name': 'Smart Wi-Fi Plug Mini',
 'deviceId': 'redacted',
 'err_code': 0,
 'feature': 'TIM',
 'fwId': '00000000000000000000000000000000',
 'hwId': 'redacted',
 'hw_ver': '1.0',
 'icon_hash': '',
 'latitude_i': redacted,
 'led_off': 0,
 'longitude_i': redacted,
 'mac': 'redacted',
 'model': 'HS105(US)',
 'next_action': {'type': -1},
 'oemId': 'redacted',
 'on_time': 0,
 'relay_state': 0,
 'rssi': -65,
 'sw_ver': '1.5.6 Build 191114 Rel.104204',
 'type': 'IOT.SMARTPLUGSWITCH',
 'updating': 0}

@rytilahti
Copy link
Member

rytilahti commented Jun 9, 2024

You have to run all I/O (i.e., commands that communicate with the device, accessing properties like hw_info do not cause any I/O) inside the same event loop due to how the library is designed, and each individual call to asyncio.run() creates a new event loop which breaks the connection.

The simplest way to fix this it to wrap your code inside a separate async function you execute using asyncio.run(), and use await inside of it to call coroutines. Try this:

import asyncio
from kasa import SmartPlug
from pprint import pformat as pf

async def main():
    charger = "192.168.11.65"
    plug = SmartPlug(charger)
    # plug.timeout = 20
    await plug.update()
    # print("Hardware: %s" % pf(plug.hw_info))
    # print("Full sysinfo: %s" % pf(plug.sys_info))
    if (plug.is_on):
        await plug.turn_off()
    else:
        print("Plug was already OFF")
        
if __name__ == "__main__":
    asyncio.run(main())

@OutsourcedGuru
Copy link
Author

You have to run all I/O (i.e., commands that communicate with the device, accessing properties like hw_info do not cause any I/O) inside the same event loop due to how the library is designed, and each individual call to asyncio.run() creates a new event loop which breaks the connection.

The simplest way to fix this it to wrap your code inside a separate async function you execute using asyncio.run(), and use await inside of it to call coroutines. Try this:

Thank you. I wonder why none of the documentation shows a simple wrapping method like this? Here's the README I'm referring to. Click on the "Plugs" link and there's essentially a stub for the (deprecated) kasa.SmartPlug class. Select the Library Usage -> Getting Started menu item on the left. There is some REPL-related activities for discovering. Move on to the How-to Guides and look at the "Connect without discovery" option. I wouldn't call that a useful example. Visit the API Reference -> Device. Again, not very helpful.

See, the problem I see as someone with more than four decades of experience is that everything worked just great about ten years ago coding to my TP-Link devices. I decided to use some code this week to remote control things on my solar system and suddenly everything has changed in that period of time. That is to be expected. I'm reasonably certain that the async was added in this timeframe. It would be lost on the user of this library that the connection is lost in this circumstance.

I would suggest minimally including an actual python script example for directing a single, known device to toggle ON or OFF. I'd also suggest to the coder in the README that the entire async session must happen in concert rather than individually-wrapped. I have read countless pseudo-tutorials online today and nobody has actually provided a working script. Mostly what they're doing is parroting snippets from your documentation. I rather doubt that they've actually controlled their own SmartPlugs, for example.

Thanks. I'll close this issue. I'm hoping that the docs get some McLovin.

@rytilahti
Copy link
Member

Agreed, the library is currently in flux but we have long planned to improve the docs for the next release (see #779 #755 #979). There is already an open PR by @sdb9696 to add some more documentation which should also help, feel free to take a look at let us know in the PR comments if you find something that feels odd: #968

Any help to improve the docs is also obviously welcome! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants