spacemanspiff2007 / HABApp

Easy home automation with MQTT and/or openHAB
Apache License 2.0
54 stars 23 forks source link

how to check if rule is loaded? #407

Closed UrsusS closed 11 months ago

UrsusS commented 11 months ago

In some cases it can happen that a rule is not loaded when it should be called, e.g.

I haven't found a function to test if the rule is available. Now I do something like this to not stop the function, but it still logs the KeyError.

        try:
            Rule = self.get_rule("MyRule")
            Rule.DoSomething()
        except KeyError:
            self.log.warning(
                LOGGER + "System noch nicht initialisiert - versuche später nochmal"
            )

I would prefer to have something like this:

        if self.is_rule_loaded("MyRule"):
            Rule = self.get_rule("MyRule")
            Rule.DoSomething()
        else:
            self.log.warning(
                LOGGER + "System noch nicht initialisiert - versuche später nochmal"
            )

Is this functionality already existing or can it get implemented?

spacemanspiff2007 commented 11 months ago

You should specif the dependency with depends on. Otherwise you can just catch the error.