Yes, you are right, it is not clear why all actions should be registered and its correct that this is not needed in many cases.
The actions provided as modules can be set HIDDEN for UIs, but not completely removed as they have to be known to the registry.
In the last revision of the sourcecode I have added “ScriptedAction” and “ScriptedCondition”, which help in that matter, as these are the only modules known to the RuleEngine, the mapping to the actual Rule/Action is done by the JSR-Loader.
In this version, I can already write something very similar to the openhab 1 jsr223 implementation:
def itemStateChangeTrigger(triggerName, itemName):
return Trigger(triggername, "ItemStateChangeTrigger", {
"itemName": itemName
})
# SimpleRule can combine any TriggerType and ConditionType registered to the RuleEngine
class MySimpleRule(SimpleRule):
def __init__(self):
pass
def getTriggers(self):
return [itemStateTrigger("trigger1", "Sun_Elevation")]
def getConditions(self):
return []
def execute(self, module, inputs):
print "module:", module
print "configuration:", module.configuration
print "inputs:", inputs
print dir(inputs)
for k, v in inputs.iteritems():
print "key:", k, "value:", v.TYPE, "value dir:", dir(v)
class ChangedEventTrigger:
UID = 0
def __init__(self, itemName, fromVale, toValue):
self.UID += 1
self.condition = None
self.trigger = Trigger(itemName+str(self.UID)+str(toValue), "GenericEventTrigger", {
"eventSource":itemName,
"eventTopic":"smarthome/items/*",
"eventTypes":"ItemStateChangedEvent"
})
#todo: build condition for fromValue (and toValue)
class Rule():
pass
class CompatRule(SimpleRule):
def __init__(self, rule):
self.rule = rule
def getTriggers(self):
return [t.trigger for t in self.rule.getEventTrigger() if t.trigger]
def getConditions(self):
return [t.condition for t in self.rule.getEventTrigger() if t.condition]
def execute(self, module, inputs):
return self.rule.execute(inputs[inputs.keys()[0]])
class Openhab1Rule(Rule):
def getEventTrigger(self):
return [ChangedEventTrigger("Light_FF_Child_Ceiling", None, OnOffType.ON)]
def execute(self, event):
print "my event:", event
RuleRegistry.addSimple(CompatRule(Openhab1Rule()))
output:
my event: Light_FF_Child_Ceiling changed from OFF to ON
Still missing: script removal logic: actions and module types need to be removed if the script is updated or removed…