🔊 sonosify

Async Python API for Sonos speakers. Find a room by name, control it, react to
what it does, and play your own audio over the music. Everything talks directly
to the speakers on your local network: no cloud account, no API keys.
Installation
Requires Python 3.13 or 3.14 and speakers on the same network. Discovery uses
UDP multicast.
uv add sonosify
# or: pip install sonosify
Quickstart
import asyncio
from sonosify import SonosController
async def main():
async with SonosController() as sonos:
kitchen = await sonos.speaker("Kitchen")
await kitchen.set_volume(25)
await kitchen.play()
print(await kitchen.now_playing())
for speaker in await sonos.speakers():
print(await speaker.get_room_name(), await speaker.get_volume())
asyncio.run(main())
How it works
| Type |
Role |
SonosController |
Your household. Discovers speakers on first use and owns their connections |
SpeakerClient |
One room. Carries every command |
SonosSystem |
Snapshot of speakers and groups, returned by discover() |
Address rooms and let the controller handle groups. Volume, mute, and audio
clips apply to the room you asked for. Playback, queue, and group volume go to
its group coordinator automatically. After changing groups, call
await sonos.discover() to refresh that routing.
The controller owns its speakers. Asking for the same room twice returns the
same client. All clients, connections, and event streams close with the
async with block.
If you already know the address, await sonos.speaker(ip="192.168.1.42") skips
discovery. SpeakerClient("192.168.1.42") works without a controller and targets
exactly that device.
What a speaker can do
| Area |
Methods |
| Playback |
play, pause, stop, next, previous, seek, now_playing |
| Sources |
open, play_uri, favorites, open_favorite, line_in, tv |
| Volume |
get_volume, set_volume, adjust_volume |
| Group volume |
get_group_volume, set_group_volume, adjust_group_volume |
| Mute and ducking |
get_mute, set_mute, toggle_mute, duck, unduck |
| Queue |
queue, enqueue_uri, seek_queue, remove_queue_item, clear_queue |
| Modes |
set_shuffle, set_repeat, set_crossfade, configure_sleep_timer |
| Groups |
join, unjoin |
| Audio clips |
play_pcm, play_audio_clip, play_audio_clip_data, stream_audio_clip |
| Events |
watch |
Live events
watch() streams typed events as they happen. By default it covers playback and
volume; pass services=ALL_SERVICES for all supported services.
import asyncio
from sonosify import AVTransportEvent, RenderingControlEvent, SonosController
async def main():
async with SonosController() as sonos:
async for event in sonos.watch("Kitchen"):
match event:
case AVTransportEvent(transport_state=state, track=track):
print(state, track.title if track else "")
case RenderingControlEvent(volume=volume, muted=muted):
print(volume, muted)
asyncio.run(main())
- Fields can be
None. An event only carries the values that changed.
- Subscriptions renew themselves. If renewal fails, the stream yields
SubscriptionLost, then SubscriptionRestored once it recovers. Events in
between are lost, so re-read the state you need.
- Stopping early: wrap the stream in
contextlib.aclosing() to unsubscribe
as soon as you break.
- Speakers connect back to your machine. Your firewall must allow it. Set
callback_host or callback_port if the automatic choice does not fit.
Audio clips
A clip plays over the current music and restores it afterwards, which suits
text-to-speech, voice agents, and notifications. Pick a method by where the
audio comes from:
| Your audio |
Method |
| PCM chunks from an async iterator |
play_pcm |
| A URL the speaker can reach |
play_audio_clip (without a URL: built-in chime) |
| A complete WAV or MP3 in memory |
play_audio_clip_data |
| A stream you write to yourself |
stream_audio_clip |
from collections.abc import AsyncIterable
from sonosify import AudioClipOptions, ClipPriority, SonosController
async def speak(pcm_chunks: AsyncIterable[bytes]) -> None:
async with SonosController() as sonos:
kitchen = await sonos.speaker("Kitchen")
await kitchen.play_pcm(
pcm_chunks,
sample_rate=24_000,
options=AudioClipOptions(
app_id="com.example.voice-agent",
volume=30,
priority=ClipPriority.HIGH,
),
)
play_pcm expects interleaved little-endian PCM, 16-bit mono by default. It
waits until playback ends and aborts the clip on errors, cancellation, or
timeout. timeout_seconds covers the whole clip and defaults to 60; pass None
for longer audio. Call prepare_audio() ahead of time to cut the startup delay
of the first clip.
If your audio plays outside of Sonos, duck() lowers the music meanwhile and
unduck() restores it.
Errors
Every library error derives from SonosifyError, and error_details() returns a
dictionary with a stable machine-readable code.
Examples
examples/ has runnable scripts, including low-latency PCM
streaming. Usage is at the top of each file.
uv run python examples/discover.py
uv run python examples/now_playing.py Kitchen
uv run python examples/watch.py Kitchen
uv run python examples/group.py join Kitchen "Living Room"
More
Changelog · Contributing · MIT License