From 1d39f5a34e2ccf390008f24f7db395f8cc7cb020 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:46:48 +0100 Subject: [PATCH] Update docs --- docs/conf.py | 1 + docs/rule_examples.rst | 23 ++++++++++++++++++++--- docs/util.rst | 3 +++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index c456e0b2..30cc030f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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', diff --git a/docs/rule_examples.rst b/docs/rule_examples.rst index e62e8ee4..64657c19 100644 --- a/docs/rule_examples.rst +++ b/docs/rule_examples.rst @@ -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 ` 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 ` for more information about the available internal topics. +It also uses the built in :ref:`rate limiter ` to limit the amount of notifications. .. exec_code:: @@ -130,6 +132,14 @@ to the mobile device (see :doc:`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): @@ -140,12 +150,19 @@ to the mobile device (see :doc:`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__() diff --git a/docs/util.rst b/docs/util.rst index 1499c06c..8caf7f6f 100644 --- a/docs/util.rst +++ b/docs/util.rst @@ -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.