I’m using the openhab2-jython scripts for rule support.
Rules using the rule decorators work fine, however, for a special use case I have my own decorators, so I want to directly register at at automationManager
.
It seems that the rule is not loaded correctly. I can see the rule in PaperUI, but there are no triggers? However, the trigger is set in the rule.
The rule worked fine with openhab 2.3 and the old version of the openhab2-jyton scripts.
Idea is that there is a String item and when a string is written to it a function with that name is executed.
Therefore, the decorator task = lambda f: tasks.setdefault(f.__name__, f)
adds the function name and the function itself to a dictionary.
This mechanism works fine (the log.debug shows the dictionary), however, there is no trigger for the rule, altough a trigger is set with self.triggers = [ItemStateChangeTrigger("global_command")]
??
Am I missing some mandatory field for the SimpleRule class? Or what else could cause this problem?
scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")
from core.triggers import ItemStateChangeTrigger
from core.log import logging, LOG_PREFIX
rlog = logging.getLogger("RULES.CMD.Global")
tasks = {}
task = lambda f: tasks.setdefault(f.__name__, f)
class GlobalCmdHandler(SimpleRule):
def __init__(self):
self.triggers = [ItemStateChangeTrigger("global_command")]
rlog.debug(u"Loading global command handler")
rlog.debug(tasks)
rlog.debug(self.triggers)
self.__name__ = "Global Command handler"
@task
def idle():
rlog.debug(u"Idle")
return False
@task
def all_off():
rlog.info(u"Alles AUS!")
# details omitted
return True
@task
def chill():
rlog.info(u"LED-Mode chill")
# details omitted
return true
# execute method maps global_command to a method with the same name.
def execute(self, module, input):
rlog.debug(u"Key: " + unicode(input))
rc = tasks.get(str(items.global_command), lambda: rlog.error("Wrong key"))()
if (rc):
rlog.debug(u"Updateding global_command back to idle state")
events.postUpdate("global_command", "idle")
gcmd = GlobalCmdHandler()
automationManager.addRule(gcmd)