sonos

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// UPnP service endpoints
	AVTransportEndpoint       = "/MediaRenderer/AVTransport/Control"
	RenderingControlEndpoint  = "/MediaRenderer/RenderingControl/Control"
	ZoneGroupTopologyEndpoint = "/ZoneGroupTopology/Control"
	DevicePropertiesEndpoint  = "/DeviceProperties/Control"

	// UPnP service URNs
	AVTransportService       = "urn:schemas-upnp-org:service:AVTransport:1"
	RenderingControlService  = "urn:schemas-upnp-org:service:RenderingControl:1"
	ZoneGroupTopologyService = "urn:upnp-org:serviceId:ZoneGroupTopology"
	DevicePropertiesService  = "urn:upnp-org:serviceId:DeviceProperties"
)

Variables

This section is empty.

Functions

func ConvertSpotifyURIWithMetadata

func ConvertSpotifyURIWithMetadata(uri string) (sonosURI, metadata string)

ConvertSpotifyURIWithMetadata converts a Spotify URI to Sonos format with DIDL-Lite metadata.

func ExtractSpotifyTrackID

func ExtractSpotifyTrackID(uri string) string

ExtractSpotifyTrackID extracts a Spotify track ID from a Sonos URI.

func IsSpotifySource

func IsSpotifySource(uri string) bool

IsSpotifySource returns true if the URI indicates Spotify content.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides high-level access to Sonos devices.

func NewClient

func NewClient() *Client

NewClient creates a new Sonos client.

func (*Client) AddToGroup

func (c *Client) AddToGroup(ctx context.Context, device *Device, coordinatorUUID string) error

AddToGroup adds a device to a group.

func (*Client) AddURIToQueue

func (c *Client) AddURIToQueue(ctx context.Context, device *Device, uri, metadata string) error

AddURIToQueue adds a URI to the playback queue.

func (*Client) ClearQueue

func (c *Client) ClearQueue(ctx context.Context, device *Device) error

ClearQueue removes all items from the playback queue.

func (*Client) Discover

func (c *Client) Discover(ctx context.Context) ([]*Device, error)

Discover finds all Sonos devices on the network. Uses cached results from ~/.cache/riff/sonos-devices.json if available.

func (*Client) DiscoverFresh

func (c *Client) DiscoverFresh(ctx context.Context) ([]*Device, error)

DiscoverFresh bypasses the cache and performs fresh SSDP discovery.

func (*Client) GetDevice

func (c *Client) GetDevice(identifier string) *Device

GetDevice returns a device by identifier (UUID, name, IP, or alias).

func (*Client) GetDeviceInfo

func (c *Client) GetDeviceInfo(ctx context.Context, device *Device) (*DeviceInfo, error)

GetDeviceInfo retrieves device information.

func (*Client) GetMediaInfo

func (c *Client) GetMediaInfo(ctx context.Context, device *Device) (*MediaInfo, error)

GetMediaInfo retrieves current media information.

func (*Client) GetMusicServices

func (c *Client) GetMusicServices(ctx context.Context, device *Device) ([]MusicService, error)

GetMusicServices retrieves configured music services from the Sonos.

func (*Client) GetPositionInfo

func (c *Client) GetPositionInfo(ctx context.Context, device *Device) (*PositionInfo, error)

GetPositionInfo retrieves the current track position.

func (*Client) GetTransportInfo

func (c *Client) GetTransportInfo(ctx context.Context, device *Device) (*TransportInfo, error)

GetTransportInfo retrieves the current transport state.

func (*Client) GetVolume

func (c *Client) GetVolume(ctx context.Context, device *Device) (int, error)

GetVolume retrieves the current volume level (0-100). Results are cached for a short period to reduce network calls.

func (*Client) GetZoneGroupState

func (c *Client) GetZoneGroupState(ctx context.Context, device *Device) (*ZoneGroupState, error)

GetZoneGroupState retrieves the current zone group topology.

func (*Client) InvalidateGroupCache

func (c *Client) InvalidateGroupCache()

InvalidateGroupCache clears the zone group cache.

func (*Client) IsPlaying

func (c *Client) IsPlaying(ctx context.Context, device *Device) (bool, error)

IsPlaying returns true if the device is currently playing.

func (*Client) ListGroups

func (c *Client) ListGroups(ctx context.Context, device *Device) ([]Group, error)

ListGroups returns all speaker groups. Results are cached for a short period to reduce network calls.

func (*Client) Next

func (c *Client) Next(ctx context.Context, device *Device) error

Next skips to the next track.

func (*Client) Pause

func (c *Client) Pause(ctx context.Context, device *Device) error

Pause pauses playback.

func (*Client) Play

func (c *Client) Play(ctx context.Context, device *Device) error

Play starts playback.

func (*Client) PlayFromQueue

func (c *Client) PlayFromQueue(ctx context.Context, device *Device) error

PlayFromQueue starts playback from the queue.

func (*Client) PlayURI

func (c *Client) PlayURI(ctx context.Context, device *Device, uri, metadata string) error

PlayURI sets the transport URI and starts playback.

func (*Client) Previous

func (c *Client) Previous(ctx context.Context, device *Device) error

Previous skips to the previous track.

func (*Client) RemoveFromGroup

func (c *Client) RemoveFromGroup(ctx context.Context, device *Device) error

RemoveFromGroup removes a device from its group (makes it standalone).

func (*Client) Seek

func (c *Client) Seek(ctx context.Context, device *Device, target string) error

Seek seeks to a position in the current track.

func (*Client) SetAlias

func (c *Client) SetAlias(alias, target string)

SetAlias maps an alias to a device.

func (*Client) SetVolume

func (c *Client) SetVolume(ctx context.Context, device *Device, volume int) error

SetVolume sets the volume level (0-100).

type DIDLItem

type DIDLItem struct {
	// Dublin Core namespace elements
	Title   string `xml:"http://purl.org/dc/elements/1.1/ title"`
	Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
	// UPnP namespace elements
	Album       string `xml:"urn:schemas-upnp-org:metadata-1-0/upnp/ album"`
	AlbumArtURI string `xml:"urn:schemas-upnp-org:metadata-1-0/upnp/ albumArtURI"`
	Class       string `xml:"urn:schemas-upnp-org:metadata-1-0/upnp/ class"`
	// Default namespace
	Res string `xml:"res"`
}

DIDLItem represents a single item in DIDL-Lite metadata.

type DIDLLite

type DIDLLite struct {
	XMLName xml.Name   `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite"`
	Items   []DIDLItem `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ item"`
}

DIDLLite represents DIDL-Lite metadata format used by UPnP.

type Device

type Device struct {
	IP       string    `json:"ip"`
	Port     int       `json:"port"`
	UUID     string    `json:"uuid"`
	Model    string    `json:"model"`
	Name     string    `json:"name"`
	Location string    `json:"location"`
	LastSeen time.Time `json:"last_seen"`
}

Device represents a discovered Sonos device.

type DeviceInfo

type DeviceInfo struct {
	RoomName        string `xml:"RoomName"`
	ModelName       string `xml:"ModelName"`
	ModelNumber     string `xml:"ModelNumber"`
	SerialNumber    string `xml:"SerialNumber"`
	SoftwareVersion string `xml:"SoftwareVersion"`
}

DeviceInfo contains detailed device information.

type Discovery

type Discovery struct {
	// contains filtered or unexported fields
}

Discovery handles Sonos device discovery via SSDP.

func NewDiscovery

func NewDiscovery(timeout time.Duration) *Discovery

NewDiscovery creates a new Discovery instance.

func (*Discovery) CachedDevices

func (d *Discovery) CachedDevices() []*Device

CachedDevices returns all cached devices that haven't expired.

func (*Discovery) Discover

func (d *Discovery) Discover(ctx context.Context) ([]*Device, error)

Discover performs SSDP discovery and returns all found Sonos devices. Results are cached to ~/.cache/riff/sonos-devices.json for faster subsequent lookups.

func (*Discovery) DiscoverFresh

func (d *Discovery) DiscoverFresh(ctx context.Context) ([]*Device, error)

DiscoverFresh bypasses the cache and performs fresh SSDP discovery.

func (*Discovery) GetDevice

func (d *Discovery) GetDevice(identifier string) *Device

GetDevice returns a cached device by UUID, name, or alias.

func (*Discovery) SetAlias

func (d *Discovery) SetAlias(alias, target string)

SetAlias maps an alias name to a device UUID or IP.

type Group

type Group struct {
	ID          string    `json:"id"`
	Coordinator *Device   `json:"coordinator"`
	Members     []*Device `json:"members"`
	Name        string    `json:"name"`
}

Group represents a Sonos speaker group.

type MediaInfo

type MediaInfo struct {
	NrTracks           int    `xml:"NrTracks"`
	MediaDuration      string `xml:"MediaDuration"`
	CurrentURI         string `xml:"CurrentURI"`
	CurrentURIMetaData string `xml:"CurrentURIMetaData"`
}

MediaInfo contains current media information.

type MusicService

type MusicService struct {
	ID        string
	Name      string
	SerialNum string
}

MusicService represents a configured music service on the Sonos.

type Player

type Player struct {
	// contains filtered or unexported fields
}

Player implements core.Player for Sonos devices.

func NewPlayer

func NewPlayer(client *Client, device *Device) *Player

NewPlayer creates a new Sonos player for the given device.

func (*Player) AddToQueue

func (p *Player) AddToQueue(ctx context.Context, trackURI string) error

AddToQueue adds a track to the queue.

func (*Player) GetQueue

func (p *Player) GetQueue(ctx context.Context) (*core.Queue, error)

GetQueue returns the current queue.

func (*Player) GetRecentlyPlayed added in v0.2.0

func (p *Player) GetRecentlyPlayed(ctx context.Context, limit int) ([]core.HistoryEntry, error)

GetRecentlyPlayed returns recently played tracks. Sonos doesn't have a recently played API, so this returns empty.

func (*Player) GetState

func (p *Player) GetState(ctx context.Context) (*core.PlaybackState, error)

GetState returns the current playback state.

func (*Player) Next

func (p *Player) Next(ctx context.Context) error

Next skips to the next track.

func (*Player) Pause

func (p *Player) Pause(ctx context.Context) error

Pause pauses playback.

func (*Player) Play

func (p *Player) Play(ctx context.Context) error

Play starts playback.

func (*Player) PlayURI

func (p *Player) PlayURI(ctx context.Context, uri string) error

PlayURI plays a specific URI on the device.

func (*Player) Prev

func (p *Player) Prev(ctx context.Context) error

Prev skips to the previous track.

func (*Player) Seek

func (p *Player) Seek(ctx context.Context, positionMs int) error

Seek seeks to the specified position in milliseconds.

func (*Player) Volume

func (p *Player) Volume(ctx context.Context, percent int) error

Volume sets the volume level (0-100).

type PositionInfo

type PositionInfo struct {
	Track         int    `xml:"Track"`
	TrackDuration string `xml:"TrackDuration"`
	TrackMetaData string `xml:"TrackMetaData"`
	TrackURI      string `xml:"TrackURI"`
	RelTime       string `xml:"RelTime"`
	AbsTime       string `xml:"AbsTime"`
}

PositionInfo contains track position information.

type SOAPBody

type SOAPBody struct {
	Content []byte `xml:",innerxml"`
}

SOAPBody contains the SOAP body content.

type SOAPClient

type SOAPClient struct {
	// contains filtered or unexported fields
}

SOAPClient makes SOAP requests to Sonos devices.

func NewSOAPClient

func NewSOAPClient() *SOAPClient

NewSOAPClient creates a new SOAP client.

func (*SOAPClient) Call

func (c *SOAPClient) Call(ctx context.Context, host string, port int, endpoint, service, action string, args map[string]string) ([]byte, error)

Call makes a SOAP request to a Sonos device.

type SOAPEnvelope

type SOAPEnvelope struct {
	XMLName xml.Name `xml:"s:Envelope"`
	NS      string   `xml:"xmlns:s,attr"`
	Body    SOAPBody `xml:"s:Body"`
}

SOAPEnvelope wraps a SOAP request/response.

type TransportInfo

type TransportInfo struct {
	CurrentTransportState  string
	CurrentTransportStatus string
	CurrentSpeed           string
}

TransportInfo contains playback transport state.

type ZoneGroupState

type ZoneGroupState struct {
	Groups []Group `json:"groups"`
}

ZoneGroupState contains the parsed zone group topology.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL