python-kasa / python-kasa

🏠🤖 Python API for TP-Link smarthome products
https://python-kasa.readthedocs.io/en/stable/
Other
1.23k stars 201 forks source link

RuntimeError: Event loop is closed on WSL #294

Closed LitBomb closed 2 years ago

LitBomb commented 2 years ago

I am running:

Every time I call: kasa --type bulb --host <blubIP> hsv 0 100 100

I get the following exception. The bulb does turn to red though. Am I missing something? I also get other errors running kasa discovery. Thanks.

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000015646DAE8C0>
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
rytilahti commented 2 years ago

Looks like windows specific issue of event loop not being closed automatically, needs more investigation to figure out what's the best approach to fix this.

Forget that, it looks like the event loop is closed already and something tries to close it again. @LitBomb is that all output you are getting? There is no code lines in that traceback from this library, so I'm wondering what is happening..

LitBomb commented 2 years ago

@rytilahti Yes, that is all I get from the PowerShell console output when I run the bulb color command. I also get errors doing discovery. See below. I want to mention that I have two HS300 power strips (firmware version 1.0.21) and five KP115 plugs (firmware version 1.0.18) and a KL135 blub (firmware version 1.0.10).

Please let me know if I can get you more debug output. Thanks for looking into this.

PS C:\Windows\system32> kasa discover
Discovering devices on 255.255.255.255 for 3 seconds
== Hot Water Kettle - KP115(US) ==
        Host: 192.168.1.242
        Device state: ON

        == Generic information ==
        Time:         2022-01-29 16:26:33
        Hardware:     1.0
        Software:     1.0.18 Build 210910 Rel.141202
        MAC (rssi):   00:5F:67:xx:xx:xx (-48)
        Location:     {'latitude': 4x.xxxx, 'longitude': -1xx.xxxx}

        == Device specific information ==
        LED state: True
        On since: 2022-01-28 12:53:34.333400

        == Current State ==
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts\kasa.exe\__main__.py", line 7, in <module>
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 1150, in __call__
    return anyio.run(self._main, main, args, kwargs, **({"backend":_anyio_backend} if _anyio_backend is not None else {}))
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\anyio\_core\_eventloop.py", line 56, in run
    return asynclib.run(func, *args, **backend_options)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\anyio\_backends\_asyncio.py", line 233, in run
    return native_run(wrapper(), debug=debug)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 641, in run_until_complete
    return future.result()
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\anyio\_backends\_asyncio.py", line 228, in wrapper
    return await func(*args)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 1153, in _main
    return await main(*args, **kwargs)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 1074, in main
    rv = await self.invoke(ctx)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 1684, in invoke
    return await _process_result(await sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 1420, in invoke
    return await ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 774, in invoke
    rv = await rv
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kasa\cli.py", line 162, in discover
    await ctx.invoke(state)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 774, in invoke
    rv = await rv
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asyncclick\core.py", line 774, in invoke
    rv = await rv
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kasa\cli.py", line 231, in state
    emeter_status = dev.emeter_realtime
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kasa\smartdevice.py", line 87, in wrapped
    return f(*args, **kwargs)
  File "C:\Users\windowsusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kasa\smartdevice.py", line 432, in emeter_realtime
    return EmeterStatus(self._last_update[self.emeter_type]["get_realtime"])
KeyError: 'emeter'
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001E01A9AE8C0>
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 745, in call_soon
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed
PS C:\Windows\system32>
K2Designs commented 2 years ago

I am also seeing this error using discover.

Python code (from https://python-kasa.readthedocs.io/en/latest/discover.html):

import asyncio
from kasa import Discover

devices = asyncio.run(Discover.discover())
for addr, dev in devices.items():
    asyncio.run(dev.update())
    print(f"{addr} >> {dev}")

Output:

F:\Development\Smart_Plugs\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 64650 --file F:/Development/Smart_Plugs/test1.py
pydev debugger: process 95440 is connecting

Connected to pydev debugger (build 193.6015.41)
192.168.1.4 >> <DeviceType.Strip model KP400(US) at ip_address (TP-LINK_Smart Plug_B8D1), is_on: False - dev specific: {'LED state': True, 'Childs count': 2, 'On since': None}>
192.168.1.9 >> <DeviceType.Plug model HS200(US) at ip_address (Garage Outdoor), is_on: False - dev specific: {'LED state': True, 'On since': None}>
192.168.1.5 >> <DeviceType.Strip model KP400(US) at ip_address (TP-LINK_Smart Plug_39F8), is_on: False - dev specific: {'LED state': False, 'Childs count': 2, 'On since': None}>
192.168.1.10 >> <DeviceType.Plug model HS103(US) at ip_address (Hallway Lamp), is_on: False - dev specific: {'LED state': True, 'On since': None}>
192.168.1.15 >> <DeviceType.Plug model HS200(US) at ip_address (Outdoor), is_on: False - dev specific: {'LED state': True, 'On since': None}>
192.168.1.18 >> <DeviceType.Plug model HS200(US) at ip_address (Outdoor3), is_on: False - dev specific: {'LED state': True, 'On since': None}>
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0429C778>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 715, in call_soon
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0429C778>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 715, in call_soon
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0429C778>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 715, in call_soon
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0429C778>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 715, in call_soon
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0429C778>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 715, in call_soon
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0429C778>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 715, in call_soon
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed

Process finished with exit code 0
Buggitt commented 2 years ago

Also running into a similar error doing anything with my outlet(s) Python 3.10.1 kasa, version 0.4.1 Plug KP115 firmware 1.0.7

C:\Users\HypeMachine>kasa --host 192.168.0.22
No --type defined, discovering..
== plug 5 - KP115(US) ==
        Host: 192.168.0.22
        Device state: ON

        == Generic information ==
        Time:         2022-01-31 18:47:45
        Hardware:     1.0
        Software:     1.0.7 Build 200825 Rel.100128
        MAC (rssi):   [REDACTED] (-50)
        Location:     {'latitude': [REDACTED], 'longitude': [REDACTED]}

        == Device specific information ==
        LED state: True
        On since: 2022-01-27 12:52:34.373412

        == Current State ==
        <EmeterStatus power=183.998 voltage=122.227 current=1.456 total=57.848>
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000266B20577F0>
Traceback (most recent call last):
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

C:\Users\HypeMachine>kasa --host 192.168.0.22 state
No --type defined, discovering..
== plug 5 - KP115(US) ==
        Host: 192.168.0.22
        Device state: ON

        == Generic information ==
        Time:         2022-01-31 18:48:30
        Hardware:     1.0
        Software:     1.0.7 Build 200825 Rel.100128
        MAC (rssi):   [REDACTED] (-50)
        Location:     {'latitude': [REDACTED], 'longitude': [REDACTED]}

        == Device specific information ==
        LED state: True
        On since: 2022-01-27 12:52:34.429749

        == Current State ==
        <EmeterStatus power=190.949 voltage=122.194 current=1.559 total=57.85>
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000183277D77F0>
Traceback (most recent call last):
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

C:\Users\HypeMachine>kasa --host 192.168.0.22 emeter
No --type defined, discovering..
== Emeter ==
Current: 1.556 A
Voltage: 122.207 V
Power: 189.222 W
Total consumption: 57.851 kWh
Today: 18.787 kWh
This month: 202.363 kWh
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000190E98077F0>
Traceback (most recent call last):
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

C:\Users\HypeMachine>kasa --host 192.168.0.22 --type plug emeter
== Emeter ==
Current: 1.561 A
Voltage: 122.145 V
Power: 189.331 W
Total consumption: 57.853 kWh
Today: 18.788 kWh
This month: 202.364 kWh
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000266831777F0>
Traceback (most recent call last):
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\HypeMachine\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

C:\Users\HypeMachine>
AgentH207 commented 2 years ago

I get this error message too althoug I use version python-kasa 0.5.0 Shouldn't that be fixed already?

Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001F4DF1881F0> Traceback (most recent call last): File "C:\Users\andre\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del self.close() File "C:\Users\andre\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\andre\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon self._check_closed() File "C:\Users\andre\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

rytilahti commented 2 years ago

See the linked issue #315