python-kasa icon indicating copy to clipboard operation
python-kasa copied to clipboard

Detected protocol reuse between different event loop

Open tawseefpatel opened this issue 3 years ago • 1 comments

I just tried the following code via command prompt. I have followed the steps from the examples yet am having this issue being thrown. Running on Windows 10, python-kasa version 0.5.0 `

import asyncio from kasa import * plug = SmartPlug("192.168.1.16") asyncio.run(plug.update()) plug <DeviceType.Plug model KP115(US) at 192.168.1.16 (switch16), is_on: False - dev specific: {'LED state': True, 'On since': None}> asyncio.run(plug.turn_on()) Detected protocol reuse between different event loop {} asyncio.run(plug.turn_off()) Traceback (most recent call last): File "C:\python37\lib\site-packages\kasa\protocol.py", line 157, in _query self._execute_query(request), timeout=timeout File "C:\python37\lib\asyncio\tasks.py", line 442, in wait_for return fut.result() File "C:\python37\lib\site-packages\kasa\protocol.py", line 94, in _execute_query packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE) File "C:\python37\lib\asyncio\streams.py", line 679, in readexactly await self._wait_for_data('readexactly') File "C:\python37\lib\asyncio\streams.py", line 473, in _wait_for_data await self._waiter RuntimeError: Task <Task pending coro=<TPLinkSmartHomeProtocol._execute_query() running at C:\python37\lib\site-packages\kasa\protocol.py:94> cb=[_release_waiter(<Future pendi...87FBB20A8>()]>)() at C:\python37\lib\asyncio\tasks.py:392]> got Future <Future pending> attached to a different loop During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\python37\lib\site-packages\kasa\smartdevice.py", line 251, in _query_helper response = await self.protocol.query(request=request) File "C:\python37\lib\site-packages\kasa\protocol.py", line 73, in query return await self._query(request, retry_count, timeout) File "C:\python37\lib\site-packages\kasa\protocol.py", line 160, in _query await self.close() File "C:\python37\lib\site-packages\kasa\protocol.py", line 110, in close writer.close() File "C:\python37\lib\asyncio\streams.py", line 317, in close return self._transport.close() File "C:\python37\lib\asyncio\selector_events.py", line 663, in close self._loop.call_soon(self._call_connection_lost, None) File "C:\python37\lib\asyncio\base_events.py", line 691, in call_soon self._check_closed() File "C:\python37\lib\asyncio\base_events.py", line 479, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 1, in File "C:\python37\lib\asyncio\runners.py", line 43, in run return loop.run_until_complete(main) File "C:\python37\lib\asyncio\base_events.py", line 587, in run_until_complete return future.result() File "C:\python37\lib\site-packages\kasa\smartplug.py", line 62, in turn_off return await self._query_helper("system", "set_relay_state", {"state": 0}) File "C:\python37\lib\site-packages\kasa\smartdevice.py", line 253, in _query_helper raise SmartDeviceException(f"Communication error on {target}:{cmd}") from ex kasa.exceptions.SmartDeviceException: Communication error on system:set_relay_state `

im looking to add the ability to toggle on/off a plug using a toggle button with a tkinter gui.

tawseefpatel avatar Aug 29 '22 20:08 tawseefpatel

Take a look at my comment here: https://github.com/python-kasa/python-kasa/issues/348#issuecomment-1159810855 - you want to create the eventloop separately, and re-use it whenever needed.

rytilahti avatar Aug 30 '22 20:08 rytilahti