Giter VIP home page Giter VIP logo

Comments (4)

OutsourcedGuru avatar OutsourcedGuru commented on July 4, 2024

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}

from python-kasa.

rytilahti avatar rytilahti commented on July 4, 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())

from python-kasa.

OutsourcedGuru avatar OutsourcedGuru commented on July 4, 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:

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.

from python-kasa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.