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:
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
|
||||
Reference in New Issue
Block a user