refactor all automation types to take a list

This commit is contained in:
Sam Lewis
2023-10-22 18:07:42 -04:00
parent 3ec6608714
commit 824d6c12d1
4 changed files with 36 additions and 42 deletions

View File

@@ -160,12 +160,12 @@ func (b elBuilder3) EnabledWhen(entityId, state string, runOnNetworkError bool)
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
}
if b.entityListener.disabledEntity != "" {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting EnabledWhen on an entity listener with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
RunOnError: runOnNetworkError,
}
b.entityListener.enabledEntity = entityId
b.entityListener.enabledEntityState = state
b.entityListener.enabledEntityRunOnError = runOnNetworkError
b.entityListener.enabledEntities = append(b.entityListener.enabledEntities, i)
return b
}
@@ -177,12 +177,12 @@ func (b elBuilder3) DisabledWhen(entityId, state string, runOnNetworkError bool)
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
}
if b.entityListener.enabledEntity != "" {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting DisabledWhen on an entity listener with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
RunOnError: runOnNetworkError,
}
b.entityListener.disabledEntity = entityId
b.entityListener.disabledEntityState = state
b.entityListener.disabledEntityRunOnError = runOnNetworkError
b.entityListener.disabledEntities = append(b.entityListener.disabledEntities, i)
return b
}
@@ -233,10 +233,10 @@ func callEntityListeners(app *App, msgBytes []byte) {
if c := checkExceptionRanges(l.exceptionRanges); c.fail {
continue
}
if c := checkEnabledEntity(app.state, l.enabledEntity, l.enabledEntityState, l.enabledEntityRunOnError); c.fail {
if c := checkEnabledEntity(app.state, l.enabledEntities); c.fail {
continue
}
if c := checkDisabledEntity(app.state, l.disabledEntity, l.disabledEntityState, l.disabledEntityRunOnError); c.fail {
if c := checkDisabledEntity(app.state, l.disabledEntities); c.fail {
continue
}

View File

@@ -102,12 +102,12 @@ func (b eventListenerBuilder3) EnabledWhen(entityId, state string, runOnNetworkE
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in eventListener EnabledWhen entityId='%s' state='%s' runOnNetworkError='%t'", entityId, state, runOnNetworkError))
}
if b.eventListener.disabledEntity != "" {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting EnabledWhen entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
RunOnError: runOnNetworkError,
}
b.eventListener.enabledEntity = entityId
b.eventListener.enabledEntityState = state
b.eventListener.enabledEntityRunOnError = runOnNetworkError
b.eventListener.enabledEntities = append(b.eventListener.enabledEntities, i)
return b
}
@@ -119,12 +119,12 @@ func (b eventListenerBuilder3) DisabledWhen(entityId, state string, runOnNetwork
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in eventListener EnabledWhen entityId='%s' state='%s' runOnNetworkError='%t'", entityId, state, runOnNetworkError))
}
if b.eventListener.enabledEntity != "" {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting DisabledWhen entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
RunOnError: runOnNetworkError,
}
b.eventListener.disabledEntity = entityId
b.eventListener.disabledEntityState = state
b.eventListener.disabledEntityRunOnError = runOnNetworkError
b.eventListener.disabledEntities = append(b.eventListener.disabledEntities, i)
return b
}
@@ -162,10 +162,10 @@ func callEventListeners(app *App, msg ws.ChanMsg) {
if c := checkExceptionRanges(l.exceptionRanges); c.fail {
continue
}
if c := checkEnabledEntity(app.state, l.enabledEntity, l.enabledEntityState, l.enabledEntityRunOnError); c.fail {
if c := checkEnabledEntity(app.state, l.enabledEntities); c.fail {
continue
}
if c := checkDisabledEntity(app.state, l.disabledEntity, l.disabledEntityState, l.disabledEntityRunOnError); c.fail {
if c := checkDisabledEntity(app.state, l.disabledEntities); c.fail {
continue
}

View File

@@ -114,12 +114,12 @@ func (ib intervalBuilderEnd) EnabledWhen(entityId, state string, runOnNetworkErr
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
}
if ib.interval.disabledEntity != "" {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting EnabledWhen on an entity listener with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
RunOnError: runOnNetworkError,
}
ib.interval.enabledEntity = entityId
ib.interval.enabledEntityState = state
ib.interval.enabledEntityRunOnError = runOnNetworkError
ib.interval.enabledEntities = append(ib.interval.enabledEntities, i)
return ib
}
@@ -131,12 +131,12 @@ func (ib intervalBuilderEnd) DisabledWhen(entityId, state string, runOnNetworkEr
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
}
if ib.interval.enabledEntity != "" {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting DisabledWhen on an entity listener with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
RunOnError: runOnNetworkError,
}
ib.interval.disabledEntity = entityId
ib.interval.disabledEntityState = state
ib.interval.disabledEntityRunOnError = runOnNetworkError
ib.interval.disabledEntities = append(ib.interval.disabledEntities, i)
return ib
}
@@ -180,10 +180,10 @@ func (i Interval) maybeRunCallback(a *App) {
if c := checkExceptionRanges(i.exceptionRanges); c.fail {
return
}
if c := checkEnabledEntity(a.state, i.enabledEntity, i.enabledEntityState, i.enabledEntityRunOnError); c.fail {
if c := checkEnabledEntity(a.state, i.enabledEntities); c.fail {
return
}
if c := checkDisabledEntity(a.state, i.disabledEntity, i.disabledEntityState, i.disabledEntityRunOnError); c.fail {
if c := checkDisabledEntity(a.state, i.disabledEntities); c.fail {
return
}
go i.callback(a.service, a.state)

View File

@@ -121,9 +121,6 @@ func (sb scheduleBuilderEnd) EnabledWhen(entityId, state string, runOnNetworkErr
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
}
if len(sb.schedule.disabledEntities) != 0 {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting EnabledWhen on a schedule with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
}
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,
@@ -141,9 +138,6 @@ func (sb scheduleBuilderEnd) DisabledWhen(entityId, state string, runOnNetworkEr
if entityId == "" {
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
}
if len(sb.schedule.enabledEntities) != 0 {
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting DisabledWhen on a schedule with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
}
i := internal.EnabledDisabledInfo{
Entity: entityId,
State: state,