Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Jan 9, 2024
1 parent 13d6615 commit 1d39f5a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def log(msg: str):
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinx_autodoc_typehints',
'sphinx_exec_code',
'sphinx.ext.inheritance_diagram',
Expand Down
23 changes: 20 additions & 3 deletions docs/rule_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ Turn a device off 30 seconds after one of the movement sensors in a room signals
Process Errors in Rules
------------------------------------------
This example shows how to create a rule with a function which will be called when **any** rule throws an error.
The rule function then can push the error message to an openHAB item or e.g. use Pushover to send the error message
to the mobile device (see :doc:`Advanced Usage <advanced_usage>` for more information).
The rule function then can push the error message to an openHAB item, use a notification service to send the error
message to the mobile device or send an email with the error message.
See :doc:`Advanced Usage <advanced_usage>` for more information about the available internal topics.
It also uses the built in :ref:`rate limiter <RATE_LIMITER>` to limit the amount of notifications.

.. exec_code::

Expand All @@ -130,6 +132,14 @@ to the mobile device (see :doc:`Advanced Usage <advanced_usage>` for more inform
import HABApp
from HABApp.core.events.habapp_events import HABAppException
from HABApp.core.events import EventFilter
from HABApp.util import RateLimiter


# Set up rate limiter to limit the amount of notifications
LIMITER = RateLimiter('MyNotifications')
LIMITER.parse_limits('5 in 1 minute', algorithm='fixed_window_elastic_expiry')
LIMITER.parse_limits("20 in 1 hour", algorithm='leaky_bucket')


class NotifyOnError(HABApp.Rule):
def __init__(self):
Expand All @@ -140,12 +150,19 @@ to the mobile device (see :doc:`Advanced Usage <advanced_usage>` for more inform

def on_error(self, error_event: HABAppException):
msg = error_event.to_str() if isinstance(error_event, HABAppException) else error_event

# use limiter
if not LIMITER.allow():
return None

# Replace this part with your notification logic
print('Error in rules:')
print(msg)

NotifyOnError()


# this is a faulty example. Do not create this part!
# this is a faulty rule as an example. Do not create this part!
class FaultyRule(HABApp.Rule):
def __init__(self):
super().__init__()
Expand Down
3 changes: 3 additions & 0 deletions docs/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Converts a hsb value to the rgb color space
.. autofunction:: HABApp.util.functions.hsb_to_rgb


.. _RATE_LIMITER:


Rate limiter
------------------------------
A simple rate limiter implementation which can be used in rules.
Expand Down

0 comments on commit 1d39f5a

Please sign in to comment.