forked from svrooij/node-sonos-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsonos-device.ts
More file actions
1368 lines (1214 loc) · 55.7 KB
/
Copy pathsonos-device.ts
File metadata and controls
1368 lines (1214 loc) · 55.7 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { EventEmitter } from 'events';
import TypedEmitter from 'typed-emitter';
import fetch from 'node-fetch';
import SonosDeviceBase from './sonos-device-base';
import {
GetZoneInfoResponse, GetZoneAttributesResponse, AddURIToQueueResponse, AVTransportServiceEvent, RenderingControlServiceEvent, MusicService, AccountData,
} from './services';
import {
PlayNotificationOptions, Alarm, TransportState, GroupTransportState, ExtendedTransportState, ServiceEvents, SonosEvents, PatchAlarm, PlayTtsOptions, BrowseResponse, ZoneGroup, ZoneMember, PlayMode, Repeat,
} from './models';
import { StrongSonosEvents } from './models/strong-sonos-events';
import { EventsError } from './models/event-errors';
import AsyncHelper from './helpers/async-helper';
import MetadataHelper from './helpers/metadata-helper';
import { SmapiClient } from './musicservices/smapi-client';
import IpHelper from './helpers/ip-helper';
import JsonHelper from './helpers/json-helper';
import TtsHelper from './helpers/tts-helper';
import PlayModeHelper from './helpers/playmode-helper';
import DeviceDescription from './models/device-description';
import { SonosState } from './models/sonos-state';
import {
PlayNotificationTwoOptions, PlayTtsTwoOptions,
} from './models/notificationQueue';
import SonosDeviceNotifications from './sonos-notification-two';
import XmlHelper from './helpers/xml-helper';
/**
* Main class to control a single sonos device.
*
* @export
* @class SonosDevice
* @extends {SonosDeviceBase}
*/
export default class SonosDevice extends SonosDeviceBase {
protected name: string | undefined;
private groupName: string | undefined;
private groupId: string | undefined;
protected coordinator: SonosDevice | undefined;
/**
* Creates an instance of SonosDevice.
* @param {string} host the ip or host of the speaker you want to add
* @param {number} [port=1400] the port (is always 1400)
* @param {(string | undefined)} [uuid=undefined] the uuid of the speaker, is set by the SonosManager. like RINCON_macaddres01400, used in some commands
* @param {(string | undefined)} [name=undefined] the name of the speaker, is set by the SonosManager by default
* @param {({coordinator?: SonosDevice; name: string; managerEvents: EventEmitter} | undefined)} [groupConfig=undefined] groupConfig is used by the SonosManager to setup group change events.
* @memberof SonosDevice
*/
constructor(host: string, port = 1400, uuid: string | undefined = undefined, name: string | undefined = undefined, groupConfig: { coordinator?: SonosDevice; name: string; managerEvents: EventEmitter, groupId?: string } | undefined = undefined) {
super(host, port, uuid);
this.name = name;
if (groupConfig) {
this.groupName = groupConfig.name;
this.groupId = groupConfig.groupId;
if (groupConfig.coordinator !== undefined && uuid !== groupConfig.coordinator.uuid) {
this.coordinator = groupConfig.coordinator;
this.handleCoordinatorSimpleStateEvent(this.coordinator.currentTransportState === TransportState.Playing ? TransportState.Playing : TransportState.Stopped);
}
if (uuid) {
groupConfig.managerEvents.on(uuid, this.boundHandleGroupUpdate);
}
}
if (IpHelper.IsIpv4(this.host) === false) {
this.debug('Sonos devices don\'t like hostnames, resolve once is faster IpHelper.ResolveHostname(host)');
}
}
/**
* Preload some device data, should be called before accessing most properties of this class.
*
* @returns {Promise<boolean>} Either returns true or throws an error
* @memberof SonosDevice
*/
public async LoadDeviceData(): Promise<boolean> {
this.zoneAttributes = await this.GetZoneAttributes();
this.name = this.zoneAttributes.CurrentZoneName;
this.volume = (await this.RenderingControlService.GetVolume({ InstanceID: 0, Channel: 'Master' })).CurrentVolume;
this.muted = (await this.RenderingControlService.GetMute({ InstanceID: 0, Channel: 'Master' })).CurrentMute;
return true;
}
/**
* Returns true if this device has no coordinator, meaning it's the group coordinator.
*/
public get IsCoordinator(): boolean {
return this.coordinator === undefined;
}
// #region Added functionality
/**
* Add One track to the queue
*
* @param {string} trackUri
* @param {number} [positionInQueue=0]
* @param {boolean} [enqueueAsNext=true]
* @returns {Promise<AddURIToQueueResponse>}
* @memberof SonosDevice
*/
public async AddUriToQueue(trackUri: string, positionInQueue = 0, enqueueAsNext = true): Promise<AddURIToQueueResponse> {
const guessedMetaData = MetadataHelper.GuessMetaDataAndTrackUri(trackUri);
return await this.AVTransportService.AddURIToQueue({
InstanceID: 0,
EnqueuedURI: guessedMetaData.trackUri,
EnqueuedURIMetaData: guessedMetaData.metadata,
DesiredFirstTrackNumberEnqueued: positionInQueue,
EnqueueAsNext: enqueueAsNext,
});
}
/**
* Get a parsed list of all alarms.
*
* @returns {Promise<Alarm[]>}
* @deprecated Will be removed in favor of Extended AlarmClockService
* @memberof SonosDevice
*/
public async AlarmList(): Promise<Alarm[]> {
return await this.AlarmClockService.ListAndParseAlarms();
}
/**
* Patch a single alarm. Only the ID and one property you want to change are required.
*
* @param {PatchAlarm} [options]
* @param {number} options.ID The ID of the alarm to update
* @param {string | undefined} options.StartLocalTime The time the alarm has to start 'hh:mm:ss'
* @param {string | undefined} options.Duration The duration of the alarm 'hh:mm:ss'
* @param {string | undefined} options.Recurrence What should the recurrence be ['DAILY','ONCE','WEEKDAYS']
* @param {boolean | undefined} options.Enabled Should this alarm be enabled
* @param {PlayMode | undefined} options.PlayMode What playmode should be used
* @param {number | undefined} options.Volume The requested alarm volume
* @deprecated Will be removed in favor of Extended AlarmClockService
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async AlarmPatch(options: PatchAlarm): Promise<boolean> {
return await this.AlarmClockService.PatchAlarm(options);
}
/**
* Execute any sonos command by name, see examples/commands.js
*
* @param {string} command Command you wish to execute, like 'Play' or 'AVTransportService.Pause'. CASE SENSITIVE!!!
* @param {(string | unknown | number | undefined)} options If the function requires options specify them here. A json string is automatically parsed to an object (if possible).
* @returns {Promise<unknown>}
* @memberof SonosDevice
*/
public ExecuteCommand(command: string, options?: string | unknown | number): Promise<unknown> {
let service = '';
let correctCommand = command;
if (command.indexOf('.') > -1) {
const split = command.split('.', 2);
[service, correctCommand] = split;
}
const foundService = this.GetServiceByName(service);
const objectToCallOn = typeof foundService !== 'undefined'
? foundService as unknown as { [key: string]: any }
: this.executeCommandGetFunctions();
const proto = Object.getPrototypeOf(objectToCallOn);
const propertyDescriptions = Object.getOwnPropertyDescriptors(proto);
const baseProto = Object.getPrototypeOf(proto);
const basePropertyDescriptions = Object.getOwnPropertyDescriptors(baseProto);
const allKeys = [...Object.keys(propertyDescriptions), ...Object.keys(basePropertyDescriptions)];
const functionToCall = allKeys.find((key) => key.toLowerCase() === correctCommand.toLowerCase());
if (typeof functionToCall === 'string' && typeof (objectToCallOn[functionToCall]) === 'function') {
if (options === undefined) {
return objectToCallOn[functionToCall]();
}
if (typeof (options) === 'string') {
const parsedOptions = JsonHelper.TryParse(options);
return objectToCallOn[functionToCall](parsedOptions);
}
// number or object options;
return objectToCallOn[functionToCall](options);
}
throw new Error(`Command ${correctCommand} isn't a function`);
}
private executeCommandGetFunctions(): { [key: string]: any } {
// This code looks weird, but is required to convince TypeScript this is actually what we want.
return this as unknown as { [key: string]: any };
}
/**
* Get the device description
*
* @returns {Promise<DeviceDescription>}
* @memberof SonosDevice
*/
public async GetDeviceDescription(): Promise<DeviceDescription> {
const resp = await fetch(`http://${this.Host}:${this.port}/xml/device_description.xml`)
.then((response) => {
if (response.ok) {
return response.text();
}
throw new Error(`Loading device description failed ${response.status} ${response.statusText}`);
});
const { root: { device } } = XmlHelper.ParseXml(resp) as { root: { device: any } };
return {
manufacturer: device.manufacturer,
modelNumber: device.modelNumber,
modelDescription: device.modelDescription,
modelName: device.modelName,
softwareVersion: device.softwareVersion,
swGen: device.swGen,
hardwareVersion: device.hardwareVersion,
serialNumber: device.serialNum,
udn: device.UDN,
minCompatibleVersion: device.minCompatibleVersion,
legacyCompatibleVersion: device.legacyCompatibleVersion,
apiVersion: device.apiVersion,
minApiVersion: device.minApiVersion,
displayVersion: device.displayVersion,
extraVersion: device.extraVersion,
roomName: device.roomName,
displayName: device.displayName,
zoneType: device.zoneType,
internalSpeakerSize: device.internalSpeakerSize,
iconUrl: `http://${this.Host}:${this.port}${device.iconList.icon.url}`,
feature1: device.feature1,
feature2: device.feature2,
feature3: device.feature3,
} as DeviceDescription;
}
/**
* Get your favorite radio shows, just a browse shortcut.
*
* @returns {Promise<BrowseResponse>}
* @memberof SonosDevice
*/
public async GetFavoriteRadioShows(): Promise<BrowseResponse> {
return await this.ContentDirectoryService.BrowseParsedWithDefaults('R:0/1');
}
/**
* Get your favorite radio stations, just a browse shortcut.
*
* @returns {Promise<BrowseResponse>}
* @memberof SonosDevice
*/
public async GetFavoriteRadioStations(): Promise<BrowseResponse> {
return await this.ContentDirectoryService.BrowseParsedWithDefaults('R:0/0');
}
/**
* Get your favorite songs, just a browse shortcut.
*
* @returns {Promise<BrowseResponse>}
* @memberof SonosDevice
*/
public async GetFavorites(): Promise<BrowseResponse> {
return await this.ContentDirectoryService.BrowseParsedWithDefaults('FV:2');
}
/**
* Get the current queue, just a browse shortcut.
*
* @returns {Promise<BrowseResponse>}
* @memberof SonosDevice
*/
public async GetQueue(): Promise<BrowseResponse> {
return await this.ContentDirectoryService.BrowseParsedWithDefaults('Q:0');
}
/**
* Join this device to an other group, if you know the coordinator uuid you can do .AVTransportService.SetAVTransportURI({InstanceID: 0, CurrentURI: `x-rincon:${uuid}`, CurrentURIMetaData: ''})
*
* @param {string} otherDevice The name of the other device, (to find the needed coordinator uuid)
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async JoinGroup(otherDevice: string): Promise<boolean> {
this.debug('JoinGroup(%s)', otherDevice);
const zones = await this.ZoneGroupTopologyService.GetParsedZoneGroupState();
const groupToJoin = zones.find((z: ZoneGroup) => z.members.some((m) => m.name.toLowerCase() === otherDevice.toLowerCase()));
if (groupToJoin === undefined) {
throw new Error(`Player '${otherDevice}' isn't found!`);
}
if (groupToJoin.members.some((m: ZoneMember) => m.uuid === this.Uuid)) {
return Promise.resolve(false); // Already in the group.
}
return await this.AVTransportService.SetAVTransportURI({ InstanceID: 0, CurrentURI: `x-rincon:${groupToJoin.coordinator.uuid}`, CurrentURIMetaData: '' });
}
public async LoadUuid(force = false): Promise<string> {
if (!this.uuid.startsWith('RINCON') || force) {
const attributes = await this.DevicePropertiesService.GetZoneInfo();
this.uuid = `RINCON_${attributes.MACAddress.replace(/:/g, '')}0${this.port}`;
}
return this.uuid;
}
private deviceId?: string;
/**
* Create a client for a specific music service
*
* @param {number} id The id of the music service, see MusicServicesList
* @param {string} options.key Cached authentication key
* @param {string} options.authToken Cached authentication token
* @returns {Promise<SmapiClient>}
* @memberof SonosDevice
*/
public async MusicServicesClient(serviceId: number, options: { key?: string; authToken?: string } = {}): Promise<SmapiClient> {
if (this.deviceId === undefined) this.deviceId = (await this.SystemPropertiesService.GetString({ VariableName: 'R_TrialZPSerial' })).StringValue;
const services = await this.MusicServicesService.ListAndParseAvailableServices(true);
if (services === undefined) throw new Error('Music list could not be loaded');
const service = services.find((s) => s.Id === serviceId);
if (service === undefined) throw new Error('MusicService could not be found');
// if (service.Policy.Auth !== 'Anonymous') throw new Error('Music service requires authentication, which isn\'t supported (yet)');
let accountData: AccountData | undefined;
if (service.Policy.Auth === 'AppLink' || service.Policy.Auth === 'DeviceLink') {
accountData = await this.SystemPropertiesService.GetAccountData(serviceId);
}
return new SmapiClient({
name: service.Name,
auth: service.Policy.Auth,
url: service.SecureUri || service.Uri,
deviceId: this.deviceId,
serviceId: service.Id,
householdId: service.Policy.Auth === 'AppLink' || service.Policy.Auth === 'DeviceLink' ? (await this.DevicePropertiesService.GetHouseholdID()).CurrentHouseholdID : undefined,
authToken: accountData?.AuthToken ?? options.authToken,
key: accountData?.Key ?? options.key,
saveNewAccount: async (serviceName, key, token) => {
await this.SystemPropertiesService.SaveAccount(serviceName, key, token);
},
});
}
/**
* Music services the user is logged-in to
*
* @returns {(Promise<Array<MusicService>>)}
* @memberof SonosDevice
*/
public async MusicServicesSubscribed(): Promise<Array<MusicService> | undefined> {
const ids = await this.SystemPropertiesService.SavedAccounts();
if (ids === undefined || ids.length === 0) {
return undefined;
}
const list = await this.MusicServicesService.ListAndParseAvailableServices(true);
return list.filter((m: MusicService) => ids.indexOf(m.Id) > -1);
}
/**
* Get the state of the speaker, which can be reverted to with RestoreState
*/
public async GetState(): Promise<SonosState> {
if (this.muted === undefined) {
this.muted = (await this.RenderingControlService.GetMute({ InstanceID: 0, Channel: 'Master' })).CurrentMute;
}
if (this.volume === undefined) {
this.volume = (await this.RenderingControlService.GetVolume({ InstanceID: 0, Channel: 'Master' })).CurrentVolume;
}
return {
mediaInfo: await this.AVTransportService.GetMediaInfo(),
muted: this.Muted === true,
positionInfo: await this.AVTransportService.GetPositionInfo(),
transportState: this.CurrentTransportStateSimple ?? (await this.AVTransportService.GetTransportInfo()).CurrentTransportState as TransportState,
volume: this.volume,
};
}
/**
* Restore to the state from before, used internally by the notification system.
*
* @param state The state of the speaker from 'GetState()'
* @param delayBetweenCommands Sonos speakers cannot process commands fast after each other. use 50ms - 800ms for best results
*/
public async RestoreState(state: SonosState, delayBetweenCommands: number | undefined = undefined): Promise<boolean> {
if (this.Volume !== state.volume) {
await this.SetVolume(state.volume);
if (delayBetweenCommands !== undefined) await AsyncHelper.Delay(delayBetweenCommands);
}
if (this.Muted !== state.muted) {
await this.RenderingControlService.SetMute({ InstanceID: 0, Channel: 'Master', DesiredMute: state.muted });
}
const isBroadcast = typeof state.mediaInfo.CurrentURIMetaData !== 'string' // Should not happen, is parsed in the service
&& state.mediaInfo.CurrentURIMetaData?.UpnpClass === 'object.item.audioItem.audioBroadcast'; // This UpnpClass should for sure be skipped.
let currentURIMetaData: typeof state.mediaInfo.CurrentURIMetaData = state.mediaInfo.CurrentURIMetaData;
if (state.mediaInfo.CurrentURI.startsWith('x-sonosapi-stream:') && typeof currentURIMetaData !== 'string') {
const storedItemId = currentURIMetaData?.ItemId;
if (storedItemId === undefined || String(storedItemId) === '-1') {
const guessedTrack = MetadataHelper.GuessTrack(state.mediaInfo.CurrentURI);
if (guessedTrack !== undefined) {
currentURIMetaData = {
...guessedTrack,
Title: currentURIMetaData?.Title ?? guessedTrack.Title,
AlbumArtUri: currentURIMetaData?.AlbumArtUri ?? guessedTrack.AlbumArtUri,
};
}
}
}
await this.AVTransportService.SetAVTransportURI({ InstanceID: 0, CurrentURI: state.mediaInfo.CurrentURI, CurrentURIMetaData: currentURIMetaData });
if (delayBetweenCommands !== undefined) await AsyncHelper.Delay(delayBetweenCommands);
if (state.positionInfo.Track > 1 && state.mediaInfo.NrTracks > 1) {
this.debug('Selecting track %d', state.positionInfo.Track);
await this.SeekTrack(state.positionInfo.Track)
.catch((err) => {
this.debug('Error selecting track, happens with some music services %o', err);
});
if (delayBetweenCommands !== undefined) await AsyncHelper.Delay(delayBetweenCommands);
}
if (state.positionInfo.RelTime && state.mediaInfo.MediaDuration !== '0:00:00' && !isBroadcast) {
this.debug('Setting back time to %s', state.positionInfo.RelTime);
await this.SeekPosition(state.positionInfo.RelTime)
.catch((err) => {
this.debug('Reverting back track time failed, happens for some music services (radio or stream). %o', err);
});
if (delayBetweenCommands !== undefined) await AsyncHelper.Delay(delayBetweenCommands);
}
if (state.transportState === TransportState.Playing || state.transportState === TransportState.Transitioning) {
await this.AVTransportService.Play({ InstanceID: 0, Speed: '1' });
}
return true;
}
// Internal notification queue
private notifications: PlayNotificationOptions[] = [];
private playingNotification?: boolean;
/**
* Play some url, and revert back to what was playing before. Very useful for playing a notification or TTS sound.
*
* @param {PlayNotificationOptions} options The options
* @param {string} [options.trackUri] The uri of the sound to play as notification, can be every supported sonos uri.
* @param {string|Track} [options.metadata] The metadata of the track to play, will be guesses if undefined.
* @param {number} [options.delayMs] Delay in ms between commands, for better notification playback stability. Use 100 to 800 for best results
* @param {callback} [options.notificationFired] Specify a callback that is called when this notification has played.
* @param {boolean} [options.onlyWhenPlaying] Only play a notification if currently playing music. You don't have to check if the user is home ;)
* @param {number} [options.timeout] Number of seconds the notification should play, as a fallback if the event doesn't come through.
* @param {number} [options.volume] Change the volume for the notification and revert afterwards.
* @returns {Promise<true>} Returns when added to queue or (for the first) when all notifications have played.
* @remarks The first notification will return when all notifications have played, notifications send in between will return when added to the queue.
* Use 'notificationFired' in the request if you want to know when your specific notification has played.
* @memberof SonosDevice
*/
public async PlayNotification(options: PlayNotificationOptions): Promise<boolean> {
this.debug('PlayNotification(%o)', options);
if (options.delayMs !== undefined && (options.delayMs < 1 || options.delayMs > 4000)) {
throw new Error('Delay (if specified) should be between 1 and 4000');
}
if (options.volume !== undefined && (options.volume < 1 || options.volume > 100)) {
throw new Error('Volume needs to be between 1 and 100');
}
const playingNotification = this.playingNotification === true;
this.playingNotification = true;
// Generate metadata if needed
if (options.metadata === undefined) {
const metaOptions = options;
const guessedMetaData = MetadataHelper.GuessMetaDataAndTrackUri(options.trackUri);
metaOptions.metadata = guessedMetaData.metadata;
metaOptions.trackUri = guessedMetaData.trackUri;
this.notifications.push(metaOptions);
} else {
this.notifications.push(options);
}
if (playingNotification) {
this.debug('Notification added to queue');
return false;
}
const state = await this.GetState();
this.debug('Current transport state is %s', state.transportState);
// Play all notifications (if calls itself if notifications where added in between)
const shouldRevert = await this.PlayNextNotification(state.transportState);
if (shouldRevert) {
// Revert everything back
this.debug('Reverting everything back to normal');
await this.RestoreState(state, options.delayMs).catch((reason) => {
this.debug('Restoring state failed %s', reason);
});
}
this.playingNotification = undefined;
return shouldRevert;
}
/**
* Download the url for the specified text, play as a notification and revert back to current track.
*
* @param {PlayTtsOptions} options
* @param {string} options.text Text to request a TTS file for.
* @param {string} options.lang Language to request tts file for.
* @param {string} [options.endpoint] TTS endpoint, see documentation, can also be set by environment variable 'SONOS_TTS_ENDPOINT'
* @param {string} [options.gender] Supply gender, some languages support both genders.
* @param {string} [options.name] Supply voice name, some services support several voices with different names.
* @param {number} [options.delayMs] Delay in ms between commands, for better notification playback stability
* @param {boolean} [options.onlyWhenPlaying] Only play a notification if currently playing music. You don't have to check if the user is home ;)
* @param {number} [options.timeout] Number of seconds the notification should play, as a fallback if the event doesn't come through.
* @param {number} [options.volume] Change the volume for the notification and revert afterwards.
* @returns {Promise<boolean>} Returns when added to queue or (for the first) when all notifications have played.
* @memberof SonosDevice
*/
public async PlayTTS(options: PlayTtsOptions): Promise<boolean> {
this.debug('PlayTTS(%o)', options);
const notificationOptions = await TtsHelper.TtsOptionsToNotification(options);
return await this.PlayNotification(notificationOptions);
}
private async PlayNextNotification(originalState: TransportState, havePlayed?: boolean): Promise<boolean> {
let result = havePlayed === true;
// Remove and returns first item from queue
const notification = this.notifications.shift();
if (notification === undefined) {
return Promise.resolve(result);
}
if (notification.onlyWhenPlaying === true && !(originalState === TransportState.Playing || originalState === TransportState.Transitioning)) {
this.debug('Skip notification, because of not playing %s', notification.trackUri);
if (notification.notificationFired !== undefined) {
notification.notificationFired(false);
}
} else {
result = true;
this.debug('Start notification playback uri %s', notification.trackUri);
await this.AVTransportService.SetAVTransportURI({ InstanceID: 0, CurrentURI: notification.trackUri, CurrentURIMetaData: notification.metadata ?? '' })
.catch((reason) => { this.debug('SetAVTransportURI failed', reason); });
if (notification.volume !== undefined && notification.volume !== this.volume) {
await this.SetVolume(notification.volume)
.catch((reason) => { this.debug('Setting volume failed', reason); });
if (notification.delayMs !== undefined) await AsyncHelper.Delay(notification.delayMs);
}
await this.AVTransportService.Play({ InstanceID: 0, Speed: '1' })
.catch((err) => { this.debug('Play threw error, wrong url? %o', err); });
// Wait for event (or timeout)
await AsyncHelper.AsyncEvent<unknown>(this.Events, SonosEvents.PlaybackStopped, notification.timeout).catch((err) => this.debug(err));
if (notification.notificationFired !== undefined) {
notification.notificationFired(true);
}
}
return this.PlayNextNotification(originalState, result);
}
// #region Notifications two
private notificationsTwo?: SonosDeviceNotifications;
/**
* A second implementation of PlayNotification.
*
* @param {PlayNotificationOptions} options The options
* @param {string} [options.trackUri] The uri of the sound to play as notification, can be every supported sonos uri.
* @param {string|Track} [options.metadata] The metadata of the track to play, will be guesses if undefined.
* @param {number} [options.delayMs] Delay in ms between commands, for better notification playback stability. Use 100 to 800 for best results
* @param {boolean} [options.onlyWhenPlaying] Only play a notification if currently playing music. You don't have to check if the user is home ;)
* @param {number} [options.timeout] Number of seconds the notification should play, as a fallback if the event doesn't come through.
* @param {number} [options.volume] Change the volume for the notification and revert afterwards.
* @param {boolean} [options.resolveAfterRevert]
* @param {number} [options.defaultTimeout]
* @param {number} [options.specificTimeout]
* @param {boolean} [options.catchQueueErrors]
*
* @deprecated This is experimental, do not depend on this. (missing the jsdocs experimental descriptor)
* @remarks This is just added to be able to test the two implementations next to each other. This will probably be removed in feature.
*/
public async PlayNotificationTwo(options: PlayNotificationTwoOptions): Promise<boolean> {
if (this.notificationsTwo === undefined) {
this.notificationsTwo = new SonosDeviceNotifications(this.AVTransportService, this.RenderingControlService, this.Events);
}
return this.notificationsTwo.PlayNotificationTwo(options);
}
/**
* Download the url for the specified text, play as a notification and revert back to current track.
*
* @param {PlayTtsOptions} options
* @param {string} options.text Text to request a TTS file for.
* @param {string} options.lang Language to request tts file for.
* @param {string} [options.endpoint] TTS endpoint, see documentation, can also be set by environment variable 'SONOS_TTS_ENDPOINT'
* @param {string} [options.gender] Supply gender, some languages support both genders.
* @param {string} [options.name] Supply voice name, some services support several voices with different names.
* @param {number} [options.delayMs] Delay in ms between commands, for better notification playback stability
* @param {boolean} [options.onlyWhenPlaying] Only play a notification if currently playing music. You don't have to check if the user is home ;)
* @param {number} [options.timeout] Number of seconds the notification should play, as a fallback if the event doesn't come through.
* @param {number} [options.volume] Change the volume for the notification and revert afterwards.
* @param {boolean} [options.resolveAfterRevert]
* @param {number} [options.defaultTimeout]
* @param {number} [options.specificTimeout]
* @param {boolean} [options.catchQueueErrors]
* @deprecated TTS using experimental notification feature
* @returns {Promise<boolean>} Returns when added to queue or (for the first) when all notifications have played.
* @memberof SonosDevice
*/
public async PlayTTSTwo(options: PlayTtsTwoOptions): Promise<boolean> {
this.debug('PlayTTSTwo(%o)', options);
const notificationOptions = await TtsHelper.TtsOptionsToNotification(options);
return await this.PlayNotificationTwo({
...notificationOptions,
catchQueueErrors: options.catchQueueErrors,
resolveAfterRevert: options.resolveAfterRevert,
defaultTimeout: options.defaultTimeout,
specificTimeout: options.specificTimeout,
});
}
// #endregion
// #region Notification AudioClip
/**
* Play an audio clip through the native (local) AudioClip command
*
* @param {PlayNotificationOptions} options The options
* @param {string} [options.trackUri] The uri of the sound to play as notification, can be every supported sonos uri.
* @param {boolean} [options.onlyWhenPlaying] Only play a notification if currently playing music. You don't have to check if the user is home ;)
* @param {number} [options.volume] Change the volume for the notification and revert afterwards.
* @returns {Promise<true>} Returns true when the AudioClip is send to the speaker, false when stopped and onlyWhenPlaying === true
* @remarks This is only supported on S2 speakers, see: https://developer.sonos.com/reference/control-api/audioclip/
* @experimental This is experimental, do not depend on this.
* @memberof SonosDevice
*/
public async PlayNotificationAudioClip(options: PlayNotificationOptions): Promise<boolean> {
this.debug('PlayNotificationAudioClip(%o)', options);
if (options.onlyWhenPlaying === true && this.CurrentTransportStateSimple === TransportState.Stopped) {
return false;
}
if (options.volume !== undefined && (options.volume < 1 || options.volume > 100)) {
throw new Error('Volume needs to be between 1 and 100');
}
const previousValue = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
// Have no idea if this should be public
// https://github.com/bencevans/node-sonos/issues/530#issuecomment-1430039043
const apiKey = '123e4567-e89b-12d3-a456-426655440000';
const body = {
name: 'Sonos TS Notification',
appId: 'io.svrooij.sonos-ts',
streamUrl: options.trackUri,
volume: options.volume ?? this.volume ?? 25,
};
return fetch(`https://${this.host}:1443/api/v1/players/local/audioClip`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Sonos-Api-Key': apiKey,
},
body: JSON.stringify(body),
}).then((response) => {
if (response.ok) {
return true;
}
throw new Error(`Playing AudioClip failed ${response.status} ${response.statusText}`);
}).finally(() => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = previousValue;
});
}
/**
* PlayTTS, but with (experimental) local AudioClip method
*
* @param {PlayTtsOptions} options
* @param {string} options.text Text to request a TTS file for.
* @param {string} options.lang Language to request tts file for.
* @param {string} [options.endpoint] TTS endpoint, see documentation, can also be set by environment variable 'SONOS_TTS_ENDPOINT'
* @param {string} [options.gender] Supply gender, some languages support both genders.
* @param {string} [options.name] Supply voice name, some services support several voices with different names.
* @param {boolean} [options.onlyWhenPlaying] Only play a notification if currently playing music. You don't have to check if the user is home ;)
* @param {number} [options.volume] Change the volume for the notification and revert afterwards.
* @returns {Promise<boolean>} Returns when added to queue or (for the first) when all notifications have played.
* @experimental This is experimental, do not depend on this.
* @memberof SonosDevice
*/
public async PlayTTSAudioClip(options: PlayTtsOptions): Promise<boolean> {
this.debug('PlayTTSAudioClip(%o)', options);
const notificationOptions = await TtsHelper.TtsOptionsToNotification(options);
return await this.PlayNotificationAudioClip(notificationOptions);
}
// #endregion
/**
* Switch the playback to this url.
*
* @param {string} trackUri
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async SetAVTransportURI(trackUri: string): Promise<boolean> {
const guessedMetaData = MetadataHelper.GuessMetaDataAndTrackUri(trackUri);
return await this.AVTransportService.SetAVTransportURI({ InstanceID: 0, CurrentURI: guessedMetaData.trackUri, CurrentURIMetaData: guessedMetaData.metadata });
}
/**
* Switch playback to line in (which will always be playing)
*
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async SwitchToLineIn(): Promise<boolean> {
await this.LoadUuid();
await this.AVTransportService
.SetAVTransportURI({ InstanceID: 0, CurrentURI: `x-rincon-stream:${this.uuid}`, CurrentURIMetaData: '' });
return await this.Play();
}
/**
* Switch playback to the queue
*
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async SwitchToQueue(): Promise<boolean> {
await this.LoadUuid();
return await this.AVTransportService
.SetAVTransportURI({ InstanceID: 0, CurrentURI: `x-rincon-queue:${this.uuid}#0`, CurrentURIMetaData: '' });
}
/**
* Switch playback to TV Input (only on Playbar)
*
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async SwitchToTV(): Promise<boolean> {
await this.LoadUuid();
await this.AVTransportService
.SetAVTransportURI({ InstanceID: 0, CurrentURI: `x-sonos-htastream:${this.uuid}:spdif`, CurrentURIMetaData: '' });
return await this.Play();
}
/**
* Toggle playback between paused and playing
*
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async TogglePlayback(): Promise<boolean> {
// Load the Current state first, if not present (eg. not using events)
const currentState = this.Coordinator.CurrentTransportStateSimple || (await this.Coordinator.AVTransportService.GetTransportInfo()).CurrentTransportState as TransportState;
return currentState === TransportState.Playing || currentState === TransportState.Transitioning
? await this.Coordinator.Pause()
: await this.Coordinator.Play();
}
// #endregion
// #region Events
private events?: TypedEmitter<StrongSonosEvents>;
private isSubscribed = false;
/**
* Cancel all subscriptions and unsubscribe for events from this device.
*
* @memberof SonosDevice
*/
public CancelEvents(): void {
if (this.events !== undefined) {
const eventNames = this.events.eventNames().filter((e) => e !== 'removeListener' && e !== 'newListener' && e !== SonosEvents.SubscriptionError) as (keyof StrongSonosEvents)[];
eventNames.forEach((e) => {
if (this.events !== undefined) this.events.removeAllListeners(e);
});
}
}
/**
* Access to the sonos event emitter. All the possible events are in the SonosEvents enum.
* Calling this will automatically subscribe for events from the device.
*
* @readonly
* @type {EventEmitter}
* @memberof SonosDevice
*/
public get Events(): TypedEmitter<StrongSonosEvents> {
if (this.events !== undefined) {
return this.events;
}
// this.events = new EventEmitter() as TypedEmitter<StrongSonosEvents>; incorrect according to DeepSource
this.events = new EventEmitter() as TypedEmitter<StrongSonosEvents>;
this.events.on('removeListener', (eventName: string | symbol) => {
this.debug('Listener removed for %s', eventName);
if (eventName === SonosEvents.SubscriptionError) return;
const events = this.Events.eventNames().filter((e) => e !== 'removeListener' && e !== 'newListener' && e !== SonosEvents.SubscriptionError);
if (events.length === 0) {
this.AVTransportService.Events.removeListener(ServiceEvents.ServiceEvent, this.boundHandleAvTransportEvent);
this.AVTransportService.Events.removeListener(ServiceEvents.SubscriptionError, this.boundHandleEventErrorEvent);
this.RenderingControlService.Events.removeListener(ServiceEvents.ServiceEvent, this.boundHandleRenderingControlEvent);
this.RenderingControlService.Events.removeListener(ServiceEvents.SubscriptionError, this.boundHandleEventErrorEvent);
this.coordinator?.events?.removeListener('simpleTransportState', this.boundHandleCoordinatorSimpleStateEvent);
this.isSubscribed = false;
}
});
this.events.on('newListener', (eventName: string | symbol) => {
this.debug('Listener added for %s (isSubscribed: %o)', eventName, this.isSubscribed);
if (eventName === SonosEvents.SubscriptionError) return;
if (!this.isSubscribed) {
this.isSubscribed = true;
this.AVTransportService.Events.on(ServiceEvents.SubscriptionError, this.boundHandleEventErrorEvent);
this.AVTransportService.Events.on(ServiceEvents.ServiceEvent, this.boundHandleAvTransportEvent);
this.RenderingControlService.Events.on(ServiceEvents.SubscriptionError, this.boundHandleEventErrorEvent);
this.RenderingControlService.Events.on(ServiceEvents.ServiceEvent, this.boundHandleRenderingControlEvent);
if (this.coordinator !== undefined) {
this.coordinator?.Events.on('simpleTransportState', this.boundHandleCoordinatorSimpleStateEvent);
this.boundHandleCoordinatorSimpleStateEvent(this.coordinator.CurrentTransportStateSimple);
}
}
});
return this.events;
}
private boundHandleEventErrorEvent = this.handleEventErrorEvent.bind(this);
private handleEventErrorEvent(err: EventsError): void {
if (this.events !== undefined) {
this.events.emit(SonosEvents.SubscriptionError, err);
}
}
private boundHandleAvTransportEvent = this.handleAvTransportEvent.bind(this);
private handleAvTransportEvent(data: AVTransportServiceEvent): void {
this.Events.emit(SonosEvents.AVTransport, data);
if (data.TransportState !== undefined && this.coordinator === undefined) {
const newState = data.TransportState as TransportState;
const newSimpleState = newState === TransportState.Paused || newState === TransportState.Stopped ? TransportState.Stopped : TransportState.Playing;
this.debug('Received TransportState new State "%s" newSimpleState "%s"', newState, newSimpleState);
if (newSimpleState !== this.CurrentTransportStateSimple) this.Events.emit(SonosEvents.CurrentTransportStateSimple, newSimpleState);
if (this.currentTransportState !== newState) {
this.currentTransportState = newState;
this.Events.emit(SonosEvents.CurrentTransportState, newState);
if (newState === TransportState.Stopped) this.Events.emit(SonosEvents.PlaybackStopped);
}
}
if (data.CurrentTrackURI && this.currentTrackUri !== data.CurrentTrackURI) {
this.currentTrackUri = data.CurrentTrackURI;
this.Events.emit(SonosEvents.CurrentTrackUri, this.currentTrackUri ?? '');
}
if (data.CurrentTrackMetaData && typeof data.CurrentTrackMetaData !== 'string') this.Events.emit(SonosEvents.CurrentTrackMetadata, data.CurrentTrackMetaData);
if (data.NextTrackURI && this.nextTrackUri !== data.NextTrackURI) {
this.nextTrackUri = data.NextTrackURI;
this.Events.emit(SonosEvents.NextTrackUri, this.nextTrackUri ?? '');
if (data.NextTrackMetaData && typeof data.NextTrackMetaData !== 'string') this.Events.emit(SonosEvents.NextTrackMetadata, data.NextTrackMetaData);
}
if (data.EnqueuedTransportURI && this.enqueuedTransportUri !== data.EnqueuedTransportURI) {
this.enqueuedTransportUri = data.EnqueuedTransportURI;
this.Events.emit(SonosEvents.EnqueuedTransportUri, this.enqueuedTransportUri ?? '');
if (data.EnqueuedTransportURIMetaData !== undefined && typeof data.EnqueuedTransportURIMetaData !== 'string') this.Events.emit(SonosEvents.EnqueuedTransportMetadata, data.EnqueuedTransportURIMetaData);
}
if (data.CurrentPlayMode) {
this.currentPlayMode = data.CurrentPlayMode;
}
}
private boundHandleRenderingControlEvent = this.handleRenderingControlEvent.bind(this);
private handleRenderingControlEvent(data: RenderingControlServiceEvent): void {
this.Events.emit(SonosEvents.RenderingControl, data);
if (data.Volume && data.Volume.Master && this.volume !== data.Volume.Master) {
this.volume = data.Volume.Master;
this.Events.emit(SonosEvents.Volume, this.volume ?? 0);
}
if (data.Mute && this.muted !== data.Mute.Master) {
this.muted = data.Mute.Master;
this.Events.emit(SonosEvents.Mute, this.muted);
}
}
private boundHandleCoordinatorSimpleStateEvent = this.handleCoordinatorSimpleStateEvent.bind(this);
private handleCoordinatorSimpleStateEvent(state: TransportState | undefined): void {
const newState = state === TransportState.Playing ? GroupTransportState.GroupPlaying : GroupTransportState.GroupStopped;
this.events?.emit('transportState', newState);
this.currentTransportState = newState;
}
// #endregion
// #region Group stuff
private boundHandleGroupUpdate = this.handleGroupUpdate.bind(this);
private handleGroupUpdate(data: { coordinator: SonosDevice | undefined; name: string; groupId: string | undefined }): void {
if (data.coordinator && data.coordinator?.uuid !== this.Uuid && (!this.coordinator || this.coordinator?.Uuid !== data.coordinator?.Uuid)) {
this.debug('Coordinator changed for %s', this.uuid);
this.coordinator?.events?.removeListener('simpleTransportState', this.boundHandleCoordinatorSimpleStateEvent);
this.coordinator = data.coordinator;
if (this.events !== undefined) {
// this.boundHandleCoordinatorSimpleStateEvent(this.coordinator.CurrentTransportStateSimple);
this.events.emit(SonosEvents.Coordinator, this.coordinator.uuid);
this.coordinator?.Events.on('simpleTransportState', this.boundHandleCoordinatorSimpleStateEvent);
setTimeout(() => {
this.boundHandleCoordinatorSimpleStateEvent(this.coordinator?.CurrentTransportStateSimple);
}, 50);
}
}
if (this.coordinator && (data.coordinator === undefined || data.coordinator.uuid === this.uuid)) {
this.debug('Coordinator removed for %s', this.uuid);
this.coordinator?.events?.removeListener('simpleTransportState', this.boundHandleCoordinatorSimpleStateEvent);
this.coordinator = undefined;
if (this.events !== undefined) {
this.events.emit(SonosEvents.Coordinator, this.uuid);
this.currentTransportState = undefined;
this.events.emit(SonosEvents.CurrentTransportState, TransportState.Stopped);
this.events.emit(SonosEvents.CurrentTransportStateSimple, TransportState.Stopped);
}
}
if (data.name && data.name !== this.groupName) {
this.groupName = data.name;
this.debug('Groupname changed for %s to %s', this.uuid, this.groupName);
if (this.events !== undefined) {
this.events.emit(SonosEvents.GroupName, this.groupName);
}
}
if (data.groupId && data.groupId !== this.groupId) {
this.groupId = data.groupId;
this.debug('GroupId changed for %s to %s', this.uuid, this.groupId);
if (this.events !== undefined) {
this.events.emit(SonosEvents.GroupId, this.groupId);
}
}
}
/**
* Get the current coordinator for this group, or the device itself if if doesn't have a coordinator.
*
* @readonly
* @type {SonosDevice}
* @memberof SonosDevice
*/
public get Coordinator(): SonosDevice {
return this.coordinator || this;
}
/**
* Get the GroupName, if device is created by the SonosManager.
*
* @readonly
* @type {(string | undefined)}
* @memberof SonosDevice
*/
public get GroupName(): string | undefined {
return this.groupName;
}
/**
* Get the GroupId, if device is created by the SonosManager.
*
* @readonly
* @type {(string | undefined)}
* @memberof SonosDevice
*/
public get GroupId(): string | undefined {
return this.groupId;
}
// #endregion
// #region Properties
protected currentPlayMode?: PlayMode;
/**