#!/command/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Home Assistant Add-on: ESPHome
# Sends discovery information to Home Assistant.
# ==============================================================================
declare port

# We only disable it when disabled explicitly
if bashio::config.false 'home_assistant_dashboard_integration';
then
    bashio::log.info "Home Assistant discovery is disabled for this add-on."
    bashio::exit.ok
fi

port=$(bashio::addon.ingress_port)

# Wait for the ESPHome Device Builder to become available
bashio::net.wait_for "${port}" "127.0.0.1" 300

# Send one discovery message; the config is a JSON string built with bashio::var.json.
send_discovery() {
    local service=$1
    local config=$2
    if bashio::discovery "${service}" "${config}" > /dev/null; then
        bashio::log.info "Successfully send ${service} discovery information to Home Assistant."
    else
        bashio::log.error "${service} discovery message to Home Assistant failed!"
    fi
}

send_discovery "esphome" "$(bashio::var.json host "127.0.0.1" port "^${port}")"
# The Device Builder MCP server, consumed by Home Assistant's mcp integration.
send_discovery "mcp" "$(bashio::var.json url "http://127.0.0.1:${port}/api/mcp")"
