From 0ce21ba0346d844ed3c962add1573119314ad842 Mon Sep 17 00:00:00 2001 From: Johan Frick Date: Thu, 8 Feb 2024 19:39:22 +0100 Subject: [PATCH] feature: make it possible to adjust volume relative to main player fixes #294 --- README.md | 3 ++- src/editor/advanced-editor.ts | 4 ++++ src/services/media-control-service.ts | 7 ++++++- src/types.ts | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0d33c706..ee81f81d 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,8 @@ entities: # Entities are automatically discovered if you don't supply this setti - media_player.sonos_livingroom excludeItemsInEntitiesList: true # Will invert the selection in the `entities` list, so that all players that are not in the list will be used. showNonSonosPlayers: true # default is false, which means only Sonos players will be shown. -volumeStepSize: 1 # Use this to change the step size when using volume up/down.. Default is to use the step size of Home Assistant's media player integration. +volumeStepSize: 1 # Use this to change the step size when using volume up/down. Default is to use the step size of Home Assistant's media player integration. +adjustVolumeRelativeToMainPlayer: true # default is false, which means all players will be set to the same volume as the main player. If set to true, volume will be adjusted relative to the main player in the group. # groups specific groupsTitle: '' diff --git a/src/editor/advanced-editor.ts b/src/editor/advanced-editor.ts index ff65cabb..f450952c 100644 --- a/src/editor/advanced-editor.ts +++ b/src/editor/advanced-editor.ts @@ -48,6 +48,10 @@ export const ADVANCED_SCHEMA = [ name: 'showAudioInputFormat', selector: { boolean: {} }, }, + { + name: 'adjustVolumeRelativeToMainPlayer', + selector: { boolean: {} }, + }, { type: 'string', help: 'Override default fallback artwork image if artwork is missing for the currently selected media', diff --git a/src/services/media-control-service.ts b/src/services/media-control-service.ts index 254b15fa..06088867 100644 --- a/src/services/media-control-service.ts +++ b/src/services/media-control-service.ts @@ -104,11 +104,16 @@ export default class MediaControlService { } async volumeSet(mediaPlayer: MediaPlayer, volume: number, updateMembers = true) { - const volume_level = volume / 100; + let volume_level = volume / 100; await this.hassService.callMediaService('volume_set', { entity_id: mediaPlayer.id, volume_level: volume_level }); + const relativeVolumeChange = volume_level - mediaPlayer.attributes.volume_level; if (updateMembers) { for (const member of mediaPlayer.members) { + if (this.config.adjustVolumeRelativeToMainPlayer) { + volume_level = member.attributes.volume_level + relativeVolumeChange; + volume_level = Math.min(1, Math.max(0, volume_level)); + } await this.hassService.callMediaService('volume_set', { entity_id: member.id, volume_level }); } } diff --git a/src/types.ts b/src/types.ts index aa0349fe..c511f6ab 100644 --- a/src/types.ts +++ b/src/types.ts @@ -51,6 +51,7 @@ export interface CardConfig extends LovelaceCardConfig { replaceHttpWithHttpsForThumbnails?: boolean; volumeStepSize?: number; mediaBrowserTitle?: string; + adjustVolumeRelativeToMainPlayer?: boolean; } export interface MediaArtworkOverride {