FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

kasa.exceptions.SmartDeviceException: Communication error on system:set_relay_state · Issue #965 · python-kasa/python-kasa · GitHub

Repository navigation

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

Closed

Description

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.

Activity

  1. OutsourcedGuru commented on Jun 9, 2024

    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}
    
  2. rytilahti commented on Jun 9, 2024

    Member

    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())
    
  3. OutsourcedGuru commented on Jun 10, 2024

    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.

  4. rytilahti commented on Jun 14, 2024

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL