add state as second arg for eventListenerCallback

This commit is contained in:
Sam Lewis
2022-11-08 22:48:34 -05:00
parent 5afa301f36
commit 1ec5878ff9
4 changed files with 14 additions and 4 deletions

9
app.go
View File

@@ -86,6 +86,15 @@ func (a *App) RegisterSchedules(schedules ...Schedule) {
}
now := time.Now()
// TODO: on the day that daylight savings starts/ends,
// this results in schedules being an hour late or hour early because
// StartOfDay() occurs before the switch. This happens if you start
// gome assistant on this day, and VERIFY I think will persist until you
// start it again. Sunrise/sunset should be unaffected since those come
// from HA.
//
// IDEA: splitting schedule into Interval and DailySchedule could address this on schedule
// side, by using SetTime instead of Add(time.Duration).
startTime := carbon.Now().StartOfDay().Carbon2Time()
// apply offset if set
if s.offset.Minutes() > 0 {

View File

@@ -26,7 +26,8 @@ type EntityListener struct {
exceptionRanges []timeRange
}
// TODO: add state object as second arg
// TODO: add RunOnStartup() to etl, evl, schedule
type EntityListenerCallback func(*Service, *State, EntityData)
type EntityData struct {

View File

@@ -22,7 +22,7 @@ type EventListener struct {
}
// TODO: add state object as second arg
type EventListenerCallback func(*Service, EventData)
type EventListenerCallback func(*Service, *State, EventData)
type EventData struct {
Type string
@@ -130,7 +130,7 @@ func callEventListeners(app *App, msg ws.ChanMsg) {
Type: baseEventMsg.Event.EventType,
RawEventJSON: msg.Raw,
}
go l.callback(app.service, eventData)
go l.callback(app.service, app.state, eventData)
l.lastRan = carbon.Now()
}
}

View File

@@ -56,7 +56,7 @@ func pantryLights(service *ga.Service, state *ga.State, sensor ga.EntityData) {
}
}
func onEvent(service *ga.Service, data ga.EventData) {
func onEvent(service *ga.Service, state *ga.State, data ga.EventData) {
// Since the structure of the event changes depending
// on the event type, you can Unmarshal the raw json
// into a Go type. If a type for your event doesn't