mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-05 23:15:07 -06:00
add etl.RunOnStartup
This commit is contained in:
23
app.go
23
app.go
@@ -189,10 +189,29 @@ func (a *App) Start() {
|
||||
ws.SubscribeToStateChangedEvents(id, a.conn, a.ctx)
|
||||
a.entityListenersId = id
|
||||
|
||||
// entity listeners
|
||||
// entity listeners runOnStartup
|
||||
for eid, etls := range a.entityListeners {
|
||||
for _, etl := range etls {
|
||||
if etl.runOnStartup && !etl.runOnStartupCompleted {
|
||||
entityState, err := a.state.Get(eid)
|
||||
if err != nil {
|
||||
log.Default().Println("Failed to get entity state \"", eid, "\" during startup, skipping RunOnStartup")
|
||||
}
|
||||
etl.callback(a.service, a.state, EntityData{
|
||||
TriggerEntityId: eid,
|
||||
FromState: entityState.State,
|
||||
FromAttributes: entityState.Attributes,
|
||||
ToState: entityState.State,
|
||||
ToAttributes: entityState.Attributes,
|
||||
LastChanged: entityState.LastChanged,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// entity listeners and event listeners
|
||||
elChan := make(chan ws.ChanMsg)
|
||||
go ws.ListenWebsocket(a.conn, a.ctx, elChan)
|
||||
|
||||
var msg ws.ChanMsg
|
||||
for {
|
||||
msg = <-elChan
|
||||
|
||||
@@ -24,9 +24,10 @@ type EntityListener struct {
|
||||
|
||||
exceptionDays []time.Time
|
||||
exceptionRanges []timeRange
|
||||
}
|
||||
|
||||
// TODO: add RunOnStartup() to etl, evl, schedule
|
||||
runOnStartup bool
|
||||
runOnStartupCompleted bool
|
||||
}
|
||||
|
||||
type EntityListenerCallback func(*Service, *State, EntityData)
|
||||
|
||||
@@ -142,6 +143,11 @@ func (b elBuilder3) ExceptionRange(start, end time.Time) elBuilder3 {
|
||||
return b
|
||||
}
|
||||
|
||||
func (b elBuilder3) RunOnStartup() elBuilder3 {
|
||||
b.entityListener.runOnStartup = true
|
||||
return b
|
||||
}
|
||||
|
||||
func (b elBuilder3) Build() EntityListener {
|
||||
return b.entityListener
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user