Skip to content

Commit

Permalink
Promisify setVolume
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrandda committed Dec 8, 2024
1 parent dd7c8dd commit af3be46
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/services/airplay/lib/airplay.setValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 5 additions & 8 deletions server/services/google-cast/lib/google_cast.setValue.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { promisify } = require('util');

const { DEVICE_FEATURE_TYPES } = require('../../../utils/constants');
const logger = require('../../../utils/logger');
/**
Expand All @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion server/services/sonos/lib/sonos.setValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 7 additions & 1 deletion server/test/services/airplay/lib/airplay.setValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
27 changes: 27 additions & 0 deletions server/test/services/sonos/lib/sonos.setValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit af3be46

Please sign in to comment.