#!/bin/sh

# Read entity registry from storage
TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhNTk3MGM3YTQxNTE0YjZhYjg1OTNjZjJjMWY2NGQ0MyIsImlhdCI6MTc2NzI1OTE1MCwiZXhwIjoyMDgyNjE5MTUwfQ.hjng9QODqQsGLly3OXCKp7f5CPM6XcpgRLztVnPynTU"

# Parse entity registry and expose each entity
cat /homeassistant/.storage/core.entity_registry | \
  grep -B2 '"should_expose":true' | \
  grep '"entity_id"' | \
  cut -d'"' -f4 | \
while read entity; do
  if [ ! -z "$entity" ]; then
    wget -q -O- --post-data="{\"entity_id\":\"$entity\",\"assistants\":[\"alexa\"]}" \
      --header="Authorization: Bearer $TOKEN" \
      --header="Content-Type: application/json" \
      http://localhost:8123/api/services/homeassistant/expose_entity
    echo "Exposed: $entity"
  fi
done
