diff --git a/example/example_live_test.go b/example/example_live_test.go index b6b25ec..159e44c 100644 --- a/example/example_live_test.go +++ b/example/example_live_test.go @@ -37,8 +37,10 @@ type ( func setupLogging() { opts := &devslog.Options{ HandlerOptions: &slog.HandlerOptions{ - Level: slog.LevelDebug, + Level: slog.LevelDebug, + AddSource: true, }, + NewLineAfterLog: true, } slog.SetDefault(slog.New(devslog.NewHandler(os.Stdout, opts))) } @@ -61,7 +63,7 @@ func (s *MySuite) SetupSuite() { } s.app, err = ga.NewApp(ga.NewAppRequest{ - // HAAuthToken: s.config.Hass.HAAuthToken, + HAAuthToken: s.config.Hass.HAAuthToken, IpAddress: s.config.Hass.IpAddress, HomeZoneEntityId: s.config.Hass.HomeZoneEntityId, }) @@ -70,13 +72,21 @@ func (s *MySuite) SetupSuite() { s.T().FailNow() } + // Register all automations entityId := s.config.Entities.LightEntityId if entityId != "" { s.suiteCtx["entityCallbackInvoked"] = false etl := ga.NewEntityListener().EntityIds(entityId).Call(s.entityCallback).Build() s.app.RegisterEntityListeners(etl) - go s.app.Start() } + + s.suiteCtx["dailyScheduleCallbackInvoked"] = false + runTime := time.Now().Add(1 * time.Minute).Format("15:04") + dailySchedule := ga.NewDailySchedule().Call(s.dailyScheduleCallback).At(runTime).Build() + s.app.RegisterSchedules(dailySchedule) + + // start GA app + go s.app.Start() } func (s *MySuite) TearDownSuite() { @@ -104,12 +114,25 @@ func (s *MySuite) TestLightService() { } } -// Test if event has been captured after light entity state changed +// Basic test of daily schedule and callback +func (s *MySuite) TestSchedule() { + assert.EventuallyWithT(s.T(), func(c *assert.CollectT) { + assert.True(c, s.suiteCtx["dailyScheduleCallbackInvoked"].(bool)) + }, 2*time.Minute, 1*time.Second, "Daily schedule callback was not invoked") +} + +// Capture event after light entity state has changed func (s *MySuite) entityCallback(se *ga.Service, st ga.State, e ga.EntityData) { slog.Info("Entity callback called.", "entity id", e.TriggerEntityId, "from state", e.FromState, "to state", e.ToState) s.suiteCtx["entityCallbackInvoked"] = true } +// Capture planned daily schedule +func (s *MySuite) dailyScheduleCallback(se *ga.Service, st ga.State) { + slog.Info("Daily schedule callback called.") + s.suiteCtx["dailyScheduleCallbackInvoked"] = true +} + func getEntityState(s *MySuite, entityId string) string { state, err := s.app.GetState().Get(entityId) if err != nil { diff --git a/go.mod b/go.mod index e4a89fe..4c9b4d1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module saml.dev/gome-assistant -go 1.19 +go 1.21 require ( github.com/golang-module/carbon v1.7.1 diff --git a/internal/internal.go b/internal/internal.go index f8c752d..7538e62 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -2,7 +2,7 @@ package internal import ( "fmt" - "log" + "log/slog" "reflect" "runtime" "time" @@ -27,7 +27,7 @@ func GetId() int64 { func ParseTime(s string) carbon.Carbon { t, err := time.Parse("15:04", s) if err != nil { - log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.\n", s) + slog.Error(fmt.Sprintf("Failed to parse time string \"%s\"; format must be HH:MM.\n", s)) } return carbon.Now().SetTimeMilli(t.Hour(), t.Minute(), 0, 0) } @@ -35,7 +35,7 @@ func ParseTime(s string) carbon.Carbon { func ParseDuration(s string) time.Duration { d, err := time.ParseDuration(s) if err != nil { - log.Fatalf(fmt.Sprintf("Couldn't parse string duration: \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units\n", s)) + slog.Error(fmt.Sprintf("Couldn't parse string duration: \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units\n", s)) } return d } diff --git a/schedule.go b/schedule.go index 2325890..a0f11d0 100644 --- a/schedule.go +++ b/schedule.go @@ -2,7 +2,7 @@ package gomeassistant import ( "fmt" - "log" + "log/slog" "time" "github.com/golang-module/carbon" @@ -168,7 +168,7 @@ func runSchedules(a *App) { sched = popSchedule(a) } - log.Println("Next schedule:", sched.nextRunTime) + slog.Info("Next schedule", "new run time", sched.nextRunTime) time.Sleep(time.Until(sched.nextRunTime)) sched.maybeRunCallback(a) requeueSchedule(a, sched)