https://github.com/mkelcik/go-ha-client
Go client for Home Assistant REST API
https://github.com/mkelcik/go-ha-client
golang home-assistant rest-api
Last synced: 4 months ago
JSON representation
Go client for Home Assistant REST API
- Host: GitHub
- URL: https://github.com/mkelcik/go-ha-client
- Owner: mkelcik
- License: mit
- Created: 2021-08-09T18:16:42.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2026-02-02T21:56:13.000Z (5 months ago)
- Last Synced: 2026-02-03T11:08:52.943Z (5 months ago)
- Topics: golang, home-assistant, rest-api
- Language: Go
- Homepage:
- Size: 106 KB
- Stars: 18
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-ha-client
Go client for home-assistant REST API. Tested with home-assistant `core-2021.7.2`
### Basic usage
Change `Token` and `Host` to your actual home-assistant bearer token and address
Check home-assistant documentation how get access token.
```go
package main
import (
"context"
"fmt"
ha "github.com/mkelcik/go-ha-client"
"net/http"
"time"
)
func main() {
client := ha.NewClient(ha.ClientConfig{Token: "mytoken", Host: "http://my-ha.home"}, &http.Client{
Timeout: 30 * time.Second,
})
// ping instance
if err := client.Ping(context.Background()); err != nil {
fmt.Println("connection error", err)
} else {
fmt.Println("connection ok")
}
// example of home-assistant instance info
discoverInfo, err := client.GetDiscoverInfo(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%+v", discoverInfo)
}
```
### Examples
To turn light with entity id `light.light_1` on, we can use `NewTurnLightOnCmd` helper, to create command and call service.
```go
// turn light on
if _, err := client.CallService(context.Background(), ha.NewTurnLightOnCmd("light.light_1")); err != nil {
panic(err)
}
// turn light off
if _, err := client.CallService(context.Background(), ha.NewTurnLightOffCmd("light.light_1")); err != nil {
panic(err)
}
```
or turn `switch.switch_1` off without helper
```go
if _, err := client.CallService(context.Background(), DefaultServiceCmd{
Service: "turn_off",
Domain: "switch",
EntityId: "switch.switch_1",
}); err != nil {
panic(err)
}
```
Take and save picture from camera
```go
camImg, err := client.GetCameraJpeg(context.Background(), "camera.my_camera")
if err != nil {
panic(err)
}
f, err := os.Create("camera.jpg")
if err != nil {
panic(err)
}
defer f.Close()
if err := jpeg.Encode(f, camImg, nil); err != nil {
panic(err)
}
```