-
-
Notifications
You must be signed in to change notification settings - Fork 247
Description
I am converting my home brew web app from pyHS100 to Kasa. I thought the asyncio interface for Kasa would speed it up, but so far I do not see much improvement and in some cases it is slower.
In the initial page render, I get updates from a list of all devices by IP address. The pyHS100 code has to do each one sequentially. With Kasa I am using asyncio.gather() to get the updates concurrently, or so I think. The klights() function below is run from the main thread using asyncio.run(). The question is whether the asyncio.gather is really concurrent or am I mistaken? It does not appear to be much faster than using pyHS100 in a similar sequential fashion.
from kasa import SmartDevice, SmartPlug, SmartStrip, SmartDeviceException
async def update_wrapper(smartdevice):
global logger
try:
await smartdevice.update()
except BaseException as bx:
logger.error("Unable to initialize SmartDevice {}: {}".format(smartdevice.host,str(bx)))
print(bx)
pass
async def klights():
devices = db(db.devices).select(db.devices.host_ip_addr,db.devices.device_type,distinct=True)
smartdevs = []
keys = []
dev_names = []
for device in devices:
if device.device_type == 'plug':
smartdevs.append(SmartPlug(device.host_ip_addr))
else:
smartdevs.append(SmartStrip(device.host_ip_addr))
sdaws = []
for sdev in smartdevs:
sdaws.append(update_wrapper(sdev))
devs = asyncio.gather(*sdaws)
return smartdevs
Lastly, the same web app takes ajax calls to the server to turn devices on/off. In this case the server has to connect to a device, change its state, and query its resulting state. Here kasa definitely seems slower as it must perform extra update()'s to perform the same function with pyHS100. The question is how to reduce the multiple round trips to the device?
pyHS100:
dev = SmartPlug(ip_addr)
dev.turn_on()
state = dev.is_on
Kasa:
dev = SmartPlug(ip_addr)
asyncio.run(dev.update())
asyncio.run(dev.turn_on())
asyncio.run(devupdate())
state = dev.is_on