Skip to main content

sonos_api/services/zone_group_topology/
operations.rs

1//! ZoneGroupTopology service operations and events
2//!
3//! This service manages the topology of zone groups in a Sonos household.
4//! It tracks which speakers are grouped together, coordinator relationships,
5//! and network topology information.
6
7use crate::{define_operation_with_response, Validate};
8use paste::paste;
9
10// Get the current zone group topology
11define_operation_with_response! {
12    operation: GetZoneGroupStateOperation,
13    action: "GetZoneGroupState",
14    service: ZoneGroupTopology,
15    request: {},
16    response: GetZoneGroupStateResponse {
17        zone_group_state: String,
18    },
19    xml_mapping: {
20        zone_group_state: "ZoneGroupState",
21    },
22}
23
24// Default Validate implementation for GetZoneGroupState operation (no parameters)
25impl Validate for GetZoneGroupStateOperationRequest {
26    // No validation needed for parameterless operation
27}
28
29// Convenience function
30pub use get_zone_group_state_operation as get_zone_group_state;
31
32/// Service identifier for ZoneGroupTopology
33pub const SERVICE: crate::Service = crate::Service::ZoneGroupTopology;
34
35/// Subscribe to ZoneGroupTopology events
36///
37/// This is a convenience function that subscribes to ZoneGroupTopology service events.
38/// Events include speakers joining/leaving groups, coordinator changes, etc.
39///
40/// # Arguments
41/// * `client` - The SonosClient to use for the subscription
42/// * `ip` - The IP address of the Sonos device
43/// * `callback_url` - URL where the device will send event notifications
44///
45/// # Returns
46/// A managed subscription for ZoneGroupTopology events
47pub fn subscribe(
48    client: &crate::SonosClient,
49    ip: &str,
50    callback_url: &str,
51) -> crate::Result<crate::ManagedSubscription> {
52    client.subscribe(ip, SERVICE, callback_url)
53}
54
55/// Subscribe to ZoneGroupTopology events with custom timeout
56pub fn subscribe_with_timeout(
57    client: &crate::SonosClient,
58    ip: &str,
59    callback_url: &str,
60    timeout_seconds: u32,
61) -> crate::Result<crate::ManagedSubscription> {
62    client.subscribe_with_timeout(ip, SERVICE, callback_url, timeout_seconds)
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_get_zone_group_state_operation() {
71        let op = get_zone_group_state_operation().build().unwrap();
72        assert_eq!(op.metadata().action, "GetZoneGroupState");
73    }
74
75    #[test]
76    fn test_service_constant() {
77        assert_eq!(SERVICE, crate::Service::ZoneGroupTopology);
78    }
79
80    #[test]
81    fn test_subscription_helpers() {
82        let client = crate::SonosClient::new();
83
84        // Test that functions exist with correct signatures
85        let _subscribe_fn = || subscribe(&client, "192.168.1.100", "http://callback.url");
86
87        let _subscribe_timeout_fn =
88            || subscribe_with_timeout(&client, "192.168.1.100", "http://callback.url", 3600);
89    }
90}