sonos-sdk
A sync-first, DOM-like SDK for controlling Sonos speakers. Access properties directly on speaker objects with a consistent three-method pattern.
Features
- Sync-First API: All methods are synchronous - no async/await required
- DOM-like Access: Properties accessed directly on speaker objects (
speaker.volume.get()) - Three Access Patterns:
get()for cached,fetch()for fresh,watch()for reactive - Automatic Subscriptions: UPnP subscriptions managed automatically via watch/unwatch
- Type Safety: All properties are strongly typed
- Blocking Iteration: Event loop pattern for reactive applications
Quick Start
use ;
The Get/Fetch/Watch Pattern
Every property on a speaker provides three methods:
get() - Cached Value (Instant)
Returns the cached value without any network calls. Fast and always available.
// Get cached volume - returns Option<Volume>
if let Some = speaker.volume.get
// Get cached playback state
if let Some = speaker.playback_state.get
fetch() - Fresh Value (API Call)
Makes a synchronous API call to the device and updates the cache.
// Fetch fresh volume from device
let volume = speaker.volume.fetch?;
println!;
// Fetch fresh playback state
let state = speaker.playback_state.fetch?;
println!;
watch() - Reactive Updates
Registers for change notifications. Changes appear in system.iter().
// Start watching volume changes
speaker.volume.watch?;
// Start watching playback state
speaker.playback_state.watch?;
// Stop watching when done
speaker.volume.unwatch;
Event Loop Pattern
Build reactive applications by iterating over property changes:
use ;
use Duration;
Non-Blocking Iteration
For applications that need to check for events without blocking:
// Check for events without blocking
for event in system.iter.try_iter
// Wait with timeout
if let Some = system.iter.recv_timeout
Available Properties
Audio Control (RenderingControl)
| Property | Type | Description |
|---|---|---|
volume |
Volume (u8) |
Master volume (0-100) |
mute |
Mute (bool) |
Mute state |
bass |
Bass (i8) |
Bass EQ (-10 to +10) |
treble |
Treble (i8) |
Treble EQ (-10 to +10) |
loudness |
Loudness (bool) |
Loudness compensation |
Playback (AVTransport)
| Property | Type | Description |
|---|---|---|
playback_state |
PlaybackState |
Playing/Paused/Stopped/Transitioning |
position |
Position |
Current position and duration |
current_track |
CurrentTrack |
Track metadata (title, artist, album) |
Grouping (ZoneGroupTopology)
| Property | Type | Description |
|---|---|---|
group_membership |
GroupMembership |
Group ID and coordinator status |
Speaker Lookup
// Get speaker by friendly name
let speaker = system.get_speaker_by_name?;
// Get speaker by unique ID
let speaker = system.get_speaker_by_id?;
// Get all speakers
for speaker in system.speakers
// Get all speaker names
let names = system.speaker_names;
Error Handling
The SDK provides structured error types:
use SdkError;
match speaker.volume.fetch
Architecture
sonos-sdk (Sync-First DOM-like API)
↓
sonos-state (State Management) ←→ sonos-event-manager (Event Subscriptions)
↓ ↓
sonos-api (UPnP Operations) sonos-stream (Event Processing)
License
MIT License
See Also
sonos-api- Low-level UPnP operationssonos-discovery- Device discoverysonos-stream- Event streaming