20-sep: Home Assistant en VPS para control luces Alexa (gratis)
This commit is contained in:
98
scripts/monitor_homeassistant.py
Normal file
98
scripts/monitor_homeassistant.py
Normal file
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
Monitor que verifica cada 30 segundos si Home Assistant esta listo.
|
||||
Cuando este accesible, muestra un mensaje box y emite aviso sonoro.
|
||||
|
||||
Uso:
|
||||
python monitor_homeassistant.py
|
||||
"""
|
||||
import time
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Fix encoding para Windows
|
||||
if sys.platform == 'win32':
|
||||
import codecs
|
||||
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
|
||||
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
|
||||
|
||||
def check_homeassistant():
|
||||
"""Verifica si Home Assistant esta corriendo en el VPS"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["ssh", "-i", r"C:\Users\juanm\Documents\GitHub\contabo",
|
||||
"root@185.187.169.109",
|
||||
"docker ps | grep homeassistant | grep -q Up && echo OK || echo NOK"],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
text=True
|
||||
)
|
||||
return "OK" in result.stdout
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def mostrar_aviso():
|
||||
"""Muestra message box y emite aviso sonoro"""
|
||||
# Message box
|
||||
ps_messagebox = """
|
||||
Add-Type -AssemblyName System.Windows.Forms
|
||||
[System.Windows.Forms.MessageBox]::Show(
|
||||
"Home Assistant esta listo en http://185.187.169.109:8123`n`nPasos siguientes:`n1. Crear cuenta admin`n2. Instalar HACS`n3. Instalar Alexa Media Player`n4. Generar API Token`n`nVer: INFRAESTRUCTURA/docs/home-assistant-setup-alexa.md",
|
||||
"Home Assistant Listo",
|
||||
[System.Windows.Forms.MessageBoxButtons]::OK,
|
||||
[System.Windows.Forms.MessageBoxIcon]::Information
|
||||
)
|
||||
"""
|
||||
subprocess.run(["powershell", "-STA", "-Command", ps_messagebox])
|
||||
|
||||
# Aviso sonoro
|
||||
try:
|
||||
subprocess.run([
|
||||
"python",
|
||||
r"C:\Users\juanm\Documents\GitHub\biblioteca_negocio_prolongo\scripts\aviso_sonoro.py",
|
||||
"ok"
|
||||
])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def main():
|
||||
print("=" * 70)
|
||||
print(" MONITOR HOME ASSISTANT - Verificando cada 30 segundos")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("Esperando a que Home Assistant este listo...")
|
||||
print("URL: http://185.187.169.109:8123")
|
||||
print()
|
||||
|
||||
intentos = 0
|
||||
max_intentos = 60 # 30 minutos max
|
||||
|
||||
while intentos < max_intentos:
|
||||
if check_homeassistant():
|
||||
print()
|
||||
print("=" * 70)
|
||||
print(" HOME ASSISTANT LISTO!")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("Accede a: http://185.187.169.109:8123")
|
||||
print("Documentacion: INFRAESTRUCTURA/docs/home-assistant-setup-alexa.md")
|
||||
print()
|
||||
mostrar_aviso()
|
||||
return
|
||||
|
||||
intentos += 1
|
||||
tiempo_restante = (max_intentos - intentos) * 30 // 60
|
||||
print(f"[{intentos}/{max_intentos}] Aun no esta listo... (max {tiempo_restante} min)")
|
||||
time.sleep(30)
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print(" TIMEOUT - Home Assistant no arranque en 30 minutos")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("Verificar manualmente:")
|
||||
print(" ssh -i C:\\Users\\juanm\\Documents\\GitHub\\contabo root@185.187.169.109")
|
||||
print(" cd /opt/homeassistant && docker compose logs -f")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user