02-ago: [proyecto] vigilancia - apagado de la tele por ADB (KEYCODE_POWER) tras el anuncio, sin VoiceMonkey + adb-tools ignorado
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,3 +27,4 @@ vigilancia/.venv/
|
|||||||
vigilancia/modelos/
|
vigilancia/modelos/
|
||||||
vigilancia/conocidos/
|
vigilancia/conocidos/
|
||||||
vigilancia/voz_media/
|
vigilancia/voz_media/
|
||||||
|
vigilancia/adb-tools/
|
||||||
|
|||||||
@@ -62,3 +62,10 @@ VOZ_MENSAJE_CONOCIDO = os.getenv(
|
|||||||
"{nombres}, ¿qué estás haciendo? No entres ahí.",
|
"{nombres}, ¿qué estás haciendo? No entres ahí.",
|
||||||
)
|
)
|
||||||
ALEXA_APAGAR_TV_MONKEY = os.getenv("ALEXA_APAGAR_TV_MONKEY", "").strip()
|
ALEXA_APAGAR_TV_MONKEY = os.getenv("ALEXA_APAGAR_TV_MONKEY", "").strip()
|
||||||
|
ADB_PATH = os.getenv(
|
||||||
|
"ADB_PATH",
|
||||||
|
os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||||
|
"adb-tools", "platform-tools", "adb.exe"),
|
||||||
|
)
|
||||||
|
ADB_TV_IP = os.getenv("ADB_TV_IP", "192.168.50.16:5555")
|
||||||
|
GOOGLE_TV_APAGAR_VIA_ADB = os.getenv("GOOGLE_TV_APAGAR_VIA_ADB", "1") == "1"
|
||||||
|
|||||||
46
vigilancia/tv_adb.py
Normal file
46
vigilancia/tv_adb.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
|
import config
|
||||||
|
|
||||||
|
|
||||||
|
def _adb(args, timeout=20):
|
||||||
|
try:
|
||||||
|
return subprocess.run(
|
||||||
|
[config.ADB_PATH] + args,
|
||||||
|
capture_output=True, text=True, timeout=timeout,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[ADB] Error: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def estado():
|
||||||
|
r = _adb(["devices"])
|
||||||
|
if r is None:
|
||||||
|
return "error"
|
||||||
|
for linea in r.stdout.splitlines()[1:]:
|
||||||
|
if linea.strip().startswith(config.ADB_TV_IP):
|
||||||
|
if "unauthorized" in linea:
|
||||||
|
return "unauthorized"
|
||||||
|
if "device" in linea:
|
||||||
|
return "device"
|
||||||
|
return "offline"
|
||||||
|
|
||||||
|
|
||||||
|
def conectar():
|
||||||
|
_adb(["connect", config.ADB_TV_IP])
|
||||||
|
time.sleep(1)
|
||||||
|
return estado()
|
||||||
|
|
||||||
|
|
||||||
|
def apagar_tv():
|
||||||
|
"""Apaga la tele igual que el boton POWER del mando (KEYCODE_POWER)."""
|
||||||
|
e = conectar()
|
||||||
|
if e != "device":
|
||||||
|
print(f"[ADB] Tele no disponible ({e})")
|
||||||
|
return False
|
||||||
|
r = _adb(["-s", config.ADB_TV_IP, "shell", "input", "keyevent", "26"])
|
||||||
|
ok = r is not None and r.returncode == 0
|
||||||
|
print("[ADB] Comando POWER enviado" if ok else "[ADB] Fallo al enviar POWER")
|
||||||
|
return ok
|
||||||
@@ -140,7 +140,10 @@ def anuncio_en_teles(texto):
|
|||||||
if _esperar_fin(c):
|
if _esperar_fin(c):
|
||||||
print(f"[Voz] Anuncio terminado en {c.cast_info.friendly_name}")
|
print(f"[Voz] Anuncio terminado en {c.cast_info.friendly_name}")
|
||||||
|
|
||||||
if config.ALEXA_APAGAR_TV_MONKEY:
|
if config.GOOGLE_TV_APAGAR_VIA_ADB:
|
||||||
|
from tv_adb import apagar_tv
|
||||||
|
apagar_tv()
|
||||||
|
elif config.ALEXA_APAGAR_TV_MONKEY:
|
||||||
print("[Voz] Apagando la tele via Alexa...")
|
print("[Voz] Apagando la tele via Alexa...")
|
||||||
from notificador import disparar_monkey_alexa
|
from notificador import disparar_monkey_alexa
|
||||||
disparar_monkey_alexa(config.ALEXA_APAGAR_TV_MONKEY)
|
disparar_monkey_alexa(config.ALEXA_APAGAR_TV_MONKEY)
|
||||||
|
|||||||
Reference in New Issue
Block a user