20-sep: Home Assistant en VPS para control luces Alexa (gratis)
This commit is contained in:
75
scripts/test_homeassistant.py
Normal file
75
scripts/test_homeassistant.py
Normal file
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
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 = r"C:\Users\juanm\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()
|
||||
Reference in New Issue
Block a user