From 38d777404bcdf30cb08b2d7be829985895d17eb8 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Mon, 20 Sep 2021 22:37:41 +0200 Subject: [PATCH] fix 'all.*' conditions counting 'incapable' devices like sensors. --- models/eventhandler.go | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/models/eventhandler.go b/models/eventhandler.go index 9d4d4e0..32e5a8b 100644 --- a/models/eventhandler.go +++ b/models/eventhandler.go @@ -101,31 +101,54 @@ func (c *EventCondition) check(key, value string, targets []Device) bool { all := strings.HasPrefix(key, "all.") if any || all { count := 0 + total := 0 for _, target := range targets { - if c.checkDevice(key[4:], target) { + matches, skip := c.checkDevice(key[4:], target) + if skip { + continue + } + + if matches { count++ } + total++ } - return (any && count > 0) || (all && count == len(targets)) + return (any && count > 0) || (all && count == total) } return c.matches(value) } -func (c *EventCondition) checkDevice(key string, device Device) bool { +func (c *EventCondition) checkDevice(key string, device Device) (matches bool, skip bool) { switch key { case "power": - return c.matches(strconv.FormatBool(device.State.Power)) + if !device.HasCapability(DCPower) { + return false, true + } + + return c.matches(strconv.FormatBool(device.State.Power)), false case "color": - return c.matches(device.State.Color.String()) + if !device.HasCapability(DCColorKelvin, DCColorHS, DCColorHSK) { + return false, true + } + + return c.matches(device.State.Color.String()), false case "intensity": - return c.matches(strconv.FormatFloat(device.State.Intensity, 'f', -1, 64)) + if !device.HasCapability(DCIntensity) { + return false, true + } + + return c.matches(strconv.FormatFloat(device.State.Intensity, 'f', -1, 64)), false case "temperature": - return c.matches(strconv.FormatFloat(device.State.Temperature, 'f', -1, 64)) - } + if !device.HasCapability(DCTemperatureControl, DCTemperatureSensor) { + return false, true + } - return false + return c.matches(strconv.FormatFloat(device.State.Temperature, 'f', -1, 64)), false + default: + return false, true + } } var numRegex = regexp.MustCompile("^{-[0-9].}+$")