pyscript
pyscript copied to clipboard
async await issue depending on script environment
2024-10-11 22:03:31.301 DEBUG (MainThread) [custom_components.pyscript.eval] file.lights-room.lights_room: calling __aexit__(<aiohttp.client.ClientSession object at 0x7f511cca10>, <class 'TypeError'>, object dict can't be used in 'await' expression, <traceback object at 0x7f3ed66600>, {})
I have matching aiohttp versions, running Python 3.12.4 inside a venv on a Mac:
@service
async def lights_room():
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session:
try:
async with session.get(
f"{uri}/endpoint",
headers={...}
) as response:
response.raise_for_status()
data = await response.json()
# ^ this await is required when running the script on a Mac. However pyscript throws an error here.
The pyscript environment requires me to remove the await in the response.json() line, otherwise I get an error mentioned above. Running the script on my laptop requires the awaot. I read somewhere that the awaits are "optional", but doesn't seem so.
I run the script on the laptop via asyncio.run(lights_room()), which I guess is not the same way pyscript does it?
anyway the current workaround is to adjust the script for the pyscript env and remove the awaits.