Skip to content

Commit 1256838

Browse files
authored
Update fast-search-card.js
1 parent d7c1f6d commit 1256838

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

dist/fast-search-card.js

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12093,7 +12093,7 @@ class FastSearchCard extends HTMLElement {
1209312093
console.log('TTS Button:', ttsBtn);
1209412094

1209512095
if (prevBtn) prevBtn.addEventListener('click', () => this.callMusicAssistantService('media_previous_track', item.id));
12096-
if (playPauseBtn) playPauseBtn.addEventListener('click', () => this.callMusicAssistantService('media_play_pause', item.id));
12096+
if (playPauseBtn) playPauseBtn.addEventListener('click', () => this.smartPlayPause(item));
1209712097
if (nextBtn) nextBtn.addEventListener('click', () => this.callMusicAssistantService('media_next_track', item.id));
1209812098

1209912099
// Music Assistant Toggle
@@ -12179,6 +12179,99 @@ class FastSearchCard extends HTMLElement {
1217912179
}
1218012180
}
1218112181

12182+
// Helper: Extract player name from entity ID for Music Assistant queue lookup
12183+
extractPlayerNameFromId(entityId) {
12184+
if (!entityId || typeof entityId !== 'string') return null;
12185+
12186+
return entityId
12187+
.replace('media_player.', '') // Remove domain prefix
12188+
.replace('music_assistant_', '') // Remove MA prefix
12189+
.replace('ma_', ''); // Remove shortened MA prefix
12190+
}
12191+
12192+
// Helper: Get Music Assistant Queue State
12193+
async getQueueState(playerId) {
12194+
try {
12195+
const playerName = this.extractPlayerNameFromId(playerId);
12196+
if (!playerName) {
12197+
console.log(`❌ Could not extract player name from: ${playerId}`);
12198+
return null;
12199+
}
12200+
12201+
const queueEntityId = `media_player.music_assistant_queue_${playerName}`;
12202+
const queueState = this._hass.states[queueEntityId];
12203+
12204+
if (!queueState) {
12205+
console.log(`❌ Queue entity not found: ${queueEntityId}`);
12206+
return null;
12207+
}
12208+
12209+
const queueItems = queueState.attributes.queue_items || 0;
12210+
const mediaContentId = queueState.attributes.media_content_id;
12211+
12212+
console.log(`🔍 Queue ${queueEntityId}: ${queueItems} items, current: ${mediaContentId}`);
12213+
12214+
return {
12215+
hasContent: queueItems > 0 && mediaContentId,
12216+
mediaContentId: mediaContentId,
12217+
queueItems: queueItems,
12218+
queueEntityId: queueEntityId
12219+
};
12220+
12221+
} catch (error) {
12222+
console.error(`❌ Error checking queue state for ${playerId}:`, error);
12223+
return null;
12224+
}
12225+
}
12226+
12227+
// Smart Play/Pause with Queue awareness
12228+
async smartPlayPause(item) {
12229+
const state = this._hass.states[item.id];
12230+
const isPlayerOff = ['off', 'idle', 'unavailable'].includes(state.state);
12231+
12232+
console.log(`🎵 Smart Play/Pause for ${item.id}: Player state = ${state.state}`);
12233+
12234+
if (!isPlayerOff) {
12235+
// Player läuft bereits - Standard Toggle
12236+
console.log(`✅ Player active, using standard toggle`);
12237+
return this.callMusicAssistantService('media_play_pause', item.id);
12238+
}
12239+
12240+
// Player ist aus - prüfe Queue
12241+
console.log(`🔍 Player off, checking queue...`);
12242+
const queueState = await this.getQueueState(item.id);
12243+
12244+
if (queueState?.hasContent) {
12245+
// Queue hat Inhalt - explizit abspielen
12246+
console.log(`🎵 Found queue content: ${queueState.mediaContentId}`);
12247+
return this.playFromQueue(item.id, queueState.mediaContentId);
12248+
}
12249+
12250+
// Fallback: Standard Play
12251+
console.log(`⚠️ No queue content, using standard play`);
12252+
return this.callMusicAssistantService('media_play_pause', item.id);
12253+
}
12254+
12255+
// Play specific media from queue
12256+
async playFromQueue(playerId, mediaContentId) {
12257+
console.log(`🎵 Playing from queue: ${mediaContentId} on ${playerId}`);
12258+
12259+
try {
12260+
await this._hass.callService('music_assistant', 'play_media', {
12261+
entity_id: playerId,
12262+
media_id: mediaContentId,
12263+
enqueue: 'play'
12264+
});
12265+
console.log(`✅ Queue play successful`);
12266+
} catch (error) {
12267+
console.error('❌ Queue play failed, fallback to standard play:', error);
12268+
// Fallback
12269+
this.callMusicAssistantService('media_play_pause', playerId);
12270+
}
12271+
}
12272+
12273+
12274+
1218212275
callMusicAssistantService(service, entity_id, data = {}) {
1218312276
// Prüfe ob es ein Music Assistant Player ist
1218412277
if (entity_id.includes('ma_') || entity_id.startsWith('music_assistant')) {

0 commit comments

Comments
 (0)