Option passed as variadic arguments to Dial or DialWithContext. They configure encryption, connection metadata, keepalive behaviour, automatic reconnection, and logging.
Connection
WithEncryptionKey
Enables Noise protocol encryption using a base64-encoded 32-byte pre-shared key. The key is theencryption.key value from the api: block of your ESPHome device YAML.
Base64-encoded 32-byte Noise PSK from the ESPHome device config.
WithExpectedName
Sets the device name to validate during the Noise handshake. If the connected device reports a different name, the handshake fails. If empty (the default), no name validation is performed.The expected device name from the
esphome.name field of the device YAML.WithClientInfo
Sets theclient_info string sent in the HelloRequest. Useful for identifying your application in ESPHome device logs.
Arbitrary string identifying the client application.
"esphome-apiclient-go"
Keepalive
WithKeepalive
Configures the interval at which the client sendsPingRequest messages to detect stale connections. Set to 0 to disable keepalive entirely.
Interval between keepalive pings. Set to
0 to disable.20 * time.Second
WithKeepaliveTimeout
Configures how long the client waits for aPingResponse before treating the connection as dead.
Maximum time to wait for a
PingResponse.10 * time.Second
Reconnection
WithReconnect
Enables automatic reconnection with the given base interval. When the connection drops unexpectedly, the client waits forinterval, then attempts to reconnect. If the attempt fails, the delay doubles on each retry (exponential backoff) up to a maximum of 5 minutes. Set to 0 to disable automatic reconnection.
After a successful reconnect the client automatically:
- Re-discovers entities with
ListEntities. - Re-subscribes to state updates if
SubscribeStateswas called before the drop. - Fires the
WithOnConnectcallback.
Base reconnect interval. The effective delay doubles on each failed attempt, up to 5 minutes.
Callbacks
WithOnConnect
Registers a callback that fires after the initial connection and after every successful reconnect.Called after connection (or reconnection) is established.
WithOnDisconnect
Registers a callback that fires when an unexpected disconnection is detected by the read loop. It does not fire whenClose or Disconnect is called intentionally.
Called on unexpected disconnection, before any reconnect attempt.
Logging
WithLogger
Attaches a custom*log.Logger used by the client to emit diagnostic messages for keepalive failures and reconnect attempts. Without this option, no internal messages are logged.
A standard library logger. The client writes keepalive and reconnect messages to it.