This commit is contained in:
Barbudor 2023-06-18 23:03:45 +02:00 committed by GitHub
parent 5059a11b9f
commit 92ce18f87c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,6 +118,9 @@ class Rule_Matcher
# <instance match="Payload">, <instance match_any>, <instance match="Temperature", <instance op='>' val='50'>
#
# Instance types:
# Rule_Matcher_AND_List(key): checks that the input map matches with all contains individual conditions in the list
# returns an array of sub-value or `nil` if at least one doesn't match
#
# Rule_Matcher_Key(key): checks that the input map contains the key (case insensitive) and
# returns the sub-value or `nil` if the key does not exist
#
@ -127,6 +130,33 @@ class Rule_Matcher
# Rule_Matcher_Operator: checks is a simple value (numerical or string) matches the operator and the value
# returns the value unchanged if match, or `nil` if no match
# do a logical AND of all sub-matchers
static class Rule_Matcher_AND_List
var and_list
def init(and_list)
self.and_list = and_list
end
def match(val)
var idx = 0
var ret_list = []
while idx < size(self.and_list)
var rule = self.and_list[idx]
var ret = rule.match(val)
if ret == nil return nil end # abort as soon as a matcher fails
ret_list.push(ret)
idx += 1
end
return ret_list
end
def tostring()
return "<Matcher_AND_List " + str(self.and_list) + ">"
end
end
static class Rule_Matcher_Key
var name # literal name of what to match
@ -294,6 +324,18 @@ class Rule_Matcher
import string
if pattern == nil return nil end
# special case if a list of patterns, recursively make a list of matchers
if isinstance(pattern, list)
var and_list = []
var trigger_list = []
for p: pattern
var matcher = _class.parse(p)
and_list.push(matcher)
trigger_list.push(matcher.trigger)
end
return _class(pattern, trigger_list, [_class.Rule_Matcher_AND_List(and_list)])
end
var matchers = []
# changes "Dimmer>50" to ['Dimmer', '>', '50']