TheDocumentation 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.
Client type manages the TCP connection to an ESPHome device, handles the API handshake, dispatches incoming messages to registered handlers, and runs background goroutines for keepalive and reconnection.
Connecting
Dial
Connects to the specified address usingcontext.Background(). A convenience wrapper around DialWithContext.
Device address in
host:port format. The default ESPHome API port is 6053.Timeout for the TCP dial and API handshake. Does not limit the connection lifetime.
(*Client, error) — a connected client, or an error if the dial or handshake fails.
DialWithContext
Connects to the specified address with a parent context. When the context is cancelled, all background goroutines (read loop, keepalive, reconnect) exit cleanly.Parent context. Cancelling it stops all background goroutines.
Device address in
host:port format.Timeout for the TCP dial and API handshake.
(*Client, error).
Lifecycle
Close
Cancels the internal context, stops all background goroutines, and closes the underlying transport. Safe to call multiple times.Disconnect
Sends aDisconnectRequest to the device, waits up to 2 seconds for the DisconnectResponse, then calls Close. Also disables automatic reconnection so the connection does not come back up after the graceful shutdown.
Connected
Reports whether the client currently has an active connection. The value is updated atomically by the read loop.Done
Returns a channel that is closed when the read loop exits. Use this to block until the connection drops.Device metadata
These methods return information populated during the API handshake. They are available immediately after a successfulDial call.
Name
Returns the device name reported in theHelloResponse.
ServerInfo
Returns the server info string reported in theHelloResponse (typically esphome <version>).
APIVersion
Returns the major and minor API version negotiated during the handshake.Device operations
DeviceInfo
Fetches full device information from the device. Sends aDeviceInfoRequest and waits up to 5 seconds for the response.
*pb.DeviceInfoResponse with fields including Name, MacAddress, EsphomeVersion, Model, ProjectName, ProjectVersion, and CompilationTime.
ListEntities
Sends aListEntitiesRequest and collects all entity response messages until the device sends ListEntitiesDoneResponse. Entity definitions are also stored in the client’s EntityRegistry. Waits up to 10 seconds.
ListEntitiesWithTimeout
LikeListEntities but with a configurable timeout.
Maximum time to wait for
ListEntitiesDoneResponse.SubscribeStates
Sends aSubscribeStatesRequest and registers handlers for all state response message types. The handler is called for every incoming state message. State updates are also applied to the EntityRegistry automatically.
The handler is saved internally so it can be re-registered transparently after an automatic reconnect.
Called for every incoming state response. Type-assert the message to access domain-specific fields.
Entities
Returns the client’sEntityRegistry. The registry is populated by ListEntities and updated by SubscribeStates.
Ping
Ping
Sends aPingRequest and waits up to 5 seconds for a PingResponse.
PingWithTimeout
LikePing with a configurable timeout.
Low-level API
These methods are intended for advanced use cases such as implementing custom message handlers or sending protocol messages not covered by the higher-level API.On
Registers a handler for a specific message type ID. Returns a function that removes the handler when called.The ESPHome protocol message type ID to listen for.
Called with the decoded
proto.Message each time a matching message arrives.SendMessage
Encodes and sends a protobuf message with the given type ID. Safe to call from multiple goroutines.The protobuf message to send.
The ESPHome protocol message type ID for this message.
Services
ESPHome devices can expose custom services defined in the device YAML. These are discovered alongside entities viaListEntities and stored in the ServiceRegistry.
Services
Returns the client’sServiceRegistry, which caches custom service definitions discovered via ListEntities.
ExecuteService
Sends anExecuteServiceRequest to invoke a custom ESPHome service by its numeric key.
The service key from
ServiceDefinition.Key or the ListEntitiesServicesResponse.Argument values matching the declared argument schema of the service. Pass an empty slice for services with no arguments.
ExecuteServiceByName
Looks up a service by name in the registry and executes it. Returns an error if no service with that name has been discovered.The service name as declared in the ESPHome YAML (e.g.
"play_rtttl").Argument values matching the service’s declared argument schema.
Call
ListEntities before ExecuteServiceByName to populate the service registry. Services are automatically cleared and re-discovered on reconnect.