76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
"""
|
|
Script de prueba para verificar control de luces Alexa via Home Assistant.
|
|
|
|
Uso:
|
|
python test_homeassistant.py [on|off]
|
|
|
|
Ejemplos:
|
|
python test_homeassistant.py off # Apagar ambas luces
|
|
python test_homeassistant.py on # Encender ambas luces
|
|
"""
|
|
import sys
|
|
import os
|
|
import requests
|
|
from dotenv import load_dotenv
|
|
|
|
# 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')
|
|
|
|
# Cargar .env
|
|
dotenv_path = os.path.expanduser("~/Documents/GitHub/biblioteca_negocio_prolongo/.env")
|
|
load_dotenv(dotenv_path)
|
|
|
|
def main():
|
|
url = os.getenv("HOMEASSISTANT_URL")
|
|
token = os.getenv("HOMEASSISTANT_TOKEN")
|
|
|
|
if not url or not token:
|
|
print("[ERROR] Falta HOMEASSISTANT_URL o HOMEASSISTANT_TOKEN en .env")
|
|
print()
|
|
print("Añade al .env:")
|
|
print(" HOMEASSISTANT_URL=http://185.187.169.109:8123")
|
|
print(" HOMEASSISTANT_TOKEN=<tu-token>")
|
|
return
|
|
|
|
# Determinar acción
|
|
action = "turn_off"
|
|
if len(sys.argv) > 1 and sys.argv[1].lower() == "on":
|
|
action = "turn_on"
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
luces = ["barras", "cuartillo"]
|
|
|
|
print("=" * 60)
|
|
print(f" TEST HOME ASSISTANT - {action.upper().replace('_', ' ')}")
|
|
print("=" * 60)
|
|
|
|
for luz in luces:
|
|
entity_id = f"light.{luz}"
|
|
try:
|
|
response = requests.post(
|
|
f"{url}/api/services/light/{action}",
|
|
headers=headers,
|
|
json={"entity_id": entity_id},
|
|
timeout=10
|
|
)
|
|
|
|
if response.status_code == 200:
|
|
print(f"[OK] {luz.capitalize()}: {action.replace('_', ' ')}")
|
|
else:
|
|
print(f"[ERROR] {luz.capitalize()}: HTTP {response.status_code}")
|
|
print(f" {response.text}")
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"[ERROR] {luz.capitalize()}: {e}")
|
|
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|