02-ago: [proyecto] vigilancia - fix apagado prematuro: esperar SOLO la media con nuestra URL (content_id) en reproduccion y fin, no estados viejos de la sirena

This commit is contained in:
minguezsanzjuanjose
2026-08-02 14:53:39 +02:00
parent 67ff032eb2
commit 2b13da934e

View File

@@ -109,16 +109,14 @@ def _url_audio(texto, nombre="anuncio.mp3"):
return f"http://{_ip_lan()}:{config.GOOGLE_TV_PUERTO}/{os.path.basename(ruta)}" return f"http://{_ip_lan()}:{config.GOOGLE_TV_PUERTO}/{os.path.basename(ruta)}"
def _esperar_reproduccion(cast, timeout=15): def _esperar_reproduccion(cast, url, timeout=20):
"""Espera a que la reproduccion realmente empiece (PLAYING/BUFFERING).""" """Espera a que NUESTRA media empiece (PLAYING/BUFFERING) por content_id."""
mc = cast.media_controller mc = cast.media_controller
t0 = time.time() t0 = time.time()
while time.time() - t0 < timeout: while time.time() - t0 < timeout:
try: try:
st = mc.status st = mc.status
if st.player_state in ("PLAYING", "BUFFERING"): if st.content_id == url and st.player_state in ("PLAYING", "BUFFERING"):
return True
if st.player_state == "IDLE" and st.idle_reason == "FINISHED":
return True return True
except Exception: except Exception:
pass pass
@@ -126,22 +124,19 @@ def _esperar_reproduccion(cast, timeout=15):
return False return False
def _esperar_fin(cast, timeout=120): def _esperar_fin(cast, url, timeout=120):
"""Espera a que el audio termine de verdad (IDLE + idle_reason FINISHED).""" """Espera a que NUESTRA media termine (IDLE + FINISHED) por content_id."""
mc = cast.media_controller mc = cast.media_controller
t0 = time.time() t0 = time.time()
visto_playing = False
while time.time() - t0 < timeout: while time.time() - t0 < timeout:
try: try:
st = mc.status st = mc.status
if st.player_state == "PLAYING": if st.content_id == url and st.player_state == "IDLE" and st.idle_reason == "FINISHED":
visto_playing = True
if st.player_state == "IDLE" and st.idle_reason == "FINISHED":
return True return True
except Exception: except Exception:
pass pass
time.sleep(1) time.sleep(1)
return visto_playing return False
def _generar_pitido(): def _generar_pitido():
@@ -216,10 +211,9 @@ def _url_silencio():
def _reproducir_y_esperar(c, url, content_type, titulo, desc): def _reproducir_y_esperar(c, url, content_type, titulo, desc):
c.media_controller.play_media(url, content_type, title=titulo) c.media_controller.play_media(url, content_type, title=titulo)
c.media_controller.block_until_active(timeout=10)
print(f"[Voz] {desc}") print(f"[Voz] {desc}")
_esperar_reproduccion(c) _esperar_reproduccion(c, url)
return _esperar_fin(c) return _esperar_fin(c, url)
def anuncio_en_teles(texto): def anuncio_en_teles(texto):