LA_
LA_

Reputation: 20409

How to properly use await with pyscript?

Here is my simplified pyscript -

async def auth(brand):
    async with aiohttp.ClientSession() as session:
        async with session.post(url_auth) as resp:
            ...
            return auth_token_b64

@service
def get_counters(address_id):
    auth_token_b64 = await auth(brand)

    ...

It worked well when I used it locally with asyncio.run(get_counters(address_id=1)).

But now I've uploaded the file to Home Assistant and there I get the following error -

return auth_token_b64

TypeError: object str can't be used in 'await' expression

What is wrong here?

Upvotes: 0

Views: 803

Answers (2)

Colin Le Nost
Colin Le Nost

Reputation: 490

found myself with the same error. From pyscript doc, you can see that await keyword is optional.

Since pyscript is async, it detects whether functions are real or async, and calls them in the correct manner. So it's not necessary to use async and await in pyscript code - they are optional.

Removing it from the script fixed the issue for me.

Upvotes: 0

Grimlock
Grimlock

Reputation: 1091

It looks like you're missing the async keyword before your get_counters function definition. This is causing the await expression inside get_counters to throw an error because it is not in an asynchronous context. To fix the issue, simply add the async keyword before your get_counters function definition:

@service
async def get_counters(address_id):
    auth_token_b64 = await auth(brand)

Can you try this?

Upvotes: 0

Related Questions