Olen/Spond

What's the best way to get a single group/event?

Closed this issue · 3 comments

Question #58 (closed) made me realise I don't actually know how best to use get_group(uid) etc. to get a single group/event/member, due to unfamiliarity with asyncio.

So far I have been using the code as per the README to get all groups or a list of events, then I look within the returned list to work with the entity I need.

@Olen I think it would be good to add an example to the README. asyncio does make it a bit more difficult to get started.

Olen commented

The main reason I used asyncio was because I had some ideas about integrating Spond into Home Assistant, where everything is async. If you want to build a bigger application with lots of stuff going on, or that run things in the background, asyncio is very handy, as you can fire off a function, and just let it do it's thing while the main thread is free to do something else.

Asyncio might seem intimidating in the beginning, but for basic stuff, it is quite easy to follow the examples from e.g. groups.py, and just remeber to prefix functions that use the API with "async def ..." and to "await" all API-calls and all calls to said functions.

Bu I can add a line or two in the readme saying about the same as above, if that makes things clearer.

Thanks @Olen. In the interests of 'show rather than tell', how about adding this as the first example in the README? Is it the simplest non-looping form?

import asyncio
from spond import spond

username = 'my@mail.invalid'
password = 'Pa55worD'
group_id = 'C9DC791FFE63D7914D6952BE10D97B46'  # fake 

async def main():
    s = spond.Spond(username=username, password=password)
    group = await s.get_group(group_id)
    print(group['name'])
    await s.clientsession.close()

asyncio.run(main())
Olen commented

You are right. Python < 3.7 requires the loop-functions. But from 3.7 it is no longer needed. I'll update the example.