sonos-cli 0.7.0

CLI and TUI for controlling Sonos speakers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::io::IsTerminal;

use sonos_sdk::{SeekTarget, SonosSystem};

use super::{
    format_duration_human, format_time_ms, parse_duration, playback_icon, playback_label,
    require_speaker_only, resolve_group, resolve_speaker, validate_seek_time, Commands,
    GlobalFlags, OnOff, QueueAction,
};
use crate::config::Config;
use crate::diagnostics;
use crate::errors::CliError;
use crate::liveness::{CommandOutput, Probes};

/// Run a command and report both what it printed and whether a speaker
/// actually answered while producing it.
///
/// Most arms return [`CommandOutput::live`]: they end in a SOAP call whose
/// error is propagated with `?`, so reaching `Ok` is itself proof of contact.
/// The read-only listings (`speakers`, `groups`, `status`) are the exceptions —
/// they tolerate per-field failure by design, which is exactly how a total
/// discovery failure used to render as success.
pub fn run_command(
    cmd: Commands,
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
) -> Result<CommandOutput, CliError> {
    let spk = || resolve_speaker(system, config, global);

    match cmd {
        Commands::Speakers => cmd_speakers(system),
        Commands::Groups => cmd_groups(system),
        Commands::Status => cmd_status(system, config, global),
        Commands::Join => cmd_join(system, config, global),
        Commands::Leave => cmd_leave(system, config, global),
        Commands::Bass { level } => cmd_bass(system, config, global, level),
        Commands::Treble { level } => cmd_treble(system, config, global, level),
        Commands::Loudness { state } => cmd_loudness(system, config, global, state),
        Commands::Sleep { duration } => cmd_sleep(system, config, global, &duration),
        Commands::Queue { action } => cmd_queue(system, config, global, action),
        Commands::Config { .. } => unreachable!("config commands dispatched before discovery"),

        Commands::Play => {
            let s = spk()?;
            s.play()?;
            Ok(CommandOutput::live(format!("Playing ({})", s.name)))
        }
        Commands::Pause => {
            let s = spk()?;
            s.pause()?;
            Ok(CommandOutput::live(format!("Paused ({})", s.name)))
        }
        Commands::Stop => {
            let s = spk()?;
            s.stop()?;
            Ok(CommandOutput::live(format!("Stopped ({})", s.name)))
        }
        Commands::Next => {
            let s = spk()?;
            s.next()?;
            Ok(CommandOutput::live(format!("Next track ({})", s.name)))
        }
        Commands::Previous => {
            let s = spk()?;
            s.previous()?;
            Ok(CommandOutput::live(format!("Previous track ({})", s.name)))
        }
        Commands::Seek { position } => {
            validate_seek_time(&position)?;
            let s = spk()?;
            s.seek(SeekTarget::Time(position.clone()))?;
            Ok(CommandOutput::live(format!(
                "Seeked to {} ({})",
                position, s.name
            )))
        }
        Commands::Mode { mode } => {
            let s = spk()?;
            s.set_play_mode(mode.to_sdk())?;
            Ok(CommandOutput::live(format!(
                "Mode set to {:?} ({})",
                mode, s.name
            )))
        }
        Commands::Volume { level } => cmd_volume(system, config, global, level),
        Commands::Mute => cmd_mute(system, config, global, true),
        Commands::Unmute => cmd_mute(system, config, global, false),
    }
}

// -- Command handlers ---------------------------------------------------------

fn cmd_speakers(system: &SonosSystem) -> Result<CommandOutput, CliError> {
    let speakers = system.speakers();
    if speakers.is_empty() {
        eprintln!("{}", diagnostics::discovery_hint());
        // No speaker was claimed, so there is nothing unvalidated to flag; the
        // hint above already says what went wrong. See `liveness::verdict`.
        return Ok(CommandOutput::live("No speakers found"));
    }
    // Two probes per speaker. The visible symptom of a dead network is these
    // two failing and each row collapsing to a bare cached name — so counting
    // them is exactly the evidence that the row is unverified.
    let mut probes = Probes::default();
    let lines: Vec<String> = speakers
        .iter()
        .map(|s| {
            let state = probes.record(s.playback_state.fetch());
            let vol = probes.record(s.volume.fetch());
            // `group()` reads the in-memory topology, not the network — not a probe.
            let group_name = s
                .group()
                .and_then(|g| g.coordinator().map(|c| c.name))
                .unwrap_or_default();

            let state_str = state
                .as_ref()
                .map(|st| format!("{} {}", playback_icon(st), playback_label(st)))
                .unwrap_or_default();
            let vol_str = vol.map(|v| format!("vol:{}", v.0)).unwrap_or_default();

            let mut parts = vec![s.name.clone()];
            if !state_str.is_empty() {
                parts.push(state_str);
            }
            if !vol_str.is_empty() {
                parts.push(vol_str);
            }
            if !group_name.is_empty() {
                parts.push(format!("({group_name})"));
            }
            parts.join("   ")
        })
        .collect();
    Ok(CommandOutput::new(lines.join("\n"), probes.verdict()))
}

fn cmd_groups(system: &SonosSystem) -> Result<CommandOutput, CliError> {
    let groups = system.groups();
    if groups.is_empty() {
        eprintln!("{}", diagnostics::discovery_hint());
        return Ok(CommandOutput::live("No groups found"));
    }
    let mut probes = Probes::default();
    let lines: Vec<String> = groups
        .iter()
        .map(|g| {
            let coord = g.coordinator();
            let coord_name = coord
                .as_ref()
                .map(|c| c.name.clone())
                .unwrap_or_else(|| "unknown".to_string());

            // A group with no coordinator attempts nothing on the speaker, so
            // it must not be counted as two failures.
            let state = coord
                .as_ref()
                .and_then(|c| probes.record(c.playback_state.fetch()));
            let track = coord
                .as_ref()
                .and_then(|c| probes.record(c.current_track.fetch()));
            let vol = probes.record(g.volume.fetch());

            let state_str = state
                .as_ref()
                .map(|st| format!("{} {}", playback_icon(st), playback_label(st)))
                .unwrap_or_default();
            let track_str = track
                .as_ref()
                .map(|t| {
                    let d = t.display();
                    if d == "Unknown" {
                        t.uri
                            .as_deref()
                            .filter(|u| !u.is_empty() && !u.starts_with("x-rincon:"))
                            .map(|_| "Playing (no metadata)".to_string())
                            .unwrap_or(d)
                    } else {
                        d
                    }
                })
                .unwrap_or_default();
            let vol_str = vol.map(|v| format!("vol:{}", v.0)).unwrap_or_default();

            let mut parts = vec![coord_name];
            if !state_str.is_empty() {
                parts.push(state_str);
            }
            if !track_str.is_empty() {
                parts.push(track_str);
            }
            if !vol_str.is_empty() {
                parts.push(vol_str);
            }
            parts.join("   ")
        })
        .collect();
    Ok(CommandOutput::new(lines.join("\n"), probes.verdict()))
}

fn cmd_volume(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    level: u8,
) -> Result<CommandOutput, CliError> {
    // Explicit --speaker (without --group) → Speaker.set_volume(u8)
    if global.speaker.is_some() && global.group.is_none() {
        let s = resolve_speaker(system, config, global)?;
        s.set_volume(level)?;
        return Ok(CommandOutput::live(format!(
            "Volume set to {} ({})",
            level, s.name
        )));
    }
    // Otherwise → Group.set_volume(u16) via GroupRenderingControl
    let g = resolve_group(system, config, global)?;
    let name = g
        .coordinator()
        .map(|c| c.name)
        .unwrap_or_else(|| "unknown".to_string());
    g.set_volume(level as u16)?;
    Ok(CommandOutput::live(format!(
        "Volume set to {level} ({name})"
    )))
}

fn cmd_mute(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    muted: bool,
) -> Result<CommandOutput, CliError> {
    let label = if muted { "Muted" } else { "Unmuted" };
    // Explicit --speaker (without --group) → Speaker.set_mute(bool)
    if global.speaker.is_some() && global.group.is_none() {
        let s = resolve_speaker(system, config, global)?;
        s.set_mute(muted)?;
        return Ok(CommandOutput::live(format!("{} ({})", label, s.name)));
    }
    // Otherwise → Group.set_mute(bool) via GroupRenderingControl
    let g = resolve_group(system, config, global)?;
    let name = g
        .coordinator()
        .map(|c| c.name)
        .unwrap_or_else(|| "unknown".to_string());
    g.set_mute(muted)?;
    Ok(CommandOutput::live(format!("{label} ({name})")))
}

fn cmd_status(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
) -> Result<CommandOutput, CliError> {
    let spk = resolve_speaker(system, config, global)?;
    // Same bug as `speakers`, one row wide: with every fetch failing this
    // printed "<name> unknown" and exited 0.
    //
    // A non-coordinator speaker answers NOT_IMPLEMENTED for track and
    // position — a real reply from a real speaker that arrives here as an
    // `Err`. That is fine: `volume` still succeeds, and one success is enough
    // for `Live`, so a partially-answering speaker is never reported as dead.
    let mut probes = Probes::default();
    let state = probes.record(spk.playback_state.fetch());
    let track = probes.record(spk.current_track.fetch());
    let pos = probes.record(spk.position.fetch());
    let vol = probes.record(spk.volume.fetch());

    let state_str = state
        .as_ref()
        .map(|st| format!("{} {}", playback_icon(st), playback_label(st)))
        .unwrap_or_else(|| "unknown".to_string());
    let track_str = track
        .as_ref()
        .map(|t| {
            let d = t.display();
            if d == "Unknown" {
                t.uri
                    .as_deref()
                    .filter(|u| !u.is_empty() && !u.starts_with("x-rincon:"))
                    .map(|_| "Playing (no metadata)".to_string())
                    .unwrap_or(d)
            } else {
                d
            }
        })
        .unwrap_or_default();
    let pos_str = pos
        .as_ref()
        .map(|p| {
            format!(
                "{}/{}",
                format_time_ms(p.position_ms),
                format_time_ms(p.duration_ms)
            )
        })
        .unwrap_or_default();
    let vol_str = vol.map(|v| format!("vol:{}", v.0)).unwrap_or_default();

    let mut parts = vec![spk.name.clone(), state_str];
    if !track_str.is_empty() {
        parts.push(track_str);
    }
    if !pos_str.is_empty() {
        parts.push(pos_str);
    }
    if !vol_str.is_empty() {
        parts.push(vol_str);
    }
    Ok(CommandOutput::new(parts.join("  "), probes.verdict()))
}

fn cmd_join(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
) -> Result<CommandOutput, CliError> {
    let raw_speaker = global
        .speaker
        .as_deref()
        .ok_or_else(|| CliError::Validation("--speaker is required for join".into()))?;
    let raw_group = global
        .group
        .as_deref()
        .ok_or_else(|| CliError::Validation("--group is required for join".into()))?;
    let speaker_name = config.resolve_alias(raw_speaker);
    let group_name = config.resolve_alias(raw_group);
    let spk = system
        .speaker(speaker_name)
        .ok_or_else(|| CliError::SpeakerNotFound(speaker_name.into()))?;
    let grp = system
        .group(group_name)
        .ok_or_else(|| CliError::GroupNotFound(group_name.into()))?;
    grp.add_speaker(&spk)?;
    Ok(CommandOutput::live(format!(
        "{speaker_name} joined {group_name}"
    )))
}

fn cmd_leave(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
) -> Result<CommandOutput, CliError> {
    let raw_speaker = global
        .speaker
        .as_deref()
        .ok_or_else(|| CliError::Validation("--speaker is required for leave".into()))?;
    let speaker_name = config.resolve_alias(raw_speaker);
    let spk = system
        .speaker(speaker_name)
        .ok_or_else(|| CliError::SpeakerNotFound(speaker_name.into()))?;
    let group_name = spk
        .group()
        .and_then(|g| g.coordinator().map(|c| c.name))
        .unwrap_or_else(|| "its group".into());
    spk.leave_group()?;
    Ok(CommandOutput::live(format!(
        "{speaker_name} left {group_name}"
    )))
}

fn cmd_bass(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    level: i8,
) -> Result<CommandOutput, CliError> {
    let spk = require_speaker_only(system, config, global, "bass")?;
    spk.set_bass(level)?;
    Ok(CommandOutput::live(format!(
        "Bass set to {} ({})",
        level, spk.name
    )))
}

fn cmd_treble(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    level: i8,
) -> Result<CommandOutput, CliError> {
    let spk = require_speaker_only(system, config, global, "treble")?;
    spk.set_treble(level)?;
    Ok(CommandOutput::live(format!(
        "Treble set to {} ({})",
        level, spk.name
    )))
}

fn cmd_loudness(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    state: OnOff,
) -> Result<CommandOutput, CliError> {
    let spk = require_speaker_only(system, config, global, "loudness")?;
    let enabled = matches!(state, OnOff::On);
    spk.set_loudness(enabled)?;
    if enabled {
        Ok(CommandOutput::live(format!(
            "Loudness enabled ({})",
            spk.name
        )))
    } else {
        Ok(CommandOutput::live(format!(
            "Loudness disabled ({})",
            spk.name
        )))
    }
}

fn cmd_sleep(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    duration: &str,
) -> Result<CommandOutput, CliError> {
    let spk = resolve_speaker(system, config, global)?;
    if duration == "cancel" {
        spk.cancel_sleep_timer()?;
        Ok(CommandOutput::live(format!(
            "Sleep timer cancelled ({})",
            spk.name
        )))
    } else {
        let hh_mm_ss = parse_duration(duration)?;
        let human = format_duration_human(duration);
        spk.configure_sleep_timer(&hh_mm_ss)?;
        Ok(CommandOutput::live(format!(
            "Sleep timer set for {} ({})",
            human, spk.name
        )))
    }
}

fn cmd_queue(
    system: &SonosSystem,
    config: &Config,
    global: &GlobalFlags,
    action: Option<QueueAction>,
) -> Result<CommandOutput, CliError> {
    let spk = resolve_speaker(system, config, global)?;
    match action {
        None => {
            let info = spk.get_media_info()?;
            if info.nr_tracks == 0 {
                return Ok(CommandOutput::live(format!(
                    "queue is empty ({})",
                    spk.name
                )));
            }
            Ok(CommandOutput::live(format!(
                "{} — {} tracks",
                spk.name, info.nr_tracks
            )))
        }
        Some(QueueAction::Add { uri }) => {
            spk.add_uri_to_queue(&uri, "", 0, false)?;
            Ok(CommandOutput::live(format!(
                "Added to queue ({})",
                spk.name
            )))
        }
        Some(QueueAction::Clear) => {
            if std::io::stdin().is_terminal() && !global.no_input {
                eprint!("Clear queue for {}? [y/N] ", spk.name);
                let mut input = String::new();
                std::io::stdin()
                    .read_line(&mut input)
                    .map_err(|e| CliError::Validation(e.to_string()))?;
                if !input.trim().eq_ignore_ascii_case("y") {
                    return Ok(CommandOutput::live("Cancelled"));
                }
            }
            spk.remove_all_tracks_from_queue()?;
            Ok(CommandOutput::live(format!("Queue cleared ({})", spk.name)))
        }
    }
}