Skip to content

Commit

Permalink
Merge pull request #16 from noahhusby/docs/examples
Browse files Browse the repository at this point in the history
Add examples to docs
  • Loading branch information
noahhusby authored Sep 10, 2024
2 parents 77182d3 + f61382a commit 50d1a4b
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50d1a4b

Please sign in to comment.