@@ -12528,53 +12528,101 @@ class FastSearchCard extends HTMLElement {
1252812528 async startTTSMonitoring(entityId) {
1252912529 console.log('🎧 Starting TTS monitoring...');
1253012530
12531- let previousState = this._hass.states[entityId]?.state;
1253212531 let ttsStarted = false;
12533- let silenceCounter = 0;
12532+ let lastPosition = null;
12533+ let lastMediaId = null;
12534+ let positionStableCount = 0;
12535+ let checksWithoutChange = 0;
12536+
12537+ // Speichere initiale Werte
12538+ const initialState = this._hass.states[entityId];
12539+ const initialMediaId = initialState?.attributes?.media_content_id;
1253412540
12535- // Überwache den Player-Status alle 100ms
1253612541 this.ttsMonitorInterval = setInterval(async () => {
1253712542 const currentState = this._hass.states[entityId];
12538-
1253912543 if (!currentState) return;
1254012544
12541- // Erkenne wenn TTS startet (Player wird aktiv)
12542- if (!ttsStarted && currentState.state === 'playing') {
12545+ const currentPosition = currentState.attributes?.media_position;
12546+ const currentMediaId = currentState.attributes?.media_content_id;
12547+ const isPlaying = currentState.state === 'playing';
12548+
12549+ // TTS Start erkennen
12550+ if (!ttsStarted && isPlaying) {
1254312551 ttsStarted = true;
12544- silenceCounter = 0 ;
12545- console.log('🎤 TTS started playing') ;
12552+ console.log('🎤 TTS started') ;
12553+ checksWithoutChange = 0 ;
1254612554 }
1254712555
12548- // Erkenne wenn TTS fertig ist
12549- if (ttsStarted && currentState.state !== 'playing') {
12550- silenceCounter++;
12551-
12552- // Warte 3 Checks (300ms) um sicher zu sein
12553- if (silenceCounter >= 3) {
12556+ // TTS Ende erkennen durch verschiedene Methoden:
12557+ if (ttsStarted) {
12558+ // Methode 1: Player ist nicht mehr playing
12559+ if (currentState.state !== 'playing') {
1255412560 console.log('🎉 TTS completed - player stopped');
1255512561 clearInterval(this.ttsMonitorInterval);
1255612562 this.ttsMonitorInterval = null;
12557-
12558- // Sofort wiederherstellen!
1255912563 await this.restorePlayerContext();
1256012564 await this.finalizeTTSProcess(entityId);
12565+ return;
12566+ }
12567+
12568+ // Methode 2: Media ID hat sich geändert (zurück zur Original-Musik)
12569+ if (currentMediaId && currentMediaId !== lastMediaId && currentMediaId === this.savedPlayerContext?.mediaContentId) {
12570+ console.log('🎉 TTS completed - original media restored');
12571+ clearInterval(this.ttsMonitorInterval);
12572+ this.ttsMonitorInterval = null;
12573+ await this.finalizeTTSProcess(entityId);
12574+ return;
12575+ }
12576+
12577+ // Methode 3: Position bewegt sich nicht mehr (TTS fertig, aber Player noch "playing")
12578+ if (currentPosition !== undefined && currentPosition !== null) {
12579+ if (currentPosition === lastPosition) {
12580+ positionStableCount++;
12581+ if (positionStableCount > 10) { // 1 Sekunde keine Bewegung
12582+ console.log('🎉 TTS completed - position stable');
12583+ clearInterval(this.ttsMonitorInterval);
12584+ this.ttsMonitorInterval = null;
12585+ await this.restorePlayerContext();
12586+ await this.finalizeTTSProcess(entityId);
12587+ return;
12588+ }
12589+ } else {
12590+ positionStableCount = 0;
12591+ }
12592+ }
12593+
12594+ // Methode 4: Keine Attribute-Änderungen für längere Zeit
12595+ const hasChanges = JSON.stringify(currentState.attributes) !== JSON.stringify(initialState.attributes);
12596+ if (!hasChanges) {
12597+ checksWithoutChange++;
12598+ if (checksWithoutChange > 20) { // 2 Sekunden keine Änderungen
12599+ console.log('🎉 TTS completed - no attribute changes');
12600+ clearInterval(this.ttsMonitorInterval);
12601+ this.ttsMonitorInterval = null;
12602+ await this.restorePlayerContext();
12603+ await this.finalizeTTSProcess(entityId);
12604+ return;
12605+ }
12606+ } else {
12607+ checksWithoutChange = 0;
1256112608 }
1256212609 }
1256312610
12564- previousState = currentState.state;
12611+ lastPosition = currentPosition;
12612+ lastMediaId = currentMediaId;
1256512613
1256612614 }, 100); // Alle 100ms prüfen
1256712615
12568- // Sicherheits-Timeout nach 30 Sekunden
12616+ // Kürzeres Sicherheits-Timeout - 10 Sekunden sollten reichen
1256912617 setTimeout(() => {
1257012618 if (this.ttsMonitorInterval) {
12571- console.log('⚠️ TTS monitoring timeout');
12619+ console.log('⚠️ TTS monitoring timeout (10s) ');
1257212620 clearInterval(this.ttsMonitorInterval);
1257312621 this.ttsMonitorInterval = null;
1257412622 this.restorePlayerContext();
1257512623 this.finalizeTTSProcess(entityId);
1257612624 }
12577- }, 30000);
12625+ }, 10000); // Reduziert von 30 auf 10 Sekunden
1257812626 }
1257912627
1258012628 // 🏁 TTS-PROZESS FINALISIEREN
0 commit comments