From af3be46f2d2291ed8d6dd4e8fb194925a24e0669 Mon Sep 17 00:00:00 2001 From: bertrandda Date: Sun, 8 Dec 2024 23:27:05 +0100 Subject: [PATCH] Promisify setVolume --- .../services/airplay/lib/airplay.setValue.js | 2 +- .../google-cast/lib/google_cast.setValue.js | 13 ++++----- server/services/sonos/lib/sonos.setValue.js | 2 +- .../airplay/lib/airplay.setValue.test.js | 8 +++++- .../services/sonos/lib/sonos.setValue.test.js | 27 +++++++++++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/server/services/airplay/lib/airplay.setValue.js b/server/services/airplay/lib/airplay.setValue.js index 2234538daf..1923aca9dc 100644 --- a/server/services/airplay/lib/airplay.setValue.js +++ b/server/services/airplay/lib/airplay.setValue.js @@ -20,7 +20,7 @@ async function setValue(device, deviceFeature, value, options) { if (deviceFeature.type === DEVICE_FEATURE_TYPES.MUSIC.PLAY_NOTIFICATION) { const client = new this.Airtunes(); const airplayDevice = client.add(ipAddress, { - volume: options.volume || 70, + volume: options?.volume || 70, }); let decodeProcess; diff --git a/server/services/google-cast/lib/google_cast.setValue.js b/server/services/google-cast/lib/google_cast.setValue.js index 028e17f166..32adb8a68f 100644 --- a/server/services/google-cast/lib/google_cast.setValue.js +++ b/server/services/google-cast/lib/google_cast.setValue.js @@ -1,3 +1,5 @@ +const { promisify } = require('util'); + const { DEVICE_FEATURE_TYPES } = require('../../../utils/constants'); const logger = require('../../../utils/logger'); /** @@ -20,17 +22,12 @@ async function setValue(device, deviceFeature, value, options) { const { Client, DefaultMediaReceiver } = this.googleCastLib; const client = new Client(); - client.connect(ipAddress, () => { + client.connect(ipAddress, async () => { logger.debug('Google Cast Connected, launching app ...'); + const setVolume = promisify(client.setVolume.bind(client)); if (options.volume) { - client.setVolume({ level: options.volume / 100 }, (err, newvol) => { - if (err) { - logger.debug('there was an error setting the volume'); - } else { - logger.debug('volume changed to %s', newvol); - } - }); + await setVolume({ level: options.volume / 100 }); } client.launch(DefaultMediaReceiver, (err, player) => { diff --git a/server/services/sonos/lib/sonos.setValue.js b/server/services/sonos/lib/sonos.setValue.js index bc93886f8c..0243448407 100644 --- a/server/services/sonos/lib/sonos.setValue.js +++ b/server/services/sonos/lib/sonos.setValue.js @@ -31,7 +31,7 @@ async function setValue(device, deviceFeature, value, options) { await sonosDevice.PlayNotification({ trackUri: value, onlyWhenPlaying: false, - volume: options.volume || 45, // Set the volume for the notification (and revert back afterwards) + volume: options?.volume || 45, // Set the volume for the notification (and revert back afterwards) timeout: 20, // If the events don't work (to see when it stops playing) or if you turned on a stream, // it will revert back after this amount of seconds. delayMs: 700, // Pause between commands in ms, (when sonos fails to play sort notification sounds). diff --git a/server/test/services/airplay/lib/airplay.setValue.test.js b/server/test/services/airplay/lib/airplay.setValue.test.js index d5b77c3b50..848b4fe509 100644 --- a/server/test/services/airplay/lib/airplay.setValue.test.js +++ b/server/test/services/airplay/lib/airplay.setValue.test.js @@ -85,9 +85,15 @@ describe('AirplayHandler.setValue', () => { airplayHandler.scanTimeout = 1; const devices = await airplayHandler.scan(); const device = devices[0]; - await airplayHandler.setValue(device, device.features[0], 'http://play-url.com', { volume: 30 }); + await airplayHandler.setValue(device, device.features[0], 'http://play-url.com'); sinon.assert.calledOnce(pipe); }); + it('should talk on speaker with custom volume', async () => { + airplayHandler.scanTimeout = 1; + const devices = await airplayHandler.scan(); + const device = devices[0]; + await airplayHandler.setValue(device, device.features[0], 'http://play-url.com', { volume: 30 }); + }); it('should return device not found', async () => { airplayHandler.scanTimeout = 1; const device = { diff --git a/server/test/services/sonos/lib/sonos.setValue.test.js b/server/test/services/sonos/lib/sonos.setValue.test.js index 714fc3503c..4fef0f080c 100644 --- a/server/test/services/sonos/lib/sonos.setValue.test.js +++ b/server/test/services/sonos/lib/sonos.setValue.test.js @@ -171,6 +171,33 @@ describe('SonosHandler.setValue', () => { assert.calledWith(deviceSetVolume, 46); }); it('should play notification on Sonos', async () => { + const device = { + name: 'My sonos', + external_id: 'sonos:test-uuid', + service_id: 'ffa13430-df93-488a-9733-5c540e9558e0', + should_poll: false, + }; + const deviceFeature = { + name: 'My sonos - Play notification', + external_id: 'sonos:test-uuid:play-notification', + category: 'music', + type: 'play_notification', + min: 1, + max: 1, + keep_history: false, + read_only: false, + has_feedback: false, + }; + await sonosHandler.setValue(device, deviceFeature, 'http://test.com'); + assert.calledWith(devicePlayNotification, { + onlyWhenPlaying: false, + timeout: 20, + trackUri: 'http://test.com', + volume: 45, + delayMs: 700, + }); + }); + it('should play notification on Sonos and change volume', async () => { const device = { name: 'My sonos', external_id: 'sonos:test-uuid',