openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 68 forks source link

Add Items, Things, and Directories #373

Closed 5iver closed 3 years ago

5iver commented 3 years ago
            @when("Item added")
            @when("Item removed")
            @when("Item updated")
            @when("Thing added")
            @when("Thing removed")
            @when("Thing updated")
            @when("Directory /opt/test [created, deleted, modified]")# requires S1566, 2.5M2 or newer
            @when("Subdirectory 'C:\My Stuff' [created]")# requires S1566, 2.5M2 or newer

One of the cool bits here is to be able to reload a rule using another rule when an Item or Thing is added/removed. Something like...

from core.rules import rule
from core.triggers import when

def zwave_thing_trigger_generator():
    def generated_triggers(function):
        for thing_uid in [thing.UID.toString() for thing in things.all if "zwave" in thing.UID.toString()]:
            when("Thing {} changed".format(thing_uid))(function)
        return function
    return generated_triggers

@rule("Z-Wave Thing added or removed")
@when("Thing added")
@when("Thing removed")
@when("System started")
def thing_added_or_removed(event):
    if event is not None:
        thing_added_or_removed.log.warn("thing: {}, topic: {}".format(event.thing.UID, event.topic.split("/")[-1]))
        scriptExtension.importPreset("RuleSupport")
        for my_rule in [my_rule for my_rule in rules.all if my_rule.name == "Alert: Z-Wave Thing"]:
            ruleRegistry.remove(my_rule.UID)

    @rule("Alert: Z-Wave Thing")
    @zwave_thing_trigger_generator()
    def zwave_thing_alert(event):
        zwave_thing_alert.log.warn("{} is '{}'".format(event.thingUID, event.statusInfo.status))

Something similar can be done for Items and a "Member of" trigger. Oh yeah... and DirectoryEventTrigger works again!

Signed-off-by: Scott Rushworth github@5iver.com