The ESPHome API client provides two connection functions:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/richard87/esphome-apiclient/llms.txt
Use this file to discover all available pages before exploring further.
Dial and DialWithContext. Both establish a TCP connection to a device, perform the API handshake, and start the read loop. DialWithContext accepts a parent context so the connection can be cancelled from outside the client.
Function signatures
address parameter uses the format hostname:port or IP:port.
The default ESPHome API port is 6053. You must include the port in the address string — there is no automatic fallback.
Connection flow
Dial the device
Call
Dial or DialWithContext with the device address and a connection timeout. The timeout applies only to the TCP dial and handshake, not to the lifetime of the connection.Optionally enable Noise encryption
If your device has
api.encryption.key set in its YAML, pass the base64-encoded pre-shared key using WithEncryptionKey. The value is the encryption.key field from the ESPHome device configuration.Read device metadata
After a successful connection, the API handshake populates device metadata available through client methods.
Using context for cancellation
DialWithContext ties the client’s background goroutines (read loop, keepalive, reconnect) to the provided context. When the context is cancelled, all goroutines exit cleanly.
Checking connection status
Connected() reports whether the client currently has an active connection. The value is an atomic bool updated by the read loop.
Done channel.
Additional options
| Option | Description |
|---|---|
WithEncryptionKey(key string) | Enable Noise PSK encryption with a base64-encoded key |
WithExpectedName(name string) | Validate device name during the Noise handshake |
WithClientInfo(info string) | Set the client_info field in the HelloRequest |
WithLogger(l *log.Logger) | Attach a custom logger for internal diagnostic messages |