Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach logic trigger methods for plugins #106

Merged
merged 4 commits into from Sep 1, 2016

Conversation

ohinckel
Copy link
Member

(copyied from mknx/smarthome#142)

Plugin developers have two possibilities to attach the logic to the SmartHome.py configuration / system:

  • using parse_item() method
  • using parse_logic() method

The parse_item() method is called for each configured item and the plugin can decide if it is interested in item changes or not by simply returning an update method depending on the condition. When returning an update method a trigger method will be added to the item and invoked each time the item's value is changed.

The parse_logic() method is called for each configured logic and the plugin can decide if it is interested in the logic or not. This can be used to setup some configuration for the plugin, but can't be used to register a trigger method which is invoked each time the logic is triggered. This patch add this functionality to register a trigger method for logics by simply returning a trigger method.

Example:

  def parse_item(self, item):
      ...
      return self.update_item

  def parse_logic(self, logic):
      ...
      return self.trigger_logic

  def update_item(self, item, caller=None, source=None, dest=None):
      // do something on item value change
      ...

   def trigger_logic(self, logic, by=None, source=None, dest=None):
      // do something on logic trigger
      ...

This way a plugin can register a logic hook, which is called each time a logic was triggered.

@@ -339,15 +339,18 @@ def _task(self, name, obj, by, source, dest, value):
sh = self._sh # noqa
try:
exec(obj.bytecode)
for method in logic.get_method_triggers():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is a bug or a feature 😃 :
Due to the try/except Block starting in line 340, the hooked methods will only be called if the logic could be executed without error, and nobody called "exit()" inside the logic. In case of errors or "exit()" in the logic, the hooked methods will not be called.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it would be better to only trigger the methods when the logic was successfuly executed.

Using exit() is IMHO a bad idea since it should only be used to completely exit the program. A logic is only a piece of code running inside SmartHome and should never use exit().

But if the others like to work around this and catching the exceptions before triggering the methods I can change this.

@cstrassburg cstrassburg merged commit cd7b3ce into smarthomeNG:develop Sep 1, 2016
cstrassburg added a commit that referenced this pull request Sep 1, 2016
@cstrassburg
Copy link
Member

merged

@ohinckel ohinckel deleted the dev-parse-logic-trigger branch September 4, 2016 19:41
cstrassburg added a commit that referenced this pull request Dec 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants