wez-sonos 0.1.0

Sonos API client
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
use instant_xml::{FromXmlOwned, ToXml};
use reqwest::{StatusCode, Url};
use std::net::Ipv4Addr;
use thiserror::Error;

mod didl;
mod discovery;
mod generated;
mod upnp;
mod xmlutil;
mod zone;

pub use didl::*;
pub use discovery::*;
pub use generated::*;
pub use upnp::*;
pub use xmlutil::DecodeXmlString;
pub use zone::*;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
pub enum Error {
    #[error("XML Error: {0}")]
    Xml(#[from] instant_xml::Error),
    #[error("XML Error: {error:#} while parsing {text}")]
    XmlParse {
        error: instant_xml::Error,
        text: String,
    },
    #[error("Service {0:?} is not supported by this device")]
    UnsupportedService(String),
    #[error("Invalid URI: {0:#?}")]
    InvalidUri(#[from] url::ParseError),
    #[error("Reqwest Error: {0:#?}")]
    Reqwest(#[from] reqwest::Error),
    #[error("Failed Request: {status:?} {body}")]
    FailedRequest {
        status: StatusCode,
        body: String,
        headers: reqwest::header::HeaderMap,
    },
    #[error("Device has no name!?")]
    NoName,
    #[error("I/O Error: {0:#}")]
    Io(#[from] std::io::Error),
    #[error("Invalid enum variant value")]
    InvalidEnumVariantValue,
    #[error("Room {0} not found")]
    RoomNotFound(String),
    #[error("Cannot find IP from device URL! {0:?}")]
    NoIpInDeviceUrl(Url),
    #[error("Subscription failed because SID header is missing")]
    SubscriptionFailedNoSid,
    #[error("TrackMetaData list is empty!?")]
    EmptyTrackMetaData,
    #[error("TrackMetaData has multiple items but expect a single item")]
    MoreThanOneTrackMetaData,
    #[error("LastChange format unexpected {0}")]
    LastChangeFormatUnexpected(String),
}

impl Error {
    pub async fn with_failed_http_response(response: reqwest::Response) -> Error {
        let status = response.status();
        let headers = response.headers().clone();
        let body = match response.bytes().await {
            Ok(bytes) => String::from_utf8_lossy(&bytes).to_string(),
            Err(err) => format!("Failed to retrieve body from failed request: {err:#}"),
        };

        return Error::FailedRequest {
            status,
            body,
            headers,
        };
    }

    pub async fn check_response(response: reqwest::Response) -> Result<reqwest::Response> {
        let status = response.status();
        if !status.is_success() {
            Err(Self::with_failed_http_response(response).await)
        } else {
            Ok(response)
        }
    }
}

#[derive(Debug, Clone)]
pub struct SonosDevice {
    url: Url,
    device: DeviceSpec,
}

impl SonosDevice {
    /// Constructs a SonosDevice from the supplied IP Address.
    /// Validates that the device is actually a Sonos device
    /// before returning successfully.
    pub async fn from_ip(addr: Ipv4Addr) -> Result<Self> {
        Self::from_url(format!("http://{addr}:1400/xml/device_description.xml").parse()?).await
    }

    /// Resolves the SonosDevice whose name is equal to the provided
    /// name.  If no matching device is found within a reasonably
    /// short, unspecified, implementation-defined timeout, then
    /// an `Error::RoomNotFound` is produced.
    pub async fn for_room(room_name: &str) -> Result<Self> {
        let mut rx = discover(std::time::Duration::from_secs(15)).await?;
        while let Some(device) = rx.recv().await {
            if let Ok(name) = device.name().await {
                if name == room_name {
                    return Ok(device);
                }
            }
        }

        Err(Error::RoomNotFound(room_name.to_string()))
    }

    /// Constructs a SonosDevice from the supplied URL, which must
    /// be the device_description.xml URL for that device.
    /// Validates that the device is actually a Sonos device
    /// before returning successfully.
    pub async fn from_url(url: Url) -> Result<Self> {
        let response = reqwest::get(url.clone()).await?;

        let response = Error::check_response(response).await?;
        let body = response.text().await?;
        let device = DeviceSpec::parse_xml(&body)?;

        Ok(Self { url, device })
    }

    /// Returns the room/zone name of the device
    pub async fn name(&self) -> Result<String> {
        let attr = self.get_zone_attributes().await?;
        attr.current_zone_name.ok_or(Error::NoName)
    }

    /// Returns information about the zone to which this device belongs
    pub async fn get_zone_group_state(&self) -> Result<Vec<ZoneGroup>> {
        let state = <Self as ZoneGroupTopology>::get_zone_group_state(self).await?;
        Ok(match state.zone_group_state {
            Some(state) => state
                .into_inner()
                .map(|s| s.groups)
                .unwrap_or_else(Vec::new),
            None => vec![],
        })
    }

    /// Stops playback
    pub async fn stop(&self) -> Result<()> {
        <Self as AVTransport>::stop(self, Default::default()).await
    }

    /// Begin playback
    pub async fn play(&self) -> Result<()> {
        <Self as AVTransport>::play(
            self,
            av_transport::PlayRequest {
                instance_id: 0,
                speed: "1".to_string(),
            },
        )
        .await
    }

    /// pause playback
    pub async fn pause(&self) -> Result<()> {
        <Self as AVTransport>::pause(self, av_transport::PauseRequest { instance_id: 0 }).await
    }

    /// Clears the queue
    pub async fn queue_clear(&self) -> Result<()> {
        <Self as AVTransport>::remove_all_tracks_from_queue(self, Default::default()).await
    }

    pub async fn set_play_mode(&self, new_play_mode: CurrentPlayMode) -> Result<()> {
        <Self as AVTransport>::set_play_mode(
            self,
            av_transport::SetPlayModeRequest {
                instance_id: 0,
                new_play_mode: new_play_mode,
            },
        )
        .await
    }

    pub async fn set_av_transport_uri(
        &self,
        uri: &str,
        metadata: Option<TrackMetaData>,
    ) -> Result<()> {
        <Self as AVTransport>::set_av_transport_uri(
            self,
            av_transport::SetAvTransportUriRequest {
                instance_id: 0,
                current_uri: uri.to_string(),
                current_uri_meta_data: metadata.into(),
            },
        )
        .await
    }

    pub async fn queue_prepend(
        &self,
        uri: &str,
        metadata: Option<TrackMetaData>,
    ) -> Result<av_transport::AddUriToQueueResponse> {
        <Self as AVTransport>::add_uri_to_queue(
            self,
            av_transport::AddUriToQueueRequest {
                instance_id: 0,
                enqueued_uri: uri.to_string(),
                enqueued_uri_meta_data: metadata.into(),
                desired_first_track_number_enqueued: 0,
                enqueue_as_next: true,
            },
        )
        .await
    }

    pub async fn queue_append(
        &self,
        uri: &str,
        metadata: Option<TrackMetaData>,
    ) -> Result<av_transport::AddUriToQueueResponse> {
        <Self as AVTransport>::add_uri_to_queue(
            self,
            av_transport::AddUriToQueueRequest {
                instance_id: 0,
                enqueued_uri: uri.to_string(),
                enqueued_uri_meta_data: metadata.into(),
                desired_first_track_number_enqueued: 0,
                enqueue_as_next: false,
            },
        )
        .await
    }

    pub async fn queue_browse(
        &self,
        starting_index: u32,
        requested_count: u32,
    ) -> Result<Vec<TrackMetaData>> {
        let result = <Self as Queue>::browse(
            self,
            queue::BrowseRequest {
                queue_id: 0,
                starting_index: starting_index,
                requested_count: requested_count,
            },
        )
        .await?;

        match result.result {
            Some(list) => Ok(list.into_inner().map(|i| i.tracks).unwrap_or_else(Vec::new)),
            None => Ok(vec![]),
        }
    }

    pub fn url(&self) -> &Url {
        &self.url
    }
}

const SOAP_ENCODING: &str = "http://schemas.xmlsoap.org/soap/encoding/";
const SOAP_ENVELOPE: &str = "http://schemas.xmlsoap.org/soap/envelope/";

mod soap {
    use super::SOAP_ENVELOPE;
    use instant_xml::ToXml;

    #[derive(Debug, Eq, PartialEq, ToXml)]
    pub struct Unit;

    #[derive(Debug, Eq, PartialEq, ToXml)]
    #[xml(rename="s:Envelope", ns("", s = SOAP_ENVELOPE))]
    pub struct Envelope<T: ToXml> {
        #[xml(attribute, rename = "s:encodingStyle")]
        pub encoding_style: &'static str,
        pub body: Body<T>,
    }

    #[derive(Debug, Eq, PartialEq, ToXml)]
    #[xml(rename = "s:Body")]
    pub struct Body<T: ToXml> {
        pub payload: T,
    }
}

mod soap_resp {
    use super::SOAP_ENVELOPE;
    use instant_xml::FromXml;

    #[derive(Debug, Eq, PartialEq, FromXml)]
    #[xml(ns(SOAP_ENVELOPE))]
    pub struct Envelope<T> {
        #[xml(rename = "encodingStyle", attribute, ns(SOAP_ENVELOPE))]
        pub encoding_style: String,
        pub body: Body<T>,
    }

    #[derive(Debug, Eq, PartialEq, FromXml)]
    #[xml(ns(SOAP_ENVELOPE))]
    pub struct Body<T> {
        pub payload: T,
    }
}

/// Special case for decoding (), as instant_xml considers the empty
/// body in the `soap_resp::Body<T>` case to be an error
mod soap_empty_resp {
    use super::SOAP_ENVELOPE;
    use instant_xml::FromXml;

    #[derive(Debug, Eq, PartialEq, FromXml)]
    #[xml(ns(SOAP_ENVELOPE))]
    pub struct Envelope {
        #[xml(rename = "encodingStyle", attribute, ns(SOAP_ENVELOPE))]
        pub encoding_style: String,
        pub body: Body,
    }

    #[derive(Debug, Eq, PartialEq, FromXml)]
    #[xml(ns(SOAP_ENVELOPE))]
    pub struct Body {}
}

/// This trait decodes a SOAP response envelope into Self
pub trait DecodeSoapResponse {
    /// xml is a complete Soap `<Envelope>` element.
    /// This method decodes and returns Self from that Envelope.
    fn decode_soap_xml(xml: &str) -> Result<Self>
    where
        Self: Sized;
}

impl DecodeSoapResponse for () {
    fn decode_soap_xml(xml: &str) -> Result<()> {
        // Verify that it parses, but discard because it has no
        // useful content for us
        let _envelope: soap_empty_resp::Envelope = instant_xml::from_str(xml)?;
        Ok(())
    }
}

impl SonosDevice {
    pub fn device_spec(&self) -> &DeviceSpec {
        &self.device
    }

    pub async fn subscribe_helper<T: DecodeXml + 'static>(
        &self,
        service: &str,
    ) -> Result<EventStream<T>> {
        let service = self
            .device
            .get_service(service)
            .ok_or_else(|| Error::UnsupportedService(service.to_string()))?;
        service.subscribe(&self.url).await
    }

    /// This is a low level helper function for performing a SOAP Action
    /// request. You most likely want to use one of the methods
    /// implemented by the various service traits instead of this.
    pub async fn action<REQ: ToXml, RESP>(
        &self,
        service: &str,
        action: &str,
        payload: REQ,
    ) -> Result<RESP>
    where
        RESP: FromXmlOwned + std::fmt::Debug + DecodeSoapResponse,
    {
        let service = self
            .device
            .get_service(service)
            .ok_or_else(|| Error::UnsupportedService(service.to_string()))?;

        let envelope = soap::Envelope {
            encoding_style: SOAP_ENCODING,
            body: soap::Body { payload },
        };

        let body = instant_xml::to_string(&envelope)?;
        log::trace!("Sending: {body}");

        let soap_action = format!("\"{}#{action}\"", service.service_type);
        let url = service.control_url(&self.url);

        let response = reqwest::Client::new()
            .post(url)
            .header("CONTENT-TYPE", "text/xml; charset=\"utf-8\"")
            .header("SOAPAction", soap_action)
            .body::<String>(body.into())
            .send()
            .await?;

        let response = Error::check_response(response).await?;

        let body = response.text().await?;
        log::trace!("Got response: {body}");

        RESP::decode_soap_xml(&body)
    }
}

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

    #[test]
    fn test_xml() {
        use crate::av_transport::StopRequest;
        let stop = StopRequest { instance_id: 32 };
        k9::snapshot!(
            instant_xml::to_string(&stop).unwrap(),
            r#"<Stop xmlns="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID xmlns="">32</InstanceID></Stop>"#
        );
    }

    #[test]
    fn test_soap_envelope() {
        use crate::av_transport::StopRequest;

        let action = soap::Envelope {
            encoding_style: crate::SOAP_ENCODING,
            body: soap::Body {
                payload: StopRequest { instance_id: 0 },
            },
        };

        k9::snapshot!(
            instant_xml::to_string(&action).unwrap(),
            r#"<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><Stop xmlns="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID xmlns="">0</InstanceID></Stop></s:Body></s:Envelope>"#
        );
    }
}