Added daily schedule test. Migrated relevant files to slog.

This commit is contained in:
Jiri Luzny
2023-12-09 10:52:28 +01:00
parent a4fbbe5ac3
commit b3a38013c6
4 changed files with 33 additions and 10 deletions

View File

@@ -38,7 +38,9 @@ func setupLogging() {
opts := &devslog.Options{ opts := &devslog.Options{
HandlerOptions: &slog.HandlerOptions{ HandlerOptions: &slog.HandlerOptions{
Level: slog.LevelDebug, Level: slog.LevelDebug,
AddSource: true,
}, },
NewLineAfterLog: true,
} }
slog.SetDefault(slog.New(devslog.NewHandler(os.Stdout, opts))) slog.SetDefault(slog.New(devslog.NewHandler(os.Stdout, opts)))
} }
@@ -61,7 +63,7 @@ func (s *MySuite) SetupSuite() {
} }
s.app, err = ga.NewApp(ga.NewAppRequest{ s.app, err = ga.NewApp(ga.NewAppRequest{
// HAAuthToken: s.config.Hass.HAAuthToken, HAAuthToken: s.config.Hass.HAAuthToken,
IpAddress: s.config.Hass.IpAddress, IpAddress: s.config.Hass.IpAddress,
HomeZoneEntityId: s.config.Hass.HomeZoneEntityId, HomeZoneEntityId: s.config.Hass.HomeZoneEntityId,
}) })
@@ -70,13 +72,21 @@ func (s *MySuite) SetupSuite() {
s.T().FailNow() s.T().FailNow()
} }
// Register all automations
entityId := s.config.Entities.LightEntityId entityId := s.config.Entities.LightEntityId
if entityId != "" { if entityId != "" {
s.suiteCtx["entityCallbackInvoked"] = false s.suiteCtx["entityCallbackInvoked"] = false
etl := ga.NewEntityListener().EntityIds(entityId).Call(s.entityCallback).Build() etl := ga.NewEntityListener().EntityIds(entityId).Call(s.entityCallback).Build()
s.app.RegisterEntityListeners(etl) 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() { 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) { 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) slog.Info("Entity callback called.", "entity id", e.TriggerEntityId, "from state", e.FromState, "to state", e.ToState)
s.suiteCtx["entityCallbackInvoked"] = true 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 { func getEntityState(s *MySuite, entityId string) string {
state, err := s.app.GetState().Get(entityId) state, err := s.app.GetState().Get(entityId)
if err != nil { if err != nil {

2
go.mod
View File

@@ -1,6 +1,6 @@
module saml.dev/gome-assistant module saml.dev/gome-assistant
go 1.19 go 1.21
require ( require (
github.com/golang-module/carbon v1.7.1 github.com/golang-module/carbon v1.7.1

View File

@@ -2,7 +2,7 @@ package internal
import ( import (
"fmt" "fmt"
"log" "log/slog"
"reflect" "reflect"
"runtime" "runtime"
"time" "time"
@@ -27,7 +27,7 @@ func GetId() int64 {
func ParseTime(s string) carbon.Carbon { func ParseTime(s string) carbon.Carbon {
t, err := time.Parse("15:04", s) t, err := time.Parse("15:04", s)
if err != nil { 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) 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 { func ParseDuration(s string) time.Duration {
d, err := time.ParseDuration(s) d, err := time.ParseDuration(s)
if err != nil { 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 return d
} }

View File

@@ -2,7 +2,7 @@ package gomeassistant
import ( import (
"fmt" "fmt"
"log" "log/slog"
"time" "time"
"github.com/golang-module/carbon" "github.com/golang-module/carbon"
@@ -168,7 +168,7 @@ func runSchedules(a *App) {
sched = popSchedule(a) sched = popSchedule(a)
} }
log.Println("Next schedule:", sched.nextRunTime) slog.Info("Next schedule", "new run time", sched.nextRunTime)
time.Sleep(time.Until(sched.nextRunTime)) time.Sleep(time.Until(sched.nextRunTime))
sched.maybeRunCallback(a) sched.maybeRunCallback(a)
requeueSchedule(a, sched) requeueSchedule(a, sched)