sonos-api 0.3.0

Type-safe Sonos API for UPnP device control via SOAP
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
//! RenderingControl service event types and parsing
//!
//! Provides direct serde-based XML parsing with no business logic,
//! replicating exactly what Sonos produces for sonos-stream consumption.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::IpAddr;

use crate::events::{xml_utils, EnrichedEvent, EventParser, EventSource};
use crate::{ApiError, Result, Service};

/// Minimal RenderingControl event - direct serde mapping from UPnP event XML
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "propertyset")]
pub struct RenderingControlEvent {
    #[serde(rename = "property")]
    property: RenderingControlProperty,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct RenderingControlProperty {
    #[serde(
        rename = "LastChange",
        deserialize_with = "xml_utils::deserialize_nested"
    )]
    last_change: RenderingControlEventData,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "Event")]
pub struct RenderingControlEventData {
    #[serde(rename = "InstanceID")]
    instance: RenderingControlInstance,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct RenderingControlInstance {
    #[serde(rename = "Volume", default)]
    pub volumes: Vec<ChannelValueAttribute>,

    #[serde(rename = "Mute", default)]
    pub mutes: Vec<ChannelValueAttribute>,

    #[serde(rename = "Bass", default)]
    pub bass: Option<xml_utils::ValueAttribute>,

    #[serde(rename = "Treble", default)]
    pub treble: Option<xml_utils::ValueAttribute>,

    #[serde(rename = "Loudness", default)]
    pub loudness: Option<xml_utils::ValueAttribute>,

    #[serde(rename = "Balance", default)]
    pub balance: Option<xml_utils::ValueAttribute>,
}

/// Represents an XML element with both val and channel attributes
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ChannelValueAttribute {
    #[serde(rename = "@val", default)]
    pub val: String,

    #[serde(rename = "@channel", default)]
    pub channel: String,
}

impl RenderingControlEvent {
    /// Get master volume
    pub fn master_volume(&self) -> Option<String> {
        self.get_volume_for_channel("Master")
    }

    /// Get left front volume
    pub fn lf_volume(&self) -> Option<String> {
        self.get_volume_for_channel("LF")
    }

    /// Get right front volume
    pub fn rf_volume(&self) -> Option<String> {
        self.get_volume_for_channel("RF")
    }

    /// Get master mute
    pub fn master_mute(&self) -> Option<String> {
        self.get_mute_for_channel("Master")
    }

    /// Get left front mute
    pub fn lf_mute(&self) -> Option<String> {
        self.get_mute_for_channel("LF")
    }

    /// Get right front mute
    pub fn rf_mute(&self) -> Option<String> {
        self.get_mute_for_channel("RF")
    }

    /// Get bass
    pub fn bass(&self) -> Option<String> {
        self.property
            .last_change
            .instance
            .bass
            .as_ref()
            .map(|v| v.val.clone())
    }

    /// Get treble
    pub fn treble(&self) -> Option<String> {
        self.property
            .last_change
            .instance
            .treble
            .as_ref()
            .map(|v| v.val.clone())
    }

    /// Get loudness
    pub fn loudness(&self) -> Option<String> {
        self.property
            .last_change
            .instance
            .loudness
            .as_ref()
            .map(|v| v.val.clone())
    }

    /// Get balance
    pub fn balance(&self) -> Option<String> {
        self.property
            .last_change
            .instance
            .balance
            .as_ref()
            .map(|v| v.val.clone())
    }

    /// Get other channels as a map of all non-standard channels
    pub fn other_channels(&self) -> HashMap<String, String> {
        let mut channels = HashMap::new();

        // Add all volume channels that aren't Master, LF, or RF
        for volume in &self.property.last_change.instance.volumes {
            if !["Master", "LF", "RF"].contains(&volume.channel.as_str()) {
                channels.insert(format!("{}Volume", volume.channel), volume.val.clone());
            }
        }

        // Add all mute channels that aren't Master, LF, or RF
        for mute in &self.property.last_change.instance.mutes {
            if !["Master", "LF", "RF"].contains(&mute.channel.as_str()) {
                channels.insert(format!("{}Mute", mute.channel), mute.val.clone());
            }
        }

        channels
    }

    /// Helper method to get volume for a specific channel
    fn get_volume_for_channel(&self, channel: &str) -> Option<String> {
        self.property
            .last_change
            .instance
            .volumes
            .iter()
            .find(|v| v.channel == channel)
            .map(|v| v.val.clone())
    }

    /// Helper method to get mute for a specific channel
    fn get_mute_for_channel(&self, channel: &str) -> Option<String> {
        self.property
            .last_change
            .instance
            .mutes
            .iter()
            .find(|m| m.channel == channel)
            .map(|m| m.val.clone())
    }

    /// Convert parsed UPnP event to canonical state representation.
    pub fn into_state(&self) -> super::state::RenderingControlState {
        super::state::RenderingControlState {
            master_volume: self.master_volume(),
            master_mute: self.master_mute(),
            lf_volume: self.lf_volume(),
            rf_volume: self.rf_volume(),
            lf_mute: self.lf_mute(),
            rf_mute: self.rf_mute(),
            bass: self.bass(),
            treble: self.treble(),
            loudness: self.loudness(),
            balance: self.balance(),
            other_channels: self.other_channels(),
        }
    }

    /// Parse from UPnP event XML using serde
    pub fn from_xml(xml: &str) -> Result<Self> {
        let clean_xml = xml_utils::strip_namespaces(xml);
        quick_xml::de::from_str(&clean_xml)
            .map_err(|e| ApiError::ParseError(format!("Failed to parse RenderingControl XML: {e}")))
    }
}

/// Minimal parser implementation
pub struct RenderingControlEventParser;

impl EventParser for RenderingControlEventParser {
    type EventData = RenderingControlEvent;

    fn parse_upnp_event(&self, xml: &str) -> Result<Self::EventData> {
        RenderingControlEvent::from_xml(xml)
    }

    fn service_type(&self) -> Service {
        Service::RenderingControl
    }
}

/// Create enriched event for sonos-stream integration
pub fn create_enriched_event(
    speaker_ip: IpAddr,
    event_source: EventSource,
    event_data: RenderingControlEvent,
) -> EnrichedEvent<RenderingControlEvent> {
    EnrichedEvent::new(
        speaker_ip,
        Service::RenderingControl,
        event_source,
        event_data,
    )
}

/// Create enriched event with registration ID
pub fn create_enriched_event_with_registration_id(
    registration_id: u64,
    speaker_ip: IpAddr,
    event_source: EventSource,
    event_data: RenderingControlEvent,
) -> EnrichedEvent<RenderingControlEvent> {
    EnrichedEvent::with_registration_id(
        registration_id,
        speaker_ip,
        Service::RenderingControl,
        event_source,
        event_data,
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rendering_control_parser_service_type() {
        let parser = RenderingControlEventParser;
        assert_eq!(parser.service_type(), Service::RenderingControl);
    }

    #[test]
    fn test_rendering_control_event_creation() {
        let event = RenderingControlEvent {
            property: RenderingControlProperty {
                last_change: RenderingControlEventData {
                    instance: RenderingControlInstance {
                        volumes: vec![ChannelValueAttribute {
                            val: "75".to_string(),
                            channel: "Master".to_string(),
                        }],
                        mutes: vec![ChannelValueAttribute {
                            val: "false".to_string(),
                            channel: "Master".to_string(),
                        }],
                        bass: Some(xml_utils::ValueAttribute {
                            val: "0".to_string(),
                        }),
                        treble: Some(xml_utils::ValueAttribute {
                            val: "0".to_string(),
                        }),
                        loudness: Some(xml_utils::ValueAttribute {
                            val: "true".to_string(),
                        }),
                        balance: Some(xml_utils::ValueAttribute {
                            val: "0".to_string(),
                        }),
                    },
                },
            },
        };

        assert_eq!(event.master_volume(), Some("75".to_string()));
        assert_eq!(event.master_mute(), Some("false".to_string()));
        assert!(event.other_channels().is_empty());
    }

    #[test]
    fn test_basic_xml_parsing() {
        let xml = r#"<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
            <e:property>
                <LastChange>&lt;Event xmlns="urn:schemas-upnp-org:metadata-1-0/RCS/"&gt;
                    &lt;InstanceID val="0"&gt;
                        &lt;Volume channel="Master" val="75"/&gt;
                        &lt;Mute channel="Master" val="0"/&gt;
                        &lt;Bass val="2"/&gt;
                        &lt;Treble val="-1"/&gt;
                    &lt;/InstanceID&gt;
                &lt;/Event&gt;</LastChange>
            </e:property>
        </e:propertyset>"#;

        let event = RenderingControlEvent::from_xml(xml).unwrap();
        assert_eq!(event.master_volume(), Some("75".to_string()));
        assert_eq!(event.master_mute(), Some("0".to_string()));
        assert_eq!(event.bass(), Some("2".to_string()));
        assert_eq!(event.treble(), Some("-1".to_string()));
    }

    #[test]
    fn test_channel_specific_volume() {
        let xml = r#"<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
            <e:property>
                <LastChange>&lt;Event xmlns="urn:schemas-upnp-org:metadata-1-0/RCS/"&gt;
                    &lt;InstanceID val="0"&gt;
                        &lt;Volume channel="Master" val="50"/&gt;
                        &lt;Volume channel="LF" val="80"/&gt;
                        &lt;Volume channel="RF" val="85"/&gt;
                        &lt;Mute channel="LF" val="1"/&gt;
                    &lt;/InstanceID&gt;
                &lt;/Event&gt;</LastChange>
            </e:property>
        </e:propertyset>"#;

        let event = RenderingControlEvent::from_xml(xml).unwrap();
        assert_eq!(event.master_volume(), Some("50".to_string()));
        assert_eq!(event.lf_volume(), Some("80".to_string()));
        assert_eq!(event.rf_volume(), Some("85".to_string()));
        assert_eq!(event.lf_mute(), Some("1".to_string()));
    }

    #[test]
    fn test_enriched_event_creation() {
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let source = EventSource::UPnPNotification {
            subscription_id: "uuid:123".to_string(),
        };
        let event_data = RenderingControlEvent {
            property: RenderingControlProperty {
                last_change: RenderingControlEventData {
                    instance: RenderingControlInstance {
                        volumes: vec![ChannelValueAttribute {
                            val: "50".to_string(),
                            channel: "Master".to_string(),
                        }],
                        mutes: vec![ChannelValueAttribute {
                            val: "0".to_string(),
                            channel: "Master".to_string(),
                        }],
                        bass: None,
                        treble: None,
                        loudness: None,
                        balance: None,
                    },
                },
            },
        };

        let enriched = create_enriched_event(ip, source, event_data);

        assert_eq!(enriched.speaker_ip, ip);
        assert_eq!(enriched.service, Service::RenderingControl);
        assert!(enriched.registration_id.is_none());
    }

    #[test]
    fn test_enriched_event_with_registration_id() {
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let source = EventSource::UPnPNotification {
            subscription_id: "uuid:123".to_string(),
        };
        let event_data = RenderingControlEvent {
            property: RenderingControlProperty {
                last_change: RenderingControlEventData {
                    instance: RenderingControlInstance {
                        volumes: vec![ChannelValueAttribute {
                            val: "50".to_string(),
                            channel: "Master".to_string(),
                        }],
                        mutes: vec![ChannelValueAttribute {
                            val: "0".to_string(),
                            channel: "Master".to_string(),
                        }],
                        bass: None,
                        treble: None,
                        loudness: None,
                        balance: None,
                    },
                },
            },
        };

        let enriched = create_enriched_event_with_registration_id(42, ip, source, event_data);

        assert_eq!(enriched.registration_id, Some(42));
    }

    #[test]
    fn test_into_state_maps_all_fields() {
        let event = RenderingControlEvent {
            property: RenderingControlProperty {
                last_change: RenderingControlEventData {
                    instance: RenderingControlInstance {
                        volumes: vec![
                            ChannelValueAttribute {
                                val: "50".to_string(),
                                channel: "Master".to_string(),
                            },
                            ChannelValueAttribute {
                                val: "45".to_string(),
                                channel: "LF".to_string(),
                            },
                            ChannelValueAttribute {
                                val: "55".to_string(),
                                channel: "RF".to_string(),
                            },
                        ],
                        mutes: vec![ChannelValueAttribute {
                            val: "0".to_string(),
                            channel: "Master".to_string(),
                        }],
                        bass: Some(xml_utils::ValueAttribute {
                            val: "5".to_string(),
                        }),
                        treble: Some(xml_utils::ValueAttribute {
                            val: "-3".to_string(),
                        }),
                        loudness: Some(xml_utils::ValueAttribute {
                            val: "1".to_string(),
                        }),
                        balance: None,
                    },
                },
            },
        };

        let state = event.into_state();

        assert_eq!(state.master_volume, Some("50".to_string()));
        assert_eq!(state.master_mute, Some("0".to_string()));
        assert_eq!(state.lf_volume, Some("45".to_string()));
        assert_eq!(state.rf_volume, Some("55".to_string()));
        assert_eq!(state.bass, Some("5".to_string()));
        assert_eq!(state.treble, Some("-3".to_string()));
        assert_eq!(state.loudness, Some("1".to_string()));
    }
}