sync_to_alexa.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. import json
  3. import urllib.request
  4. TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhNTk3MGM3YTQxNTE0YjZhYjg1OTNjZjJjMWY2NGQ0MyIsImlhdCI6MTc2NzI1OTE1MCwiZXhwIjoyMDgyNjE5MTUwfQ.hjng9QODqQsGLly3OXCKp7f5CPM6XcpgRLztVnPynTU"
  5. HA_URL = "http://localhost:8123"
  6. # Read entity registry
  7. with open('/config/.storage/core.entity_registry', 'r') as f:
  8. registry = json.load(f)
  9. # Find entities exposed to conversation
  10. exposed = []
  11. for entity in registry['data']['entities']:
  12. if entity.get('options', {}).get('conversation', {}).get('should_expose'):
  13. exposed.append(entity['entity_id'])
  14. # Expose each to Alexa
  15. for entity_id in exposed:
  16. data = json.dumps({
  17. "entity_id": entity_id,
  18. "assistants": ["alexa"]
  19. }).encode('utf-8')
  20. req = urllib.request.Request(
  21. f"{HA_URL}/api/services/homeassistant/expose_entity",
  22. data=data,
  23. headers={
  24. 'Authorization': f'Bearer {TOKEN}',
  25. 'Content-Type': 'application/json'
  26. },
  27. method='POST'
  28. )
  29. try:
  30. urllib.request.urlopen(req, timeout=5)
  31. print(f"Exposed: {entity_id}")
  32. except Exception as e:
  33. print(f"Failed: {entity_id} - {e}")
  34. print(f"Synced {len(exposed)} entities")