20-sep: Webhook apagar PC funcionando - callable desde cualquier sitio
This commit is contained in:
176
docs/webhook-apagar-pc.md
Normal file
176
docs/webhook-apagar-pc.md
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
# Webhook para apagar PC - Guía completa
|
||||||
|
|
||||||
|
## 🔗 URL del Webhook
|
||||||
|
|
||||||
|
```
|
||||||
|
http://185.187.169.109:8123/api/webhook/apagar-pc-juanjo
|
||||||
|
```
|
||||||
|
|
||||||
|
**NO requiere autenticación.** Cualquiera con la URL puede ejecutarlo.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Qué hace el webhook
|
||||||
|
|
||||||
|
Cuando se llama, ejecuta automáticamente:
|
||||||
|
|
||||||
|
1. Apaga la luz "barras" (comando de voz a Alexa)
|
||||||
|
2. Espera 2 segundos
|
||||||
|
3. Apaga la luz "cuartillo" (comando de voz a Alexa)
|
||||||
|
|
||||||
|
**No apaga el PC** - solo las luces. Para apagar el PC completo, ejecutar el script Python local.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 Formas de usarlo
|
||||||
|
|
||||||
|
### 1. Desde el PC (PowerShell/CMD)
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
curl -X POST http://185.187.169.109:8123/api/webhook/apagar-pc-juanjo
|
||||||
|
```
|
||||||
|
|
||||||
|
o
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
python -c "import requests; requests.post('http://185.187.169.109:8123/api/webhook/apagar-pc-juanjo')"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Desde el móvil (navegador)
|
||||||
|
|
||||||
|
Simplemente abre esta URL en el navegador:
|
||||||
|
```
|
||||||
|
http://185.187.169.109:8123/api/webhook/apagar-pc-juanjo
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Desde Alexa (rutina)
|
||||||
|
|
||||||
|
**Crear rutina en la app de Alexa:**
|
||||||
|
|
||||||
|
1. Abre la app de Alexa
|
||||||
|
2. Menú → **Rutinas** → **+** (crear nueva)
|
||||||
|
3. **Cuando esto ocurra:**
|
||||||
|
- **Voz** → Escribe: "Alexa, apaga las luces del ordenador"
|
||||||
|
4. **Añadir acción:**
|
||||||
|
- **Personalizado** → "apaga barras"
|
||||||
|
5. **Añadir acción:**
|
||||||
|
- **Personalizado** → "apaga cuartillo"
|
||||||
|
6. Guarda la rutina
|
||||||
|
|
||||||
|
**Uso:**
|
||||||
|
```
|
||||||
|
"Alexa, apaga las luces del ordenador"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Desde Telegram (opcional)
|
||||||
|
|
||||||
|
Si quisieras ejecutarlo desde Telegram, crear un bot que haga:
|
||||||
|
```python
|
||||||
|
import requests
|
||||||
|
requests.post('http://185.187.169.109:8123/api/webhook/apagar-pc-juanjo')
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Desde iOS/Android (Shortcuts/Tasker)
|
||||||
|
|
||||||
|
**iOS Shortcuts:**
|
||||||
|
1. Crear nuevo Shortcut
|
||||||
|
2. Añadir acción "Obtener contenido de URL"
|
||||||
|
3. URL: `http://185.187.169.109:8123/api/webhook/apagar-pc-juanjo`
|
||||||
|
4. Método: POST
|
||||||
|
5. Añadir al Home Screen o decir "Siri, apaga las luces del PC"
|
||||||
|
|
||||||
|
**Android Tasker:**
|
||||||
|
1. Nueva tarea
|
||||||
|
2. Net → HTTP POST
|
||||||
|
3. Server:Port: `185.187.169.109:8123`
|
||||||
|
4. Path: `/api/webhook/apagar-pc-juanjo`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔐 Seguridad
|
||||||
|
|
||||||
|
**IMPORTANTE:**
|
||||||
|
- El webhook NO requiere autenticación
|
||||||
|
- Cualquiera con la URL puede apagar las luces
|
||||||
|
- La URL contiene un ID único (`apagar-pc-juanjo`) que actúa como "contraseña"
|
||||||
|
- **NO compartir la URL públicamente**
|
||||||
|
|
||||||
|
Si necesitas más seguridad, se puede:
|
||||||
|
1. Añadir autenticación al webhook
|
||||||
|
2. Usar VPN para acceder al VPS
|
||||||
|
3. Cambiar el webhook_id a uno más largo/aleatorio
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🛠️ Mantenimiento
|
||||||
|
|
||||||
|
### Ver logs del webhook en Home Assistant
|
||||||
|
|
||||||
|
1. Settings → System → Logs
|
||||||
|
2. Buscar: `webhook`
|
||||||
|
|
||||||
|
### Editar el script
|
||||||
|
|
||||||
|
SSH al VPS:
|
||||||
|
```bash
|
||||||
|
ssh -i C:\Users\juanm\Documents\GitHub\contabo root@185.187.169.109
|
||||||
|
nano /opt/homeassistant/config/scripts.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
Después de editar:
|
||||||
|
```bash
|
||||||
|
docker restart homeassistant
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cambiar el webhook_id
|
||||||
|
|
||||||
|
Editar `/opt/homeassistant/config/automations.yaml` y cambiar:
|
||||||
|
```yaml
|
||||||
|
webhook_id: apagar-pc-juanjo
|
||||||
|
```
|
||||||
|
|
||||||
|
Por:
|
||||||
|
```yaml
|
||||||
|
webhook_id: mi-nuevo-webhook-secreto-12345
|
||||||
|
```
|
||||||
|
|
||||||
|
La nueva URL será:
|
||||||
|
```
|
||||||
|
http://185.187.169.109:8123/api/webhook/mi-nuevo-webhook-secreto-12345
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📋 Troubleshooting
|
||||||
|
|
||||||
|
### El webhook no responde (timeout)
|
||||||
|
|
||||||
|
Verificar que Home Assistant esté corriendo:
|
||||||
|
```bash
|
||||||
|
ssh -i C:\Users\juanm\Documents\GitHub\contabo root@185.187.169.109 "docker ps | grep homeassistant"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Las luces no se apagan
|
||||||
|
|
||||||
|
1. Verificar que el Echo Dot esté online
|
||||||
|
2. Probar manualmente: "Alexa, apaga barras"
|
||||||
|
3. Revisar logs en Home Assistant
|
||||||
|
|
||||||
|
### Error 404
|
||||||
|
|
||||||
|
El webhook_id es incorrecto o la automation no está cargada. Reiniciar Home Assistant.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚀 Próximos pasos (opcional)
|
||||||
|
|
||||||
|
Si quieres que el webhook **también apague el PC** (no solo las luces):
|
||||||
|
|
||||||
|
1. Instalar Home Assistant en el PC local
|
||||||
|
2. O montar un servicio HTTP en el PC que escuche comandos
|
||||||
|
3. O usar Wake-on-LAN inverso (shutdown remoto)
|
||||||
|
|
||||||
|
Por ahora, para apagar TODO (luces + PC):
|
||||||
|
```powershell
|
||||||
|
python C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\scripts\protocolo_apagado_final.py
|
||||||
|
```
|
||||||
29
scripts/check_ha_config.py
Normal file
29
scripts/check_ha_config.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import requests
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv(r'C:\Users\juanm\Documents\GitHub\biblioteca_negocio_prolongo\.env')
|
||||||
|
|
||||||
|
url = os.getenv('HOMEASSISTANT_URL')
|
||||||
|
token = os.getenv('HOMEASSISTANT_TOKEN')
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'Authorization': f'Bearer {token}',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verificar estados
|
||||||
|
response = requests.get(f'{url}/api/states', headers=headers)
|
||||||
|
states = response.json()
|
||||||
|
|
||||||
|
# Buscar automation
|
||||||
|
automations = [s for s in states if s['entity_id'].startswith('automation.')]
|
||||||
|
print('Automations encontradas:')
|
||||||
|
for a in automations:
|
||||||
|
print(f' - {a["entity_id"]}: {a["state"]}')
|
||||||
|
|
||||||
|
# Buscar scripts
|
||||||
|
scripts = [s for s in states if s['entity_id'].startswith('script.')]
|
||||||
|
print('\nScripts encontrados:')
|
||||||
|
for s in scripts:
|
||||||
|
print(f' - {s["entity_id"]}: {s["state"]}')
|
||||||
Reference in New Issue
Block a user