mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-10 14:07:26 -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)
|
ws.SubscribeToStateChangedEvents(id, a.conn, a.ctx)
|
||||||
a.entityListenersId = id
|
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)
|
elChan := make(chan ws.ChanMsg)
|
||||||
go ws.ListenWebsocket(a.conn, a.ctx, elChan)
|
go ws.ListenWebsocket(a.conn, a.ctx, elChan)
|
||||||
|
|
||||||
var msg ws.ChanMsg
|
var msg ws.ChanMsg
|
||||||
for {
|
for {
|
||||||
msg = <-elChan
|
msg = <-elChan
|
||||||
|
|||||||
@@ -24,9 +24,10 @@ type EntityListener struct {
|
|||||||
|
|
||||||
exceptionDays []time.Time
|
exceptionDays []time.Time
|
||||||
exceptionRanges []timeRange
|
exceptionRanges []timeRange
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: add RunOnStartup() to etl, evl, schedule
|
runOnStartup bool
|
||||||
|
runOnStartupCompleted bool
|
||||||
|
}
|
||||||
|
|
||||||
type EntityListenerCallback func(*Service, *State, EntityData)
|
type EntityListenerCallback func(*Service, *State, EntityData)
|
||||||
|
|
||||||
@@ -142,6 +143,11 @@ func (b elBuilder3) ExceptionRange(start, end time.Time) elBuilder3 {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b elBuilder3) RunOnStartup() elBuilder3 {
|
||||||
|
b.entityListener.runOnStartup = true
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b elBuilder3) Build() EntityListener {
|
func (b elBuilder3) Build() EntityListener {
|
||||||
return b.entityListener
|
return b.entityListener
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user