06-sep: [domotica] Pantalla como dispositivo aparte (comando + submenu)

This commit is contained in:
juanjo
2026-09-06 15:33:20 +02:00
parent df1f7f0d93
commit 7f0769edd2
3 changed files with 50 additions and 20 deletions

View File

@@ -28,21 +28,28 @@ def _env(name):
return os.getenv(name, FILE_ENV.get(name, "")).strip()
async def _device():
host = _env("TAPO_TERMO_IP")
DISPOSITIVOS = {
"termo": "TAPO_TERMO_IP",
"pantalla": "TAPO_PANTALLA_IP",
}
async def _device(nombre="termo"):
var_ip = DISPOSITIVOS.get(nombre, "TAPO_TERMO_IP")
host = _env(var_ip)
username = _env("TAPO_USERNAME")
password = _env("TAPO_PASSWORD")
if not host or not username or not password:
raise RuntimeError(
"Falta configurar TAPO_TERMO_IP, TAPO_USERNAME y TAPO_PASSWORD en .env"
f"Falta configurar {var_ip}, TAPO_USERNAME y TAPO_PASSWORD en .env"
)
return await Discover.discover_single(
host, username=username, password=password, timeout=8
)
async def _run(action):
device = await _device()
async def _run(nombre, action):
device = await _device(nombre)
await device.update()
if action == "on":
await device.turn_on()
@@ -52,18 +59,23 @@ async def _run(action):
raise ValueError("Accion no soportada")
await device.update()
state = "encendido" if device.is_on else "apagado"
return f"termo: {state}"
return f"{nombre}: {state}"
def controlar_termo(action):
def controlar(nombre, action):
"""Ejecuta una accion Tapo y devuelve un mensaje seguro para Telegram."""
try:
return asyncio.run(_run(action))
return asyncio.run(_run(nombre, action))
except Exception as exc:
message = str(exc)
if "TAPO_" in message:
return f"Error de configuracion Tapo: {message}"
return f"No se pudo controlar el termo: {message[:180]}"
return f"No se pudo controlar {nombre}: {message[:180]}"
def controlar_termo(action):
"""Compatibilidad: el termo es el dispositivo por defecto."""
return controlar("termo", action)
async def _discover():