From 25755255286bc3c0d5384562333f5fee01ca6a40 Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Fri, 4 Nov 2022 21:10:14 -0400 Subject: [PATCH] make App exported --- README.md | 2 +- app.go | 20 ++++++++++---------- entitylistener.go | 2 +- eventListener.go | 2 +- example/example.go | 2 +- go.mod | 10 +++++----- go.sum | 25 +++++++++++++++++-------- schedule.go | 12 ++++++------ 8 files changed, 42 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index aa86a7e..3a64397 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ The general flow is import ga "github.com/saml-dev/gome-assistant" // replace with IP and port of your Home Assistant installation -app := ga.App("0.0.0.0:8123") +app := ga.NewApp("0.0.0.0:8123") // create automations here (see next sections) diff --git a/app.go b/app.go index f0450ba..b3c30fa 100644 --- a/app.go +++ b/app.go @@ -14,7 +14,7 @@ import ( ws "github.com/saml-dev/gome-assistant/internal/websocket" ) -type app struct { +type App struct { ctx context.Context ctxCancel context.CancelFunc conn *websocket.Conn @@ -41,10 +41,10 @@ type timeRange struct { } /* -App establishes the websocket connection and returns an object +NewApp establishes the websocket connection and returns an object you can use to register schedules and listeners. */ -func App(connString string) *app { +func NewApp(connString string) *App { token := os.Getenv("HA_AUTH_TOKEN") conn, ctx, ctxCancel := ws.SetupConnection(connString, token) @@ -53,7 +53,7 @@ func App(connString string) *app { service := newService(conn, ctx, httpClient) state := newState(httpClient) - return &app{ + return &App{ conn: conn, ctx: ctx, ctxCancel: ctxCancel, @@ -66,13 +66,13 @@ func App(connString string) *app { } } -func (a *app) Cleanup() { +func (a *App) Cleanup() { if a.ctxCancel != nil { a.ctxCancel() } } -func (a *app) RegisterSchedules(schedules ...Schedule) { +func (a *App) RegisterSchedules(schedules ...Schedule) { for _, s := range schedules { // realStartTime already set for sunset/sunrise if s.isSunrise || s.isSunset { @@ -101,7 +101,7 @@ func (a *app) RegisterSchedules(schedules ...Schedule) { } } -func (a *app) RegisterEntityListeners(etls ...EntityListener) { +func (a *App) RegisterEntityListeners(etls ...EntityListener) { for _, etl := range etls { if etl.delay != 0 && etl.toState == "" { panic("EntityListener error: you have to use ToState() when using Duration()") @@ -117,7 +117,7 @@ func (a *app) RegisterEntityListeners(etls ...EntityListener) { } } -func (a *app) RegisterEventListeners(evls ...EventListener) { +func (a *App) RegisterEventListeners(evls ...EventListener) { for _, evl := range evls { for _, eventType := range evl.eventTypes { if elList, ok := a.eventListeners[eventType]; ok { @@ -130,7 +130,7 @@ func (a *app) RegisterEventListeners(evls ...EventListener) { } } -func getSunriseSunset(a *app, sunrise bool, offset []DurationString) carbon.Carbon { +func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carbon { printString := "Sunset" attrKey := "next_setting" if sunrise { @@ -163,7 +163,7 @@ func getSunriseSunset(a *app, sunrise bool, offset []DurationString) carbon.Carb return nextSetOrRise } -func (a *app) Start() { +func (a *App) Start() { // schedules go runSchedules(a) diff --git a/entitylistener.go b/entitylistener.go index 61b4e24..132a8e8 100644 --- a/entitylistener.go +++ b/entitylistener.go @@ -146,7 +146,7 @@ func (b elBuilder3) Build() EntityListener { } /* Functions */ -func callEntityListeners(app *app, msgBytes []byte) { +func callEntityListeners(app *App, msgBytes []byte) { msg := stateChangedMsg{} json.Unmarshal(msgBytes, &msg) data := msg.Event.Data diff --git a/eventListener.go b/eventListener.go index 2317e4f..b0e6997 100644 --- a/eventListener.go +++ b/eventListener.go @@ -102,7 +102,7 @@ type BaseEventMsg struct { } /* Functions */ -func callEventListeners(app *app, msg ws.ChanMsg) { +func callEventListeners(app *App, msg ws.ChanMsg) { baseEventMsg := BaseEventMsg{} json.Unmarshal(msg.Raw, &baseEventMsg) listeners, ok := app.eventListeners[baseEventMsg.Event.EventType] diff --git a/example/example.go b/example/example.go index 791c4dc..af0dc74 100644 --- a/example/example.go +++ b/example/example.go @@ -9,7 +9,7 @@ import ( ) func main() { - app := ga.App("0.0.0.0:8123") // Replace with your Home Assistant IP Address + app := ga.NewApp("0.0.0.0:8123") // Replace with your Home Assistant IP Address defer app.Cleanup() pantryDoor := ga. diff --git a/go.mod b/go.mod index e510c14..6f10cec 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,14 @@ module github.com/saml-dev/gome-assistant go 1.19 require ( - github.com/golang-module/carbon v1.6.9 + github.com/golang-module/carbon v1.7.1 github.com/gorilla/websocket v1.5.0 ) require ( - github.com/gobuffalo/envy v1.7.0 // indirect - github.com/gobuffalo/packd v0.3.0 // indirect + github.com/gobuffalo/envy v1.10.2 // indirect + github.com/gobuffalo/packd v1.0.2 // indirect github.com/gobuffalo/packr v1.30.1 // indirect - github.com/joho/godotenv v1.3.0 // indirect - github.com/rogpeppe/go-internal v1.3.0 // indirect + github.com/joho/godotenv v1.4.0 // indirect + github.com/rogpeppe/go-internal v1.9.0 // indirect ) diff --git a/go.sum b/go.sum index f85c10e..ee8c362 100644 --- a/go.sum +++ b/go.sum @@ -8,22 +8,25 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/gobuffalo/envy v1.7.0 h1:GlXgaiBkmrYMHco6t4j7SacKO4XUjvh5pwXh0f4uxXU= github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= +github.com/gobuffalo/envy v1.10.2 h1:EIi03p9c3yeuRCFPOKcSfajzkLb3hrRjEpHGI8I2Wo4= +github.com/gobuffalo/envy v1.10.2/go.mod h1:qGAGwdvDsaEtPhfBzb3o0SfDea8ByGn9j8bKmVft9z8= github.com/gobuffalo/logger v1.0.0/go.mod h1:2zbswyIUa45I+c+FLXuWl9zSWEiVuthsk8ze5s8JvPs= -github.com/gobuffalo/packd v0.3.0 h1:eMwymTkA1uXsqxS0Tpoop3Lc0u3kTfiMBE6nKtQU4g4= github.com/gobuffalo/packd v0.3.0/go.mod h1:zC7QkmNkYVGKPw4tHpBQ+ml7W/3tIebgeo1b36chA3Q= +github.com/gobuffalo/packd v1.0.2 h1:Yg523YqnOxGIWCp69W12yYBKsoChwI7mtu6ceM9Bwfw= +github.com/gobuffalo/packd v1.0.2/go.mod h1:sUc61tDqGMXON80zpKGp92lDb86Km28jfvX7IAyxFT8= github.com/gobuffalo/packr v1.30.1 h1:hu1fuVR3fXEZR7rXNW3h8rqSML8EVAf6KNm0NKO/wKg= github.com/gobuffalo/packr v1.30.1/go.mod h1:ljMyFO2EcrnzsHsN99cvbq055Y9OhRrIaviy289eRuk= github.com/gobuffalo/packr/v2 v2.5.1/go.mod h1:8f9c96ITobJlPzI44jj+4tHnEKNt0xXWSVlXRN9X1Iw= -github.com/golang-module/carbon v1.6.9 h1:fobotpw4zUvU1ZPXLOe6qn5l5zSbiKeJNJSIBeUHgJo= -github.com/golang-module/carbon v1.6.9/go.mod h1:M/TDTYPp3qWtW68u49dLDJOyGmls6L6BXdo/pyvkMaU= +github.com/golang-module/carbon v1.7.1 h1:EDPV0YjxeS2kE2cRedfGgDikU6l5D79HB/teHuZDLu8= +github.com/golang-module/carbon v1.7.1/go.mod h1:M/TDTYPp3qWtW68u49dLDJOyGmls6L6BXdo/pyvkMaU= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/karrick/godirwalk v1.10.12/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -34,11 +37,13 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= @@ -49,10 +54,13 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -72,5 +80,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 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= diff --git a/schedule.go b/schedule.go index 13a4c1a..de8103e 100644 --- a/schedule.go +++ b/schedule.go @@ -101,7 +101,7 @@ func (sb scheduleBuilderDaily) At(s string) scheduleBuilderEnd { // Sunrise takes an app pointer and an optional duration string that is passed to time.ParseDuration. // Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration // for full list. -func (sb scheduleBuilderDaily) Sunrise(a *app, offset ...DurationString) scheduleBuilderEnd { +func (sb scheduleBuilderDaily) Sunrise(a *App, offset ...DurationString) scheduleBuilderEnd { sb.schedule.realStartTime = getSunriseSunset(a, true, offset).Carbon2Time() sb.schedule.isSunrise = true return scheduleBuilderEnd(sb) @@ -110,7 +110,7 @@ func (sb scheduleBuilderDaily) Sunrise(a *app, offset ...DurationString) schedul // Sunset takes an app pointer and an optional duration string that is passed to time.ParseDuration. // Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration // for full list. -func (sb scheduleBuilderDaily) Sunset(a *app, offset ...DurationString) scheduleBuilderEnd { +func (sb scheduleBuilderDaily) Sunset(a *App, offset ...DurationString) scheduleBuilderEnd { sb.schedule.realStartTime = getSunriseSunset(a, false, offset).Carbon2Time() sb.schedule.isSunset = true return scheduleBuilderEnd(sb) @@ -161,7 +161,7 @@ func getFunctionName(i interface{}) string { } // app.Start() functions -func runSchedules(a *app) { +func runSchedules(a *App) { if a.schedules.Len() == 0 { return } @@ -184,7 +184,7 @@ func runSchedules(a *app) { } } -func maybeRunCallback(a *app, s Schedule) { +func maybeRunCallback(a *App, s Schedule) { if c := checkExceptionDays(s.exceptionDays); c.fail { return } @@ -194,12 +194,12 @@ func maybeRunCallback(a *app, s Schedule) { go s.callback(a.service, a.state) } -func popSchedule(a *app) Schedule { +func popSchedule(a *App) Schedule { _sched, _ := a.schedules.Pop() return _sched.(Schedule) } -func requeueSchedule(a *app, s Schedule) { +func requeueSchedule(a *App, s Schedule) { if s.isSunrise || s.isSunset { nextSunTime := getSunriseSunset(a, s.isSunrise, []DurationString{s.sunOffset})