From f6f211dec09e8c6fe8ec15fa57da8da2c758e2ef Mon Sep 17 00:00:00 2001 From: jweigele Date: Wed, 30 Aug 2023 11:40:12 -0700 Subject: [PATCH] "OverrideHoldSeconds" for switch overrides to provide longer duration --- lights/main.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lights/main.go b/lights/main.go index 1434162..7c768fe 100644 --- a/lights/main.go +++ b/lights/main.go @@ -62,6 +62,7 @@ type Switch struct { Location string `yaml:"Location"` FriendlyName string `yaml:"FriendlyName"` OverrideSeconds int `yaml:"OverrideSeconds"` + OverrideHoldSeconds int `yaml:"OverrideHoldSeconds"` StateExpiresAt time.Time ColorStateExpiresAt time.Time parentRelay *RGBRelay @@ -92,18 +93,34 @@ func (curSwitch *Switch) getRoutingKey() string { return fmt.Sprintf("zigbee2mqtt.%s", curSwitch.FriendlyName) } -func (curSwitch *Switch) setState(state bool) { +func (curSwitch *Switch) setStateExpireSeconds(state bool, expireSeconds int) { curTime := time.Now().UTC() curSwitch.State = state - curSwitch.StateExpiresAt = curTime.Add(time.Duration(curSwitch.OverrideSeconds) * time.Second) + curSwitch.StateExpiresAt = curTime.Add(time.Duration(expireSeconds) * time.Second) logger.Info("Setting state for switch", "state", state, "switch", curSwitch) } +func (curSwitch *Switch) setState(state bool) { + curSwitch.setStateExpireSeconds(state, curSwitch.OverrideSeconds) +} -func (curSwitch *Switch) setColor() { +func (curSwitch *Switch) setStateHold(state bool) { + curSwitch.setStateExpireSeconds(state, curSwitch.OverrideHoldSeconds) +} + +func (curSwitch *Switch) setColorExpireSeconds(expireSeconds int) { curTime := time.Now().UTC() curSwitch.ColorStateExpiresAt = curTime.Add(time.Duration(curSwitch.OverrideSeconds) * time.Second) curSwitch.parentRelay.ForceRefresh() logger.Info("Setting color state for switch", "switch", curSwitch) + +} + +func (curSwitch *Switch) setColor() { + curSwitch.setColorExpireSeconds(curSwitch.OverrideSeconds) +} + +func (curSwitch *Switch) setColorHold() { + curSwitch.setColorExpireSeconds(curSwitch.OverrideHoldSeconds) } // get on off status, first "is active", second "is on (or off)" @@ -139,10 +156,16 @@ func (curSwitch *Switch) parseState(data map[string]interface{}) { switch switchAction { case "on_press_release", "on_press": curSwitch.setState(true) + case "on_hold_release", "on_hold": + curSwitch.setStateHold(true) case "off_press_release", "off_press": curSwitch.setState(false) + case "off_hold_release", "off_hold": + curSwitch.setStateHold(false) case "up_press", "up_press_release": curSwitch.setColor() + case "up_hold", "up_hold_release": + curSwitch.setColorHold() case "down_press", "down_press_release": curSwitch.setExpired(true) default: @@ -303,7 +326,7 @@ func sunIsUp(localTime time.Time) bool { year, month, day, ) oneDay, _ := time.ParseDuration("24h") - fudgeDuration, _ := time.ParseDuration("2h") + fudgeDuration, _ := time.ParseDuration("3h") year, month, day = localTime.Add(-oneDay).Date() risePrevious, setPrevious := sunrise.SunriseSunset( sunriseLat, sunriseLon, -- 2.30.2