"OverrideHoldSeconds" for switch overrides to provide longer duration
authorjweigele <jweigele@local>
Wed, 30 Aug 2023 18:40:12 +0000 (11:40 -0700)
committerjweigele <jweigele@local>
Wed, 30 Aug 2023 18:40:12 +0000 (11:40 -0700)
lights/main.go

index 1434162d53d0f8a4388ae060dfb458d664c0b2b2..7c768fe27dc66847e3f0534db9aba0a5d07328be 100644 (file)
@@ -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,