Skip to content

Commit

Permalink
Dim lights before turning off
Browse files Browse the repository at this point in the history
  • Loading branch information
dansimau committed Dec 29, 2024
1 parent 9b1bc4c commit e676af0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
77 changes: 75 additions & 2 deletions automations/sensor_lights.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,40 @@ type SensorsTriggerLights struct {
name string
log *slog.Logger

// Brightness is the brightness of the lights when they are turned on. We
// have to set a brightness here so support dimming lights before turn off.
// The reason is that Home Assistant doesn't support changing the brightness of
// a light while it is off. Because the light is dimmed prior to turn off,
// turning it back on would mean it comes back on in a dimmed state. Thus we
// have to specify a default brightness when turning on to avoid this.
brightness float64
scene map[string]any

condition func() bool // optional: func that must return true for the automation to run
conditionScene []ConditionScene
sensors []hal.EntityInterface
turnsOnLights []hal.LightInterface
turnsOffLights []hal.LightInterface
turnsOffAfter *time.Duration // optional: duration after which lights will turn off after being turned on

turnOffTimer *time.Timer
dimLightsTimer *time.Timer
turnOffTimer *time.Timer
}

func NewSensorsTriggerLights() *SensorsTriggerLights {
return &SensorsTriggerLights{
log: slog.Default(),
brightness: 255,
log: slog.Default(),
}
}

// WithBrightness sets the brightness of the lights when they are turned on.
func (a *SensorsTriggerLights) WithBrightness(brightness float64) *SensorsTriggerLights {
a.brightness = brightness

return a
}

// WithCondition sets a condition that must be true for the automation to run.
func (a *SensorsTriggerLights) WithCondition(condition func() bool) *SensorsTriggerLights {
a.condition = condition
Expand Down Expand Up @@ -102,6 +120,12 @@ func (a *SensorsTriggerLights) TurnsOffAfter(turnsOffAfter time.Duration) *Senso
return a
}

func (a *SensorsTriggerLights) SetScene(scene map[string]any) *SensorsTriggerLights {
a.scene = scene

return a
}

// triggered returns true if any of the sensors have been triggered.
func (a *SensorsTriggerLights) triggered() bool {
for _, sensor := range a.sensors {
Expand All @@ -113,6 +137,24 @@ func (a *SensorsTriggerLights) triggered() bool {
return false
}

func (a *SensorsTriggerLights) startDimLightsTimer() {
if a.turnsOffAfter == nil {
return
}

// TODO: Make this configurable
dimLightsAfter := *a.turnsOffAfter - 10*time.Second
if dimLightsAfter < 1*time.Second {
return
}

if a.dimLightsTimer == nil {
a.dimLightsTimer = time.AfterFunc(dimLightsAfter, a.dimLights)
} else {
a.dimLightsTimer.Reset(dimLightsAfter)
}
}

func (a *SensorsTriggerLights) startTurnOffTimer() {
if a.turnsOffAfter == nil {
return
Expand All @@ -123,6 +165,8 @@ func (a *SensorsTriggerLights) startTurnOffTimer() {
} else {
a.turnOffTimer.Reset(*a.turnsOffAfter)
}

a.startDimLightsTimer()
}

func (a *SensorsTriggerLights) stopTurnOffTimer() {
Expand All @@ -134,19 +178,48 @@ func (a *SensorsTriggerLights) stopTurnOffTimer() {
func (a *SensorsTriggerLights) turnOnLights() {
var attributes map[string]any

// If a scene is set, use it.
if a.scene != nil {
attributes = a.scene
}

// If a condition scene matches use that
for _, conditionScene := range a.conditionScene {
if conditionScene.Condition() {
attributes = conditionScene.Scene
}
}

// Otherwise use the default brightness.
if attributes == nil {
attributes = map[string]any{"brightness": a.brightness}
}

for _, light := range a.turnsOnLights {
if err := light.TurnOn(attributes); err != nil {
slog.Error("Error turning on light", "error", err)
}
}
}

func (a *SensorsTriggerLights) dimLights() {
a.log.Info("Dimming lights prior to turning off")

for _, light := range a.turnsOffLights {
brightness := light.GetBrightness()
if brightness < 2 {
a.log.Info("Light is already at minimum brightness, skipping dimming", "light", light.GetID())
continue
}

dimmedBrightness := brightness / 2

if err := light.TurnOn(map[string]any{"brightness": dimmedBrightness}); err != nil {
slog.Error("Error dimming light", "error", err)
}
}
}

func (a *SensorsTriggerLights) turnOffLights() {
a.log.Info("Turning off lights")

Expand Down
54 changes: 54 additions & 0 deletions entity_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package hal
import (
"errors"
"log/slog"
"strings"

"github.com/dansimau/hal/hassws"
"github.com/dansimau/hal/homeassistant"
)

type LightInterface interface {
EntityInterface

GetBrightness() float64
IsOn() bool
TurnOn(attributes ...map[string]any) error
TurnOff() error
Expand All @@ -21,6 +26,14 @@ func NewLight(id string) *Light {
return &Light{Entity: NewEntity(id)}
}

func (l *Light) GetBrightness() float64 {
if v, ok := l.Entity.GetState().Attributes["brightness"].(float64); ok {
return v
}

return 0
}

func (l *Light) IsOn() bool {
return l.Entity.GetState().State == "on"
}
Expand Down Expand Up @@ -83,6 +96,47 @@ func (l *Light) TurnOff() error {

type LightGroup []LightInterface

func (lg LightGroup) BindConnection(connection *Connection) {
for _, l := range lg {
l.BindConnection(connection)
}
}

func (lg LightGroup) GetID() string {
if len(lg) == 0 {
return "(empty light group)"
}

ids := make([]string, len(lg))
for i, l := range lg {
ids[i] = l.GetID()
}

return strings.Join(ids, ", ")
}

func (lg LightGroup) GetBrightness() float64 {
if len(lg) == 0 {
return 0
}

return lg[0].GetBrightness()
}

func (lg LightGroup) GetState() homeassistant.State {
if len(lg) == 0 {
return homeassistant.State{}
}

return lg[0].GetState()
}

func (lg LightGroup) SetState(state homeassistant.State) {
for _, l := range lg {
l.SetState(state)
}
}

func (lg LightGroup) IsOn() bool {
for _, l := range lg {
if !l.IsOn() {
Expand Down

0 comments on commit e676af0

Please sign in to comment.