5 Commits

46 changed files with 206 additions and 197 deletions

View File

View File

@@ -1,20 +1,18 @@
# Gome-Assistant
# go-ha
Write strongly typed [Home Assistant](https://www.home-assistant.io/) automations in Go!
## Disclaimer
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
```bash
go get github.com/Xevion/go-ha
```
### 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.
@@ -37,7 +35,7 @@ exclude_domains: ["device_tracker", "person"]
2. Add a `//go:generate` comment in your project:
```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.
@@ -72,11 +70,11 @@ Check out [`example/example.go`](./example/example.go) for an example of the 3 t
### 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
@@ -87,13 +85,13 @@ The general flow is
3. Start app
```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
app, err := ga.NewApp(ga.NewAppRequest{
URL: "http://192.168.1.123:8123",
HAAuthToken: os.Getenv("HA_AUTH_TOKEN"),
HomeZoneEntityId: "zone.home",
URL: "http://192.168.1.123:8123",
HAAuthToken: os.Getenv("HA_AUTH_TOKEN"),
HomeZoneEntityId: "zone.home",
})
// create automations here (see next sections)
@@ -107,7 +105,7 @@ app.RegisterIntervals(...)
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)
- [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

27
app.go
View File

@@ -14,9 +14,9 @@ import (
sunriseLib "github.com/nathan-osman/go-sunrise"
"github.com/Workiva/go-datastructures/queue"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/gome-assistant/internal/http"
ws "github.com/Xevion/gome-assistant/internal/websocket"
internal "github.com/Xevion/go-ha/internal"
"github.com/Xevion/go-ha/internal/parse"
ws "github.com/Xevion/go-ha/internal/websocket"
)
var ErrInvalidArgs = errors.New("invalid arguments provided")
@@ -29,7 +29,7 @@ type App struct {
// Wraps the ws connection with added mutex locking
wsWriter *ws.WebsocketWriter
httpClient *http.HttpClient
httpClient *internal.HttpClient
service *Service
state *StateImpl
@@ -147,19 +147,8 @@ func NewApp(request NewAppRequest) (*App, error) {
var err error
baseURL, err = url.Parse(request.URL)
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)
@@ -170,7 +159,7 @@ func NewApp(request NewAppRequest) (*App, error) {
return nil, err
}
httpClient := http.NewHttpClient(baseURL, request.HAAuthToken)
httpClient := internal.NewHttpClient(baseURL, request.HAAuthToken)
wsWriter := &ws.WebsocketWriter{Conn: conn}
service := newService(wsWriter)
@@ -273,7 +262,7 @@ func (a *App) RegisterIntervals(intervals ...Interval) {
panic(ErrInvalidArgs)
}
i.nextRunTime = internal.ParseTime(string(i.startTime)).Carbon2Time()
i.nextRunTime = parse.ParseTime(string(i.startTime)).Carbon2Time()
now := time.Now()
for i.nextRunTime.Before(now) {
i.nextRunTime = i.nextRunTime.Add(i.frequency)
@@ -369,7 +358,7 @@ func (a *App) Start() {
go runIntervals(a)
// subscribe to state_changed events
id := internal.GetId()
id := internal.NextId()
ws.SubscribeToStateChangedEvents(id, a.wsWriter, a.ctx)
a.entityListenersId = id

View File

@@ -3,7 +3,8 @@ package gomeassistant
import (
"time"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/go-ha/internal"
"github.com/Xevion/go-ha/internal/parse"
"github.com/golang-module/carbon"
)
@@ -16,8 +17,8 @@ func checkWithinTimeRange(startTime, endTime string) conditionCheck {
// if betweenStart and betweenEnd both set, first account for midnight
// overlap, then check if between those times.
if startTime != "" && endTime != "" {
parsedStart := internal.ParseTime(startTime)
parsedEnd := internal.ParseTime(endTime)
parsedStart := parse.ParseTime(startTime)
parsedEnd := parse.ParseTime(endTime)
// check for midnight overlap
if parsedEnd.Lt(parsedStart) { // example turn on night lights when motion from 23:00 to 07:00
@@ -34,9 +35,9 @@ func checkWithinTimeRange(startTime, endTime string) conditionCheck {
}
// otherwise just check individual before/after
} else if startTime != "" && internal.ParseTime(startTime).IsFuture() {
} else if startTime != "" && parse.ParseTime(startTime).IsFuture() {
cc.fail = true
} else if endTime != "" && internal.ParseTime(endTime).IsPast() {
} else if endTime != "" && parse.ParseTime(endTime).IsPast() {
cc.fail = true
}
return cc
@@ -168,7 +169,7 @@ func checkStartEndTime(s TimeString, isStart bool) conditionCheck {
}
now := time.Now()
parsedTime := internal.ParseTime(string(s)).Carbon2Time()
parsedTime := parse.ParseTime(string(s)).Carbon2Time()
if isStart {
if parsedTime.After(now) {
cc.fail = true

View File

@@ -4,7 +4,7 @@ import (
"errors"
"testing"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/go-ha/internal"
"github.com/stretchr/testify/assert"
)

View File

@@ -9,7 +9,7 @@ import (
"strings"
"text/template"
ga "github.com/Xevion/gome-assistant"
ga "github.com/Xevion/go-ha"
"gopkg.in/yaml.v3"
)

View File

@@ -7,7 +7,8 @@ import (
"github.com/golang-module/carbon"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/go-ha/internal"
"github.com/Xevion/go-ha/internal/parse"
)
type EntityListener struct {
@@ -127,13 +128,13 @@ func (b elBuilder3) ToState(s string) elBuilder3 {
}
func (b elBuilder3) Duration(s DurationString) elBuilder3 {
d := internal.ParseDuration(string(s))
d := parse.ParseDuration(string(s))
b.entityListener.delay = d
return b
}
func (b elBuilder3) Throttle(s DurationString) elBuilder3 {
d := internal.ParseDuration(string(s))
d := parse.ParseDuration(string(s))
b.entityListener.throttle = d
return b
}

View File

@@ -7,8 +7,9 @@ import (
"github.com/golang-module/carbon"
"github.com/Xevion/gome-assistant/internal"
ws "github.com/Xevion/gome-assistant/internal/websocket"
"github.com/Xevion/go-ha/internal"
"github.com/Xevion/go-ha/internal/parse"
ws "github.com/Xevion/go-ha/internal/websocket"
)
type EventListener struct {
@@ -80,7 +81,7 @@ func (b eventListenerBuilder3) OnlyBefore(end string) eventListenerBuilder3 {
}
func (b eventListenerBuilder3) Throttle(s DurationString) eventListenerBuilder3 {
d := internal.ParseDuration(string(s))
d := parse.ParseDuration(string(s))
b.eventListener.throttle = d
return b
}

View File

View File

@@ -8,10 +8,10 @@ import (
// "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() {
app, err := ga.NewApp(ga.NewAppRequest{

View File

@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v3"
ga "github.com/Xevion/gome-assistant"
ga "github.com/Xevion/go-ha"
)
type (

4
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/Xevion/gome-assistant
module github.com/Xevion/go-ha
go 1.21
@@ -9,6 +9,7 @@ require (
github.com/nathan-osman/go-sunrise v1.1.0
github.com/stretchr/testify v1.10.0
gopkg.in/yaml.v3 v3.0.1
resty.dev/v3 v3.0.0-beta.3
)
require (
@@ -19,4 +20,5 @@ require (
github.com/joho/godotenv v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
golang.org/x/net v0.33.0 // indirect
)

4
go.sum
View File

@@ -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-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.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-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
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
View 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
}

View File

@@ -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
}

28
internal/misc.go Normal file
View File

@@ -0,0 +1,28 @@
package internal
import (
"reflect"
"runtime"
"sync/atomic"
)
type EnabledDisabledInfo struct {
Entity string
State string
RunOnError bool
}
var (
id atomic.Int64 // default value is 0
)
// NextId returns a unique integer (for the given process), often used for providing a uniquely identifiable
// id for a request. This function is thread-safe.
func NextId() int64 {
return id.Add(1)
}
// GetFunctionName returns the name of the function that the interface is a pointer to.
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

View File

@@ -1,30 +1,13 @@
package internal
package parse
import (
"fmt"
"log/slog"
"reflect"
"runtime"
"sync/atomic"
"time"
"github.com/golang-module/carbon"
)
type EnabledDisabledInfo struct {
Entity string
State string
RunOnError bool
}
var (
id atomic.Int64 // default value is 0
)
func GetId() int64 {
return id.Add(1)
}
// Parses a HH:MM string.
func ParseTime(s string) carbon.Carbon {
t, err := time.Parse("15:04", s)
@@ -45,7 +28,3 @@ func ParseDuration(s string) time.Duration {
}
return d
}
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,8 +1,8 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
"github.com/Xevion/gome-assistant/types"
ws "github.com/Xevion/go-ha/internal/websocket"
"github.com/Xevion/go-ha/types"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,8 +1,8 @@
package services
import (
"github.com/Xevion/gome-assistant/internal"
ws "github.com/Xevion/gome-assistant/internal/websocket"
"github.com/Xevion/go-ha/internal"
ws "github.com/Xevion/go-ha/internal/websocket"
)
type Event struct {
@@ -23,7 +23,7 @@ type FireEventRequest struct {
// as `event_data`.
func (e Event) Fire(eventType string, eventData ...map[string]any) error {
req := FireEventRequest{
Id: internal.GetId(),
Id: internal.NextId(),
Type: "fire_event",
}

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
type HomeAssistant struct {

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"time"
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,8 +1,8 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
"github.com/Xevion/gome-assistant/types"
ws "github.com/Xevion/go-ha/internal/websocket"
"github.com/Xevion/go-ha/types"
)
type Notify struct {

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
type Number struct {

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,8 +1,8 @@
package services
import (
"github.com/Xevion/gome-assistant/internal"
ws "github.com/Xevion/gome-assistant/internal/websocket"
"github.com/Xevion/go-ha/internal"
ws "github.com/Xevion/go-ha/internal/websocket"
)
func BuildService[
@@ -45,13 +45,15 @@ type BaseServiceRequest struct {
}
func NewBaseServiceRequest(entityId string) BaseServiceRequest {
id := internal.GetId()
bsr := BaseServiceRequest{
id := internal.NextId()
request := BaseServiceRequest{
Id: id,
RequestType: "call_service",
}
if entityId != "" {
bsr.Target.EntityId = entityId
request.Target.EntityId = entityId
}
return bsr
return request
}

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -1,7 +1,7 @@
package services
import (
ws "github.com/Xevion/gome-assistant/internal/websocket"
ws "github.com/Xevion/go-ha/internal/websocket"
)
/* Structs */

View File

@@ -16,7 +16,7 @@ import (
"github.com/gorilla/websocket"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/go-ha/internal"
)
var ErrInvalidToken = errors.New("invalid authentication token")
@@ -140,7 +140,7 @@ func SubscribeToStateChangedEvents(id int64, conn *WebsocketWriter, ctx context.
func SubscribeToEventType(eventType string, conn *WebsocketWriter, ctx context.Context, id ...int64) {
var finalId int64
if len(id) == 0 {
finalId = internal.GetId()
finalId = internal.NextId()
} else {
finalId = id[0]
}

View File

@@ -5,7 +5,8 @@ import (
"log/slog"
"time"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/go-ha/internal"
"github.com/Xevion/go-ha/internal/parse"
)
type IntervalCallback func(*Service, State)
@@ -80,7 +81,7 @@ func (ib intervalBuilder) Call(callback IntervalCallback) intervalBuilderCall {
// Takes a DurationString ("2h", "5m", etc) to set the frequency of the interval.
func (ib intervalBuilderCall) Every(s DurationString) intervalBuilderEnd {
d := internal.ParseDuration(string(s))
d := parse.ParseDuration(string(s))
ib.interval.frequency = d
return intervalBuilderEnd(ib)
}

View File

@@ -5,7 +5,8 @@ import (
"log/slog"
"time"
"github.com/Xevion/gome-assistant/internal"
"github.com/Xevion/go-ha/internal"
"github.com/Xevion/go-ha/internal/parse"
"github.com/golang-module/carbon"
)
@@ -75,7 +76,7 @@ func (sb scheduleBuilder) Call(callback ScheduleCallback) scheduleBuilderCall {
// At takes a string in 24hr format time like "15:30".
func (sb scheduleBuilderCall) At(s string) scheduleBuilderEnd {
t := internal.ParseTime(s)
t := parse.ParseTime(s)
sb.schedule.hour = t.Hour()
sb.schedule.minute = t.Minute()
return scheduleBuilderEnd(sb)

View File

@@ -1,8 +1,8 @@
package gomeassistant
import (
"github.com/Xevion/gome-assistant/internal/services"
ws "github.com/Xevion/gome-assistant/internal/websocket"
"github.com/Xevion/go-ha/internal/services"
ws "github.com/Xevion/go-ha/internal/websocket"
)
type Service struct {

View File

@@ -8,7 +8,7 @@ import (
"github.com/golang-module/carbon"
"github.com/Xevion/gome-assistant/internal/http"
internal "github.com/Xevion/go-ha/internal"
)
type State interface {
@@ -23,7 +23,7 @@ type State interface {
// State is used to retrieve state from Home Assistant.
type StateImpl struct {
httpClient *http.HttpClient
httpClient *internal.HttpClient
latitude float64
longitude float64
}
@@ -35,7 +35,7 @@ type EntityState struct {
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}
// Ensure the zone exists and has required attributes