Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

SyntaxError: 'async with' outside async function #20

Closed
adnan2911 opened this issue Jan 1, 2020 · 3 comments
Closed

SyntaxError: 'async with' outside async function #20

adnan2911 opened this issue Jan 1, 2020 · 3 comments

Comments

@adnan2911
Copy link

Hello
I am using very simple code on raspberry pi using python 3 as below

import aiohttp
import pysmartthings

token = 'xxxxxxx'

async with aiohttp.ClientSession() as session:
        api = pysmarthings.SmartThings(session,token)
        devices = await api.devices()
        print(len(devices))

but i am getting below error
SyntaxError: 'async with' outside async function

Kindly please help
Thanks
Adnan

@posborne
Copy link

posborne commented Jan 6, 2020

Hi @adnan2911, you are correct that the snippet is not complete as provided. With python asyncio you will need to put that fragment inside an async function. You will also need to run the async function on a reactor. Here's a complete example you can play with:

#!/usr/bin/env python3
import aiohttp
import asyncio
import pysmartthings

token = '...'

async def print_devices():
    async with aiohttp.ClientSession() as session:
        api = pysmartthings.SmartThings(session, token)
        devices = await api.devices()
        for device in devices:
            print("{}: {}".format(device.device_id, device.label))


def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(print_devices())
    loop.close()

if __name__ == '__main__':
    main()

@adnan2911
Copy link
Author

Thanks alot Paul, It worked with above code

Regards,
Adnan

@FelixAsch
Copy link

How can I change the mediaInputSource on my SmartTV?
Im new in programming with smartthings, so i don't get your doku.

Regards,
Felix

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants