From f61382aa38c98ff4d69a6e0c237dd65d0eea59c0 Mon Sep 17 00:00:00 2001 From: noahhusby <32528627+noahhusby@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:39:08 -0400 Subject: [PATCH] docs: add examples --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README.md b/README.md index fffb7f1..9d4eeb5 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,71 @@ If your model is not on the list of supported devices, and everything works corr pip install aiostreammagic ``` +# Examples + +## Basic Example +```python +import asyncio + +from aiostreammagic import StreamMagicClient, Source, Info + +HOST = "192.168.20.218" + + +async def main(): + """Basic demo entrypoint.""" + client = StreamMagicClient("192.168.20.218") + await client.connect() + + info: Info = await client.get_info() + sources: list[Source] = await client.get_sources() + + print(f"Model: {info.model}") + for source in sources: + print(f"Name: {source.id} ({source.id})") + + await client.disconnect() + +if __name__ == '__main__': + asyncio.run(main()) +``` + +## Subscription Example + +The Cambridge Audio StreamMagic API can automatically notify the client of changes instead of the need for polling. Register a callback to be called whenver new information is available. + +```python +import asyncio + +from aiostreammagic import StreamMagicClient + +HOST = "192.168.20.218" + + +async def on_state_change(client: StreamMagicClient): + """Called when new information is received.""" + print(f"System info: {client.get_info()}") + print(f"Sources: {client.get_sources()}") + print(f"State: {client.get_state()}") + print(f"Play State: {client.get_play_state()}") + print(f"Now Playing: {client.get_now_playing()}") + +async def main(): + """Subscribe demo entrypoint.""" + client = StreamMagicClient("192.168.20.218") + await client.register_state_update_callbacks(on_state_change) + await client.connect() + + # Play media using the unit's front controls or StreamMagic app + await asyncio.sleep(60) + + await client.disconnect() + + +if __name__ == '__main__': + asyncio.run(main()) +``` + [license-shield]: https://img.shields.io/github/license/noahhusby/aiostreammagic.svg [downloads-shield]: https://img.shields.io/pypi/dm/aiostreammagic [python-versions-shield]: https://img.shields.io/pypi/pyversions/aiostreammagic