Skip to content

Commit

Permalink
Add condition for lights/sensor automation
Browse files Browse the repository at this point in the history
  • Loading branch information
dansimau committed Dec 23, 2024
1 parent 4e37dea commit b75562e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions automations/sensor_lights.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type SensorsTriggerLights struct {
name string
log *slog.Logger

condition func() bool

sensors []hal.EntityInterface
lights []hal.LightInterface
turnsOffAfter *time.Duration
Expand All @@ -27,6 +29,13 @@ func NewSensorsTriggersLights() *SensorsTriggerLights {
}
}

// WithCondition sets a condition that must be true for the automation to run.
func (a *SensorsTriggerLights) WithCondition(condition func() bool) *SensorsTriggerLights {
a.condition = condition

return a
}

func (a *SensorsTriggerLights) WithName(name string) *SensorsTriggerLights {
a.name = name
a.log = slog.With("automation", a.name)
Expand Down Expand Up @@ -100,6 +109,11 @@ func (a *SensorsTriggerLights) turnOffLights() {
}

func (a *SensorsTriggerLights) Action() {
if a.condition != nil && !a.condition() {
a.log.Info("Condition not met, skipping")
return
}

if a.triggered() {
a.log.Info("Sensor triggered, turning on lights")
a.stopTurnOffTimer()
Expand Down

0 comments on commit b75562e

Please sign in to comment.