Hello there! Thank you for this library; it has been immensely useful in automating my smart home full of mostly TPLink devices.
I am having an issue simply querying and printing several of a bulb's properties using the current version of python-kasa. I checked the documentation and my code seems to be in line with the examples.
I'm running this code in a clean VM with only python-kasa installed using sudo pip3 install python-kasa
Ubuntu 20.04.3 LTS
Python 3.8.10
kasa, version 0.4.0
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/kasa/protocol.py", line 138, in _query
return await asyncio.wait_for(
File "/usr/lib/python3.8/asyncio/tasks.py", line 494, in wait_for
return fut.result()
File "/usr/local/lib/python3.8/dist-packages/kasa/protocol.py", line 99, in _execute_query
packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE)
File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
await self._wait_for_data('readexactly')
File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
await self._waiter
RuntimeError: Task <Task pending name='Task-11' coro=<TPLinkSmartHomeProtocol._execute_query() running at /usr/local/lib/python3.8/dist-packages/kasa/protocol.py:99> cb=[_release_waiter(<Future pendi...0bdabfc10>()]>)() at /usr/lib/python3.8/asyncio/tasks.py:429]> got Future <Future pending> attached to a different loop
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./bulb.py", line 13, in <module>
asyncio.run(bulb.update())
File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/usr/local/lib/python3.8/dist-packages/kasa/smartdevice.py", line 302, in update
self._last_update = await self.protocol.query(req)
File "/usr/local/lib/python3.8/dist-packages/kasa/protocol.py", line 71, in query
return await self._query(request, retry_count, timeout)
File "/usr/local/lib/python3.8/dist-packages/kasa/protocol.py", line 142, in _query
await self.close()
File "/usr/local/lib/python3.8/dist-packages/kasa/protocol.py", line 115, in close
writer.close()
File "/usr/lib/python3.8/asyncio/streams.py", line 353, in close
return self._transport.close()
File "/usr/lib/python3.8/asyncio/selector_events.py", line 692, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/usr/lib/python3.8/asyncio/base_events.py", line 719, in call_soon
self._check_closed()
File "/usr/lib/python3.8/asyncio/base_events.py", line 508, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
At time of error, my counter (count) prints 2
Output of kasa --host 10.10.10.123 --bulb
== Desk Lamp - KL130(US) ==
Host: 10.10.10.123
Device state: ON
== Generic information ==
Time: 2021-10-01 07:55:57
Hardware: 2.0
Software: 1.0.12 Build 210329 Rel.141126
MAC (rssi): (sanitized) (-65)
Location: {'latitude': xx.xxxx, 'longitude': xx.xxxx}
== Device specific information ==
Brightness: 100
Is dimmable: True
Color temperature: 0
Valid temperature range: ColorTempRange(min=2500, max=9000)
HSV: HSV(hue=110, saturation=4, value=100)
== Current State ==
<EmeterStatus power=10.8 voltage=None current=None total=0.573>
On a side note, when I install(ed) python-kasa, there is an error with requests and idna:
sudo pip3 install python-kasa --force-reinstall
ERROR: requests 2.25.1 has requirement idna<3,>=2.5, but you'll have idna 3.2 which is incompatible.
Also, on my "prod" machine, I use the code from PR-117 installed and this code functions without issue, although a lot of newer HW versioned switches don't show up (out of scope).
So, the current version of this library will re-use an existing TCP session to optimize the updates, which is especially useful for devices like HS300 which require multiple requests for the energy consumption information. In your example code you are calling asyncio.run() once per loop, which internally creates a new event loop for each iteration and this causes the exception you are seeing.
Hope that helps, feel free to open separate issues for the other things you mentioned. I don't think that idna/requests warning is caused by python-kasa as we don't depend (at least directly) on those, but it's still worth verifying.
Activity
rytilahti commented on Oct 1, 2021
Hi there!
So, the current version of this library will re-use an existing TCP session to optimize the updates, which is especially useful for devices like HS300 which require multiple requests for the energy consumption information. In your example code you are calling asyncio.run() once per loop, which internally creates a new event loop for each iteration and this causes the exception you are seeing.
Here's the preferred way to do it:
#!/usr/bin/env python3 import os import time import asyncio from kasa import SmartBulb async def show_bulb_state(host): bulb = SmartBulb(host) count = 0 while True: count = count + 1 await bulb.update() os.system('clear') print ('\n\n') print (f'\tCount: {count}\n') print (f'\tPower: {"On" if bulb.is_on else "Off"}') print (f'\tHue: {bulb.hsv[0]}') print (f'\tSat: {bulb.hsv[1]}') print (f'\tVol: {bulb.hsv[2]}') print (f'\tTemp: {bulb.color_temp}') time.sleep(1) if __name__ == "__main__": asyncio.run(show_bulb_state("10.10.10.123"))Hope that helps, feel free to open separate issues for the other things you mentioned. I don't think that idna/requests warning is caused by python-kasa as we don't depend (at least directly) on those, but it's still worth verifying.
jasonlboggs commented on Oct 4, 2021
This worked. Thank you!
Prepare 0.4.1
Prepare 0.4.1 (#288)
Prepare 0.4.1 (python-kasa#288)