I am using jython to create some advanced handling of my light. It is working fine as, but due to various changes in the jsr223 I had made some panic changes / hacks over time. And I am quite sure that it could be simplified a lot.
I would like to have the ability to enable / disable rules by thier UID. I have found quite a few examples of that. But the problem is that UID always return None
for the class. The rule triggers will be created dynamically. I was first looking at the decorator approach, but I cannot figure out how to build multiple instances of a decorated class. Therefore I continued to look at this approach:
scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")
class MyRule(SimpleRule):
def __init__(self):
self.triggers = [
TriggerBuilder.create()
.withId("MyTrigger")
.withTypeUID("core.ItemStateUpdateTrigger")
.withConfiguration(
Configuration({
"itemName": "TestString1"
})).build()
]
def execute(self, module, input):
events.postUpdate("TestString2", "some data")
my_rule = MyRule()
automationManager.addRule(my_rule)
If I query the UID, I just get nothing back.
I also tried this approach (with same result):
from org.slf4j import LoggerFactory
scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")
class GenericCronTriggerRawAPI(SimpleRule):
def __init__(self):
self.triggers = [
TriggerBuilder.create()
.withId("Hello_World_Cron_Trigger")
.withTypeUID("timer.GenericCronTrigger")
.withConfiguration(
Configuration({
"cronExpression": "0/10 * * * * ?"
})).build(),
TriggerBuilder.create()
.withId("Hello_World_Item_State_Trigger")
.withTypeUID("timer.GenericCronTrigger")
.withConfiguration(
Configuration({
"itemName": "Test_Switch_1"
})).build()
]
self.name = "Jython Hello World (GenericCronTrigger raw API)"
self.description = "This is an example Jython cron rule using the raw API"
self.tags = set("Example rule tag")
self.log = LoggerFactory.getLogger("jsr223.jython.Hello World (GenericCronTrigger raw API)")
def execute(self, module, inputs):
self.log.info("Hello World!")
automationManager.addRule(GenericCronTriggerRawAPI())
The above samples is from the documentation and I have tried on my Windows machine until now. Any suggestions?