mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-11 16:07:24 -06:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c91c4f85c3 | |||
| 5698a30b37 |
36
README.md
36
README.md
@@ -1,20 +1,18 @@
|
|||||||
# Gome-Assistant
|
# go-ha
|
||||||
|
|
||||||
Write strongly typed [Home Assistant](https://www.home-assistant.io/) automations in Go!
|
Write strongly typed [Home Assistant](https://www.home-assistant.io/) automations in Go!
|
||||||
|
|
||||||
## Disclaimer
|
```bash
|
||||||
|
go get github.com/Xevion/go-ha
|
||||||
Gome-Assistant is a new library, and I'm opening it up early to get some user feedback on the API and help shape the direction. I plan for it to grow to cover all Home Assistant use cases, services, and event types. So it's possible that breaking changes will happen before v1.0.0!
|
|
||||||
|
|
||||||
## Quick Start
|
|
||||||
|
|
||||||
### Installation
|
|
||||||
|
|
||||||
```
|
|
||||||
go get github.com/Xevion/gome-assistant
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Generate Entity Constants
|
or in `go.mod`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
require github.com/Xevion/go-ha
|
||||||
|
```
|
||||||
|
|
||||||
|
## Generate Entity Constants
|
||||||
|
|
||||||
You can generate type-safe constants for all your Home Assistant entities using `go generate`. This makes it easier to reference entities in your code.
|
You can generate type-safe constants for all your Home Assistant entities using `go generate`. This makes it easier to reference entities in your code.
|
||||||
|
|
||||||
@@ -37,7 +35,7 @@ exclude_domains: ["device_tracker", "person"]
|
|||||||
2. Add a `//go:generate` comment in your project:
|
2. Add a `//go:generate` comment in your project:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//go:generate go run github.com/Xevion/gome-assistant/cmd/generate
|
//go:generate go run github.com/Xevion/go-ha/cmd/generate
|
||||||
```
|
```
|
||||||
|
|
||||||
Optionally use the `-config` flag to customize the file path of the config file.
|
Optionally use the `-config` flag to customize the file path of the config file.
|
||||||
@@ -72,11 +70,11 @@ Check out [`example/example.go`](./example/example.go) for an example of the 3 t
|
|||||||
|
|
||||||
### Run your code
|
### Run your code
|
||||||
|
|
||||||
Keeping with the simplicity that Go is famous for, you don't need a specific environment or docker container to run Gome-Assistant. You just write and run your code like any other Go binary. So once you build your code, you can run it however you like — using `screen` or `tmux`, a cron job, a linux service, or wrap it up in a docker container if you like!
|
Keeping with the simplicity that Go is famous for, you don't need a specific environment or docker container to run go-ha. You just write and run your code like any other Go binary. So once you build your code, you can run it however you like — using `screen` or `tmux`, a cron job, a linux service, or wrap it up in a docker container if you like!
|
||||||
|
|
||||||
> _❗ No promises, but I may provide a Docker image with file watching to automatically restart gome-assistant, to make it easier to use gome-assistant on a fully managed Home Assistant installation._
|
> _❗ No promises, but I may provide a Docker image with file watching to automatically restart go-ha, to make it easier to use go-ha on a fully managed Home Assistant installation._
|
||||||
|
|
||||||
## gome-assistant Concepts
|
## go-ha Concepts
|
||||||
|
|
||||||
### Overview
|
### Overview
|
||||||
|
|
||||||
@@ -87,7 +85,7 @@ The general flow is
|
|||||||
3. Start app
|
3. Start app
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import ga "github.com/Xevion/gome-assistant"
|
import ga "github.com/Xevion/go-ha"
|
||||||
|
|
||||||
// replace with IP and port of your Home Assistant installation
|
// replace with IP and port of your Home Assistant installation
|
||||||
app, err := ga.NewApp(ga.NewAppRequest{
|
app, err := ga.NewApp(ga.NewAppRequest{
|
||||||
@@ -107,7 +105,7 @@ app.RegisterIntervals(...)
|
|||||||
app.Start()
|
app.Start()
|
||||||
```
|
```
|
||||||
|
|
||||||
A full reference is available on [pkg.go.dev](https://pkg.go.dev/github.com/Xevion/gome-assistant), but all you need to know to get started are the four types of automations in gome-assistant.
|
A full reference is available on [pkg.go.dev](https://pkg.go.dev/github.com/Xevion/go-ha), but all you need to know to get started are the four types of automations in go-ha.
|
||||||
|
|
||||||
- [Daily Schedules](#daily-schedule)
|
- [Daily Schedules](#daily-schedule)
|
||||||
- [Entity Listeners](#entity-listener)
|
- [Entity Listeners](#entity-listener)
|
||||||
@@ -226,7 +224,7 @@ func myCallback(service *ga.Service, state ga.State, data ga.EventData) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
> 💡 Check `eventTypes.go` for pre-defined event types, or create your own struct type for custom events and contribute them back to gome-assistant with a PR.
|
> 💡 Check `eventTypes.go` for pre-defined event types, or create your own struct type for custom events and contribute them back to go-ha with a PR.
|
||||||
|
|
||||||
### Interval
|
### Interval
|
||||||
|
|
||||||
|
|||||||
24
app.go
24
app.go
@@ -14,10 +14,9 @@ import (
|
|||||||
sunriseLib "github.com/nathan-osman/go-sunrise"
|
sunriseLib "github.com/nathan-osman/go-sunrise"
|
||||||
|
|
||||||
"github.com/Workiva/go-datastructures/queue"
|
"github.com/Workiva/go-datastructures/queue"
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
internal "github.com/Xevion/go-ha/internal"
|
||||||
"github.com/Xevion/gome-assistant/internal/http"
|
"github.com/Xevion/go-ha/internal/parse"
|
||||||
"github.com/Xevion/gome-assistant/internal/parse"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrInvalidArgs = errors.New("invalid arguments provided")
|
var ErrInvalidArgs = errors.New("invalid arguments provided")
|
||||||
@@ -30,7 +29,7 @@ type App struct {
|
|||||||
// Wraps the ws connection with added mutex locking
|
// Wraps the ws connection with added mutex locking
|
||||||
wsWriter *ws.WebsocketWriter
|
wsWriter *ws.WebsocketWriter
|
||||||
|
|
||||||
httpClient *http.HttpClient
|
httpClient *internal.HttpClient
|
||||||
|
|
||||||
service *Service
|
service *Service
|
||||||
state *StateImpl
|
state *StateImpl
|
||||||
@@ -148,19 +147,8 @@ func NewApp(request NewAppRequest) (*App, error) {
|
|||||||
var err error
|
var err error
|
||||||
baseURL, err = url.Parse(request.URL)
|
baseURL, err = url.Parse(request.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrInvalidArgs
|
return nil, fmt.Errorf("failed to parse URL: %w", err)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// This is deprecated and will be removed in a future release
|
|
||||||
port := request.Port
|
|
||||||
if port == "" {
|
|
||||||
port = "8123"
|
|
||||||
}
|
|
||||||
baseURL.Scheme = "http"
|
|
||||||
if request.Secure {
|
|
||||||
baseURL.Scheme = "https"
|
|
||||||
}
|
|
||||||
baseURL.Host = request.IpAddress + ":" + port
|
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, ctx, ctxCancel, err := ws.ConnectionFromUri(baseURL, request.HAAuthToken)
|
conn, ctx, ctxCancel, err := ws.ConnectionFromUri(baseURL, request.HAAuthToken)
|
||||||
@@ -171,7 +159,7 @@ func NewApp(request NewAppRequest) (*App, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
httpClient := http.NewHttpClient(baseURL, request.HAAuthToken)
|
httpClient := internal.NewHttpClient(baseURL, request.HAAuthToken)
|
||||||
|
|
||||||
wsWriter := &ws.WebsocketWriter{Conn: conn}
|
wsWriter := &ws.WebsocketWriter{Conn: conn}
|
||||||
service := newService(wsWriter)
|
service := newService(wsWriter)
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package gomeassistant
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
"github.com/Xevion/gome-assistant/internal/parse"
|
"github.com/Xevion/go-ha/internal/parse"
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
ga "github.com/Xevion/gome-assistant"
|
ga "github.com/Xevion/go-ha"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
"github.com/Xevion/gome-assistant/internal/parse"
|
"github.com/Xevion/go-ha/internal/parse"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EntityListener struct {
|
type EntityListener struct {
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import (
|
|||||||
|
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
"github.com/Xevion/gome-assistant/internal/parse"
|
"github.com/Xevion/go-ha/internal/parse"
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EventListener struct {
|
type EventListener struct {
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import (
|
|||||||
|
|
||||||
// "example/entities" // Optional import generated entities
|
// "example/entities" // Optional import generated entities
|
||||||
|
|
||||||
ga "github.com/Xevion/gome-assistant"
|
ga "github.com/Xevion/go-ha"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate go run github.com/Xevion/gome-assistant/cmd/generate
|
//go:generate go run github.com/Xevion/go-ha/cmd/generate
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app, err := ga.NewApp(ga.NewAppRequest{
|
app, err := ga.NewApp(ga.NewAppRequest{
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
ga "github.com/Xevion/gome-assistant"
|
ga "github.com/Xevion/go-ha"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module github.com/Xevion/gome-assistant
|
module github.com/Xevion/go-ha
|
||||||
|
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ require (
|
|||||||
github.com/nathan-osman/go-sunrise v1.1.0
|
github.com/nathan-osman/go-sunrise v1.1.0
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.10.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
resty.dev/v3 v3.0.0-beta.3
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -19,4 +20,5 @@ require (
|
|||||||
github.com/joho/godotenv v1.4.0 // indirect
|
github.com/joho/godotenv v1.4.0 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.9.0 // indirect
|
github.com/rogpeppe/go-internal v1.9.0 // indirect
|
||||||
|
golang.org/x/net v0.33.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -84,6 +84,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
|
|||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||||
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
@@ -109,3 +111,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
resty.dev/v3 v3.0.0-beta.3 h1:3kEwzEgCnnS6Ob4Emlk94t+I/gClyoah7SnNi67lt+E=
|
||||||
|
resty.dev/v3 v3.0.0-beta.3/go.mod h1:OgkqiPvTDtOuV4MGZuUDhwOpkY8enjOsjjMzeOHefy4=
|
||||||
|
|||||||
74
internal/http.go
Normal file
74
internal/http.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
// http is used to interact with the home assistant
|
||||||
|
// REST API. Currently only used to retrieve state for
|
||||||
|
// a single entity_id
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"resty.dev/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HttpClient struct {
|
||||||
|
client *resty.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHttpClient(url *url.URL, token string) *HttpClient {
|
||||||
|
// Shallow copy the URL to avoid modifying the original
|
||||||
|
u := *url
|
||||||
|
u.Path = "/api"
|
||||||
|
if u.Scheme == "ws" {
|
||||||
|
u.Scheme = "http"
|
||||||
|
}
|
||||||
|
if u.Scheme == "wss" {
|
||||||
|
u.Scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create resty client with configuration
|
||||||
|
client := resty.New().
|
||||||
|
SetBaseURL(u.String()).
|
||||||
|
SetHeader("Authorization", "Bearer "+token).
|
||||||
|
SetTimeout(30 * time.Second).
|
||||||
|
SetRetryCount(3).
|
||||||
|
SetRetryWaitTime(1 * time.Second).
|
||||||
|
SetRetryMaxWaitTime(5 * time.Second).
|
||||||
|
AddRetryConditions(func(r *resty.Response, err error) bool {
|
||||||
|
return err != nil || r.StatusCode() >= 500
|
||||||
|
})
|
||||||
|
|
||||||
|
return &HttpClient{
|
||||||
|
client: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *HttpClient) GetState(entityId string) ([]byte, error) {
|
||||||
|
resp, err := c.client.R().
|
||||||
|
Get("/states/" + entityId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("Error making HTTP request: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode() >= 400 {
|
||||||
|
return nil, errors.New("HTTP error: " + resp.Status() + " - " + string(resp.Bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *HttpClient) States() ([]byte, error) {
|
||||||
|
resp, err := c.client.R().
|
||||||
|
Get("/states")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("Error making HTTP request: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode() >= 400 {
|
||||||
|
return nil, errors.New("HTTP error: " + resp.Status() + " - " + string(resp.Bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Bytes(), nil
|
||||||
|
}
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
// http is used to interact with the home assistant
|
|
||||||
// REST API. Currently only used to retrieve state for
|
|
||||||
// a single entity_id
|
|
||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HttpClient struct {
|
|
||||||
url string
|
|
||||||
token string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHttpClient(url *url.URL, token string) *HttpClient {
|
|
||||||
// Shallow copy the URL to avoid modifying the original
|
|
||||||
u := *url
|
|
||||||
u.Path = "/api"
|
|
||||||
if u.Scheme == "ws" {
|
|
||||||
u.Scheme = "http"
|
|
||||||
}
|
|
||||||
if u.Scheme == "wss" {
|
|
||||||
u.Scheme = "https"
|
|
||||||
}
|
|
||||||
|
|
||||||
return &HttpClient{
|
|
||||||
url: u.String(),
|
|
||||||
token: token,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *HttpClient) GetState(entityId string) ([]byte, error) {
|
|
||||||
resp, err := get(c.url+"/states/"+entityId, c.token)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *HttpClient) States() ([]byte, error) {
|
|
||||||
resp, err := get(c.url+"/states", c.token)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func get(url, token string) ([]byte, error) {
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("Error creating HTTP request: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("Authorization", "Bearer "+token)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("Error on response.\n[ERROR] -" + err.Error())
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("Error while reading the response bytes:" + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return body, nil
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
"github.com/Xevion/gome-assistant/types"
|
"github.com/Xevion/go-ha/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HomeAssistant struct {
|
type HomeAssistant struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
"github.com/Xevion/gome-assistant/types"
|
"github.com/Xevion/go-ha/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Notify struct {
|
type Notify struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Number struct {
|
type Number struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildService[
|
func BuildService[
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Structs */
|
/* Structs */
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrInvalidToken = errors.New("invalid authentication token")
|
var ErrInvalidToken = errors.New("invalid authentication token")
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
"github.com/Xevion/gome-assistant/internal/parse"
|
"github.com/Xevion/go-ha/internal/parse"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IntervalCallback func(*Service, State)
|
type IntervalCallback func(*Service, State)
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal"
|
"github.com/Xevion/go-ha/internal"
|
||||||
"github.com/Xevion/gome-assistant/internal/parse"
|
"github.com/Xevion/go-ha/internal/parse"
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package gomeassistant
|
package gomeassistant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xevion/gome-assistant/internal/services"
|
"github.com/Xevion/go-ha/internal/services"
|
||||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
ws "github.com/Xevion/go-ha/internal/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
|||||||
6
state.go
6
state.go
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
|
|
||||||
"github.com/Xevion/gome-assistant/internal/http"
|
internal "github.com/Xevion/go-ha/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type State interface {
|
type State interface {
|
||||||
@@ -23,7 +23,7 @@ type State interface {
|
|||||||
|
|
||||||
// State is used to retrieve state from Home Assistant.
|
// State is used to retrieve state from Home Assistant.
|
||||||
type StateImpl struct {
|
type StateImpl struct {
|
||||||
httpClient *http.HttpClient
|
httpClient *internal.HttpClient
|
||||||
latitude float64
|
latitude float64
|
||||||
longitude float64
|
longitude float64
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ type EntityState struct {
|
|||||||
LastChanged time.Time `json:"last_changed"`
|
LastChanged time.Time `json:"last_changed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func newState(c *http.HttpClient, homeZoneEntityId string) (*StateImpl, error) {
|
func newState(c *internal.HttpClient, homeZoneEntityId string) (*StateImpl, error) {
|
||||||
state := &StateImpl{httpClient: c}
|
state := &StateImpl{httpClient: c}
|
||||||
|
|
||||||
// Ensure the zone exists and has required attributes
|
// Ensure the zone exists and has required attributes
|
||||||
|
|||||||
Reference in New Issue
Block a user