Skip to content

Commit f3e8529

Browse files
authored
Merge pull request #236 from svrooij/master
feat: Added `togglePlayback` to mimic the sonos button.
2 parents 768c9d9 + 8613c4b commit f3e8529

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ device.getVolume()
116116
* parseDIDL(didl)
117117
* pause()
118118
* play(uri)
119+
* togglePlayback()
119120
* previous()
120121
* queue(uri, positionInQueue)
121122
* queueNext(uri)

docs/services/AVTransport.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Set the Transport URI
2626

2727
+ **options.CurrentURIMetaData**: `string`, The metadata of the uri you wish to set.
2828

29+
**Returns**: `Object`, Parsed response data.
2930

3031
### sonos.AVTransport.AddURIToQueue(options)
3132

@@ -45,6 +46,7 @@ Add an URI to the queue
4546

4647
+ **options.EnqueueAsNext**: `number`, To Queue this item as the next item set to `1`
4748

49+
**Returns**: `Object`, Parsed response data.
4850

4951
### sonos.AVTransport.ReorderTracksInQueue(options)
5052

@@ -64,6 +66,7 @@ Reorder tracks in queue
6466

6567
+ **options.InsertBefore**: `number`, Where should these tracks be inserted?
6668

69+
**Returns**: `Object`, Parsed response data.
6770

6871
### sonos.AVTransport.SetPlayMode(playmode)
6972

@@ -73,6 +76,7 @@ Set the new playmode
7376

7477
**playmode**: `string`, One of the following `NORMAL` `REPEAT_ALL` `SHUFFLE` `SHUFFLE_NOREPEAT`
7578

79+
**Returns**: `Object`, Parsed response data.
7680

7781
### sonos.AVTransport.ConfigureSleepTimer(duration)
7882

@@ -82,6 +86,8 @@ Configure a sleeptimer.
8286

8387
**duration**: `string`, the duration as 'ISO8601Time', needs sample!
8488

89+
**Returns**: `Object`, Parsed response data.
90+
8591
### sonos.AVTransport.SnoozeAlarm(duration)
8692

8793
Snooze the current running alarm for a number of minutes.
@@ -90,10 +96,12 @@ Snooze the current running alarm for a number of minutes.
9096

9197
**duration**: `string`, The duration, as 'ISO8601Time', needs sample!
9298

99+
**Returns**: `Object`, Parsed response data.
93100

94101
### sonos.AVTransport.CurrentTrack()
95102

96103
Get information about the current track, parsed version of `GetPositionInfo()`
97104

105+
**Returns**: `Object`, The current playing track
98106

99107
* * *

docs/sonos.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Get Music Library Information
2929

3030
**Returns**: `Object`, {returned: {String}, total: {String}, items:[{title:{String}, uri: {String}}]}
3131

32-
### sonos.Sonos.searchMusicLibrary(searchType, searchTerm, options)
32+
### sonos.Sonos.searchMusicLibrary(searchType, searchTerm, requestOptions, separator)
3333

3434
Get Music Library Information
3535

@@ -39,7 +39,9 @@ Get Music Library Information
3939

4040
**searchTerm**: `String`, Optional - search term to search for
4141

42-
**options**: `Object`, Optional - default {start: 0, total: 100}
42+
**requestOptions**: `Object`, Optional - default {start: 0, total: 100}
43+
44+
**separator**: `String`, Optional - default is colon. `:` or `/`
4345

4446
**Returns**: `Object`, {returned: {String}, total: {String}, items:[{title:{String}, uri: {String}}]}
4547

@@ -91,19 +93,19 @@ Plays a uri directly (the queue stays the same)
9193

9294
Stop What's Playing
9395

94-
**Returns**: `Promise`
96+
**Returns**: `boolean`
9597

9698
### sonos.Sonos.becomeCoordinatorOfStandaloneGroup()
9799

98100
Become Coordinator of Standalone Group
99101

100-
**Returns**: `Promise`
102+
**Returns**: `boolean`
101103

102104
### sonos.Sonos.leaveGroup()
103105

104106
Leave the group (shortcut to becomeCoordinatorOfStandaloneGroup)
105107

106-
**Returns**: `Promise`
108+
**Returns**: `boolean`
107109

108110
### sonos.Sonos.joinGroup(otherDeviceName)
109111

@@ -139,6 +141,7 @@ Select specific track in queue
139141

140142
**trackNr**: `Number`, Number of track in queue (optional, indexed from 1)
141143

144+
**Returns**: `Boolean`
142145

143146
### sonos.Sonos.next()
144147

@@ -211,6 +214,7 @@ Set the LED State
211214

212215
**newState**: `String`, "On"/"Off"
213216

217+
**Returns**: `Boolean`
214218

215219
### sonos.Sonos.getZoneInfo()
216220

@@ -302,6 +306,12 @@ Get Current Playback State
302306

303307
**Returns**: `String`, the current playback state
304308

309+
### sonos.Sonos.togglePlayback()
310+
311+
Toggle the current playback, like the button. Currently only works for state `playing` or `paused`
312+
313+
**Returns**: `Boolean`
314+
305315
### sonos.Sonos.getFavoritesRadioStations(options)
306316

307317
Get Favorites Radio Stations
@@ -372,10 +382,12 @@ Reorder tracks in queue.
372382

373383
**updateId**: `number`, Not sure what this means, just leave it at `0`
374384

385+
**Returns**: `Boolean`
375386

376387
### sonos.Sonos.getSpotifyConnectInfo()
377388

378389
Get SpotifyConnect info, will error when no premium account is present
379390

391+
**Returns**: `Object`
380392

381393
* * *

lib/services/AVTransport.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ util.inherits(AVTransport, Service)
3232
* @param {number} options.InstanceID The instance you want to control is always `0`
3333
* @param {string} options.CurrentURI The new URI you wish to set.
3434
* @param {string} options.CurrentURIMetaData The metadata of the uri you wish to set.
35+
* @returns {Object} Parsed response data.
3536
*/
3637
AVTransport.prototype.SetAVTransportURI = async function (options) { return this._request('SetAVTransportURI', options) }
3738

@@ -43,6 +44,7 @@ AVTransport.prototype.SetAVTransportURI = async function (options) { return this
4344
* @param {number} options.EnqueuedURIMetaData The Metadata of the track you wish to add, see `Helpers.GenerateMetadata`
4445
* @param {number} options.DesiredFirstTrackNumberEnqueued The position in the queue
4546
* @param {number} options.EnqueueAsNext To Queue this item as the next item set to `1`
47+
* @returns {Object} Parsed response data.
4648
*/
4749
AVTransport.prototype.AddURIToQueue = async function (options) { return this._request('AddURIToQueue', options) }
4850
AVTransport.prototype.AddMultipleURIsToQueue = async function (options) { return this._request('AddMultipleURIsToQueue', options) }
@@ -55,6 +57,7 @@ AVTransport.prototype.AddMultipleURIsToQueue = async function (options) { return
5557
* @param {number} options.StartingIndex The index of the first song you want to move.
5658
* @param {number} options.NumberOfTracks How many tracks do you want to move?
5759
* @param {number} options.InsertBefore Where should these tracks be inserted?
60+
* @returns {Object} Parsed response data.
5861
*/
5962
AVTransport.prototype.ReorderTracksInQueue = async function (options) { return this._request('ReorderTracksInQueue', options) }
6063
AVTransport.prototype.RemoveTrackFromQueue = async function (options) { return this._request('RemoveTrackFromQueue', options) }
@@ -81,6 +84,7 @@ AVTransport.prototype.PreviousSection = async function (options) { return this._
8184
/**
8285
* Set the new playmode
8386
* @param {string} playmode One of the following `NORMAL` `REPEAT_ALL` `SHUFFLE` `SHUFFLE_NOREPEAT`
87+
* @returns {Object} Parsed response data.
8488
*/
8589
AVTransport.prototype.SetPlayMode = async function (playmode) { return this._request('SetPlayMode', {InstanceID: 0, NewPlayMode: playmode}) }
8690
AVTransport.prototype.SetCrossfadeMode = async function (options) { return this._request('SetCrossfadeMode', options) }
@@ -94,6 +98,7 @@ AVTransport.prototype.BecomeGroupCoordinatorAndSource = async function (options)
9498
/**
9599
* Configure a sleeptimer.
96100
* @param {string} duration the duration as 'ISO8601Time', needs sample!
101+
* @returns {Object} Parsed response data.
97102
*/
98103
AVTransport.prototype.ConfigureSleepTimer = async function (duration) { return this._request('ConfigureSleepTimer', {InstanceID: 0, NewSleepTimerDuration: duration}) }
99104
AVTransport.prototype.GetRemainingSleepTimerDuration = async function () { return this._request('GetRemainingSleepTimerDuration', {InstanceID: 0}) }
@@ -104,11 +109,13 @@ AVTransport.prototype.GetRunningAlarmProperties = async function (options) { ret
104109
/**
105110
* Snooze the current running alarm for a number of minutes.
106111
* @param {string} duration The duration, as 'ISO8601Time', needs sample!
112+
* @returns {Object} Parsed response data.
107113
*/
108114
AVTransport.prototype.SnoozeAlarm = async function (duration) { return this._request('SnoozeAlarm', {InstanceID: 0, Duration: duration}) }
109115

110116
/**
111117
* Get information about the current track, parsed version of `GetPositionInfo()`
118+
* @returns {Object} The current playing track
112119
*/
113120
AVTransport.prototype.CurrentTrack = async function () {
114121
return this.GetPositionInfo()

lib/sonos.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var SpotifyRegion = {
4747
* @class Sonos
4848
* @param {String} host IP/DNS
4949
* @param {Number} port
50-
* @return {Sonos}
50+
* @returns {Sonos}
5151
*/
5252
var Sonos = function Sonos (host, port, options) {
5353
this.host = host
@@ -309,6 +309,7 @@ Sonos.prototype.seek = async function (seconds) {
309309
/**
310310
* Select specific track in queue
311311
* @param {Number} trackNr Number of track in queue (optional, indexed from 1)
312+
* @returns {Boolean}
312313
*/
313314
Sonos.prototype.selectTrack = async function (trackNr) {
314315
debug('Sonos.selectTrack(%j)', trackNr)
@@ -413,6 +414,7 @@ Sonos.prototype.getLEDState = async function () {
413414
/**
414415
* Set the LED State
415416
* @param {String} newState "On"/"Off"
417+
* @returns {Boolean}
416418
*/
417419
Sonos.prototype.setLEDState = async function (newState) {
418420
debug('Sonos.setLEDState(%j)', newState)
@@ -467,7 +469,7 @@ Sonos.prototype.setName = async function (name) {
467469

468470
/**
469471
* Get Play Mode
470-
* @return {String}
472+
* @returns {String}
471473
*/
472474
Sonos.prototype.getPlayMode = async function () {
473475
debug('Sonos.getPlayMode()')
@@ -558,14 +560,34 @@ Sonos.prototype.getTopology = async function () {
558560
* Get Current Playback State
559561
* @returns {String} the current playback state
560562
*/
561-
Sonos.prototype.getCurrentState = function () {
563+
Sonos.prototype.getCurrentState = async function () {
562564
debug('Sonos.currentState()')
563565
if (this._isSubscribed && this._state) return this._state
564566
return this.avTransportService().GetTransportInfo()
565567
.then(result => {
566568
return Helpers.TranslateState(result.CurrentTransportState)
567569
})
568570
}
571+
572+
/**
573+
* Toggle the current playback, like the button. Currently only works for state `playing` or `paused`
574+
* @returns {Boolean}
575+
*/
576+
Sonos.prototype.togglePlayback = async function () {
577+
debug('Sonos.togglePlayback()')
578+
return this.getCurrentState()
579+
.then(state => {
580+
if (state === 'paused') {
581+
return this.play()
582+
} else if (state === 'playing') {
583+
return this.pause()
584+
} else {
585+
debug('toggle playback doesn\'t support %s', state)
586+
return false
587+
}
588+
})
589+
}
590+
569591
/**
570592
* Get Favorites Radio Stations
571593
* @param {Object} options Optional - default {start: 0, total: 100}
@@ -687,6 +709,7 @@ Sonos.prototype.playNotification = async function (options) {
687709
* @param {number} numberOfTracks How many tracks do we want to move
688710
* @param {number} insertBefore Index of place where the tracks should be moved to
689711
* @param {number} updateId Not sure what this means, just leave it at `0`
712+
* @returns {Boolean}
690713
*/
691714
Sonos.prototype.reorderTracksInQueue = async function (startingIndex, numberOfTracks, insertBefore, updateId = 0) {
692715
return this.avTransportService().ReorderTracksInQueue({
@@ -700,6 +723,7 @@ Sonos.prototype.reorderTracksInQueue = async function (startingIndex, numberOfTr
700723

701724
/**
702725
* Get SpotifyConnect info, will error when no premium account is present
726+
* @returns {Object}
703727
*/
704728
Sonos.prototype.getSpotifyConnectInfo = async function () {
705729
const uri = 'http://' + this.host + ':' + this.port + '/spotifyzc?action=getInfo'

0 commit comments

Comments
 (0)