89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
"""
|
|
Test de comandos de voz a Alexa via Home Assistant.
|
|
|
|
Uso:
|
|
python test_alexa_voz.py "apaga barras"
|
|
python test_alexa_voz.py "apaga cuartillo"
|
|
python test_alexa_voz.py "enciende barras"
|
|
"""
|
|
import sys
|
|
import os
|
|
import requests
|
|
from dotenv import load_dotenv
|
|
|
|
# Fix encoding
|
|
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')
|
|
|
|
dotenv_path = os.path.expanduser("~/Documents/GitHub/biblioteca_negocio_prolongo/.env")
|
|
load_dotenv(dotenv_path)
|
|
|
|
def enviar_comando_voz(echo_entity, comando):
|
|
"""Envia un comando de voz a un dispositivo Echo"""
|
|
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")
|
|
return False
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
data = {
|
|
"entity_id": echo_entity,
|
|
"media_content_type": "custom",
|
|
"media_content_id": comando
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
f"{url}/api/services/media_player/play_media",
|
|
headers=headers,
|
|
json=data,
|
|
timeout=10
|
|
)
|
|
|
|
if response.status_code == 200:
|
|
print(f"[OK] Comando enviado: '{comando}' -> {echo_entity}")
|
|
return True
|
|
else:
|
|
print(f"[ERROR] HTTP {response.status_code}: {response.text}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"[ERROR] {e}")
|
|
return False
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Uso: python test_alexa_voz.py \"comando\"")
|
|
print()
|
|
print("Ejemplos:")
|
|
print(" python test_alexa_voz.py \"apaga barras\"")
|
|
print(" python test_alexa_voz.py \"apaga cuartillo\"")
|
|
print(" python test_alexa_voz.py \"enciende barras\"")
|
|
return
|
|
|
|
comando = sys.argv[1]
|
|
|
|
# Probamos con el Echo Dot principal
|
|
echo_entity = "media_player.echo_dot_de_nahid"
|
|
|
|
print("=" * 60)
|
|
print(f" ENVIANDO COMANDO DE VOZ A ALEXA")
|
|
print("=" * 60)
|
|
print(f"Comando: {comando}")
|
|
print(f"Echo: {echo_entity}")
|
|
print()
|
|
|
|
enviar_comando_voz(echo_entity, comando)
|
|
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|