From 447f88aa89041f58c85e60c3e496ed1b5471eb29 Mon Sep 17 00:00:00 2001 From: Johan Frick Date: Sun, 1 Oct 2023 20:38:38 +0200 Subject: [PATCH] feature: make it possible to exclude items in predefined group entities list --- README.md | 6 +++++- src/editor/predefined-group-editor.ts | 1 + src/model/store.ts | 17 ++++++++++++++++- src/types.ts | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 31a4b1e2..c1925b6e 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,17 @@ predefinedGroups: # defaults to empty entities: - media_player.bedroom - media_player.hall - - name: Kök&Hall + - name: Kitchen&Hall media: Legendary # If you want to start playing a specific favorite when grouping entities: # Use below format if you want to set the volume of the speakers when grouping - player: media_player.kitchen volume: 10 - player: media_player.hall volume: 5 + - name: All (except TV) + excludeItemsInEntitiesList: true # Invert entities selection, so that all players will be grouped except those in the entities list + entities: + - media_player.tv # player specific showVolumeUpAndDownButtons: true # default is false, shows buttons for increasing and decreasing volume diff --git a/src/editor/predefined-group-editor.ts b/src/editor/predefined-group-editor.ts index 68215d28..ff6338a3 100644 --- a/src/editor/predefined-group-editor.ts +++ b/src/editor/predefined-group-editor.ts @@ -11,6 +11,7 @@ class PredefinedGroupEditor extends BaseEditor { private schema = [ { type: 'string', name: 'name', required: true }, { type: 'string', name: 'media' }, + { type: 'boolean', name: 'excludeItemsInEntitiesList' }, { name: 'entities', selector: { entity: { multiple: true, filter: { integration: 'sonos', domain: 'media_player' } } }, diff --git a/src/model/store.ts b/src/model/store.ts index f67470b1..75e9c77f 100644 --- a/src/model/store.ts +++ b/src/model/store.ts @@ -59,7 +59,11 @@ export default class Store { private createPredefinedGroup(configItem: ConfigPredefinedGroup): PredefinedGroup | undefined { let result = undefined; const entities: PredefinedGroupPlayer[] = []; - for (const item of configItem.entities) { + let configEntities = configItem.entities; + if (configItem.excludeItemsInEntitiesList) { + configEntities = this.convertExclusionsInPredefinedGroupsToInclusions(configEntities); + } + for (const item of configEntities) { const predefinedGroupPlayer = this.createPredefinedGroupPlayer(item); if (predefinedGroupPlayer) { entities.push(predefinedGroupPlayer); @@ -74,6 +78,17 @@ export default class Store { return result; } + private convertExclusionsInPredefinedGroupsToInclusions(configEntities: (string | ConfigPredefinedGroupPlayer)[]) { + return this.allMediaPlayers + .filter( + (mp) => + !configEntities.find((player) => { + return (typeof player === 'string' ? player : player.player) === mp.id; + }), + ) + .map((mp) => mp.id); + } + private createPredefinedGroupPlayer(configItem: string | ConfigPredefinedGroupPlayer) { let pgEntityId: string; let volume; diff --git a/src/types.ts b/src/types.ts index 0535d969..cb627312 100644 --- a/src/types.ts +++ b/src/types.ts @@ -84,6 +84,7 @@ export interface PredefinedGroup { media?: string; volume?: number; unmuteWhenGrouped?: boolean; + excludeItemsInEntitiesList?: boolean; } export interface PredefinedGroupPlayer {