Sure. This is the script I written today to control speakers using fibaro keyfob. In it you have how to group, ungroup speakers, play favorites, increase and decrease volume and also how to mute and unmute.
I still didn’t do full testing of it but it should give you idea how to use homey script to control speakers. It combines usage of runFlowCardAction
and setCapabilityValue
. to achieve what I need. It am not using playlists but you just need to use playlist action card same way I am using play favorite action card. (fibaro keyfob part is removed as it is not necessary here).
//Devices.
const bedroomSpeaker = await Homey.devices.getDevice({id:`<here you put speaker device id>`});
const livingRoomSpeakers = await Homey.devices.getDevice({id:`<here you put speaker device id>`});
const kitchenSpeaker = await Homey.devices.getDevice({id:`<here you put speaker device id>`,});
const romeSpeaker = await Homey.devices.getDevice({id:`<here you put speaker device id>`});
//Volume.
const normalVolume = await Homey.logic.getVariable({id:`<varibale id>`});
const roamNormalVolume = await Homey.logic.getVariable({id:`<varibale id>`});
const volumeIncrement = await Homey.logic.getVariable({id:`<varibale id>`});
//Actions.
const actionPlayFavorit = `cloud_play_sonos_favorite`;
const actionJoinSpeaker = `cloud_join_player`;
const actionLeaveCurrentGroup = `cloud_leave_current_group`;
//Capabilities
const capabilitySetVolume = `volume_set`;
const capabilityMute = `volume_mute`;
//Sonos Favorites.
const favorites = await Homey.flow.getFlowCardAutocomplete({
uri: `homey:device:${livingRoomSpeakers.id}`,
id: actionPlayFavorit,
name: `favorite`,
query: ``,
type: `flowcardaction`
});
const dasko_i_mladja = favorites[0];
const flow = favorites[1];
const indie_gold = favorites[2];
const jazz_london_radio = favorites[3];
const kexp_fm = favorites[4];
const punk_fm = favorites[5];
//Helper functions.
async function joinAllSpeakers()
{
await runFlowCard(bedroomSpeaker, actionJoinSpeaker, livingRoomSpeakers);
await runFlowCard(kitchenSpeaker, actionJoinSpeaker, livingRoomSpeakers);
await runFlowCard(romeSpeaker, actionJoinSpeaker, livingRoomSpeakers);
}
async function ungroupAllSpeakers()
{
await runFlowCard(bedroomSpeaker, actionLeaveCurrentGroup);
await runFlowCard(kitchenSpeaker, actionLeaveCurrentGroup);
await runFlowCard(romeSpeaker, actionLeaveCurrentGroup);
}
async function setNormalVolumeToAllSpeakers()
{
await setSpeakerVolume(bedroomSpeaker, normalVolume.value);
await setSpeakerVolume(kitchenSpeaker, normalVolume.value);
await setSpeakerVolume(romeSpeaker, roamNormalVolume.value);
}
async function playFavoriteOnAllSpekaers(favorite)
{
await setNormalVolumeToAllSpeakers();
await runFlowCard(livingRoomSpeakers, actionPlayFavorit, favorite)
await joinAllSpeakers();
}
async function joinSpeaker(device)
{
await runFlowCard(device, actionJoinSpeaker, livingRoomSpeakers);
}
async function ungroupSpeaker(device)
{
await runFlowCard(device, actionLeaveCurrentGroup);
}
async function decreaseSpeakerVolume(device)
{
const volume = getSpeakerVolume(device) - volumeIncrement.value;
await setSpeakerVolume(device, volume);
}
async function decreaseAllSpeakersVolume()
{
await decreaseSpeakerVolume(livingRoomSpeakers);
await decreaseSpeakerVolume(bedroomSpeaker);
await decreaseSpeakerVolume(romeSpeaker);
await decreaseSpeakerVolume(kitchenSpeaker);
}
async function pauseAllSpeakers()
{
await muteSpeakerVolume(livingRoomSpeakers, true)
await muteSpeakerVolume(kitchenSpeaker, true)
await muteSpeakerVolume(bedroomSpeaker, true)
await muteSpeakerVolume(romeSpeaker, true)
}
async function increaseSpeakerVolume(device)
{
const volume = getSpeakerVolume(device) + volumeIncrement.value;
await setSpeakerVolume(device, volume);
}
async function increaseAllSpeakersVolume()
{
await increaseSpeakerVolume(livingRoomSpeakers);
await increaseSpeakerVolume(bedroomSpeaker);
await increaseSpeakerVolume(romeSpeaker);
await increaseSpeakerVolume(kitchenSpeaker);
}
async function playAllSpeakers()
{
await muteSpeakerVolume(livingRoomSpeakers, false)
await muteSpeakerVolume(kitchenSpeaker, false)
await muteSpeakerVolume(bedroomSpeaker, false)
await muteSpeakerVolume(romeSpeaker, false)
}
function getSpeakerVolume(device)
{
return device.capabilitiesObj.volume_set.value;
}
async function muteSpeakerVolume(device, mute)
{
await Homey.devices.setCapabilityValue({ deviceId:device.id, capabilityId: capabilityMute, value: mute} )
}
async function setSpeakerVolume(device, value)
{
await Homey.devices.setCapabilityValue({ deviceId:device.id, capabilityId: capabilitySetVolume, value: value} )
}
async function runFlowCard(device, actionId, argument = null)
{
var argName = argument? await getFlowCardArgName(device, actionId) : null;
return await Homey.flow.runFlowCardAction({
uri: `homey:device:${device.id}`,
id: actionId,
args: argument? { [argName]: argument } : []
});
}
async function getFlowCardArgName(device, actionId)
{
const card =await Homey.flow.getFlowCardAction({
uri: `homey:device:${device.id}`,
id: actionId
});
return card.args[0].name;
}