Install
go get github.com/taumatix/homeassistant-go
The module root is package ha, with the types the others share. api talks to Home Assistant over REST and WebSocket, mqtt over an MQTT broker, and domains/<domain> holds one package per entity domain. switch and select are Go keywords, so those two packages are domains/switches and domains/selects.
Get an access token
The HTTP client authenticates with a long-lived access token, which you create on your Home Assistant profile page. Home Assistant describes this in its authentication API documentation.
Send a first command
client := api.NewClient(api.WithBaseURL(baseURL), api.WithAuthToken(token))
defer func() { _ = client.Close() }() // closes the WebSocket, if one was opened
kitchen := light.Get(client, "kitchen") // the "light." prefix is optional
err := kitchen.TurnOn(ctx, light.BrightnessPct(80), light.Transition(2))
BrightnessPct and Transition are service options: optional fields of the service call. Each domain page lists its own.
Read a typed state
thermostat := climate.Get(transport, "living_room")
state, err := thermostat.State(ctx)
if err != nil {
return "", err
}
mode := climate.HVACModeOf(state) // "unavailable" and "unknown" are HVACModeUnknown
summary := string(mode)
// Attributes Home Assistant may leave out are pointers.
if current := state.Attributes.CurrentTemperature; current != nil {
summary = fmt.Sprintf("%s at %.1f", mode, *current)
}
Every attribute of a thermostat is a field of climate.Attributes, under the name Home Assistant publishes it with.
Next
- Control entities over HTTP or MQTT.
- Serve your own devices, which needs an MQTT broker connected to Home Assistant's MQTT integration.