Skip to content

Commit

Permalink
lib.item: log_mapping and log_rules are now expected to be stated as …
Browse files Browse the repository at this point in the history
…a list. However, putting a dict as string is still possible (non-breaking).
  • Loading branch information
onkelandy committed Oct 20, 2024
1 parent 6f57729 commit 1787ecd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
30 changes: 14 additions & 16 deletions doc/user/source/referenz/items/standard_attribute/log_change.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,40 +135,38 @@ Attribut *log_mapping*
======================

Über das **log_mapping** Attribut kann festgelegt werden, auf welche Werte/Strings der Wert eines Items für das
Logging gemappt werden soll. Das Attribut **log_mapping** enthält dazu in einem String die Beschreibung eines
dicts. Wobei der Key den zu übersetzenden/mappenden Wert angibt und der dazu gehörige Value des dicts den String
angibt, der über die Variable ``{mvalue}`` ausgegeben wird.
Logging gemappt werden soll. Das Attribut **log_mapping** enthält dazu eine Liste mit Wertzuweisungen im folgenden Format:
zu übersetzender/mappender Wert: String, der über die Variable ``{mvalue}`` ausgegeben wird

**Beispiel:**

.. code-block:: yaml
log_mapping: "{
'1': 'Eins',
'2': 'Zwei',
'3': 'Drei'
}"
log_mapping:
- 1: 'Eins'
- 2: 'Zwei'
- 3: 'Drei'
Attribut *log_rules*
====================

Über das **log_rules** Attribut kann festgelegt werden, welche zusätzliche Regeln für das Erzeugen des Log-Eintrages
anzuwenden sind. Das Attribut **log_rules** enthält dazu in einem String die Beschreibung eines dicts.
anzuwenden sind. Das Attribut **log_rules** enthält dazu eine Liste mit den folgenden möglichen Definitionen:
``lowlimit``, ``highlimit``, ``filter``, ``exclude``, ``itemvalue``

**Beispiel:**

.. code-block:: yaml
item:
type: num
log_rules: "{
'lowlimit' : -1.0,
'highlimit': 10.0,
'filter': [1, 2, 5],
'exclude': '.exclude_values',
'itemvalue': '.text'
}"
log_rules:
- 'lowlimit' : -1.0
- 'highlimit': 10.0
- 'filter': [1, 2, 5]
- 'exclude': '.exclude_values'
- 'itemvalue': '.text'
exclude_values:
type: list
Expand Down
14 changes: 9 additions & 5 deletions lib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@
KEY_ON_UPDATE = 'on_update'
KEY_ON_CHANGE = 'on_change'

KEY_LOG_CHANGE = 'log_change'
KEY_LOG_LEVEL = 'log_level'
KEY_LOG_TEXT = 'log_text'
KEY_LOG_CHANGE = 'log_change'
KEY_LOG_LEVEL = 'log_level'
KEY_LOG_TEXT = 'log_text'
KEY_LOG_MAPPING = 'log_mapping'
KEY_LOG_RULES = 'log_rules'

KEY_LOG_RULES = 'log_rules'
KEY_LOG_RULES_LOWLIMIT = 'lowlimit'
KEY_LOG_RULES_HIGHLIMIT = 'highlimit'
KEY_LOG_RULES_FILTER = 'filter'
KEY_LOG_RULES_EXCLUDE = 'exclude'
KEY_LOG_RULES_ITEMVALUE = 'itemvalue'
KEY_HYSTERESIS_INPUT = 'hysteresis_input'
KEY_HYSTERESIS_UPPER_THRESHOLD = 'hysteresis_upper_threshold'
KEY_HYSTERESIS_LOWER_THRESHOLD = 'hysteresis_lower_threshold'
Expand Down
27 changes: 21 additions & 6 deletions lib/item/item.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
KEY_EVAL, KEY_EVAL_TRIGGER, KEY_TRIGGER, KEY_CONDITION, KEY_NAME, KEY_DESCRIPTION, KEY_TYPE,
KEY_STRUCT, KEY_REMARK, KEY_INSTANCE, KEY_VALUE, KEY_INITVALUE, PLUGIN_PARSE_ITEM,
KEY_AUTOTIMER, KEY_ON_UPDATE, KEY_ON_CHANGE, KEY_LOG_CHANGE, KEY_LOG_LEVEL, KEY_LOG_TEXT,
KEY_LOG_MAPPING, KEY_LOG_RULES, KEY_THRESHOLD, KEY_EVAL_TRIGGER_ONLY,
KEY_ATTRIB_COMPAT, ATTRIB_COMPAT_V12, ATTRIB_COMPAT_LATEST, PLUGIN_REMOVE_ITEM,
KEY_HYSTERESIS_INPUT, KEY_HYSTERESIS_UPPER_THRESHOLD, KEY_HYSTERESIS_LOWER_THRESHOLD,
ATTRIBUTE_SEPARATOR)
KEY_LOG_MAPPING, KEY_LOG_RULES, KEY_LOG_RULES_LOWLIMIT, KEY_LOG_RULES_HIGHLIMIT,
KEY_LOG_RULES_FILTER, KEY_LOG_RULES_EXCLUDE, KEY_LOG_RULES_ITEMVALUE, KEY_THRESHOLD,
KEY_EVAL_TRIGGER_ONLY, KEY_ATTRIB_COMPAT, ATTRIB_COMPAT_V12, ATTRIB_COMPAT_LATEST,
PLUGIN_REMOVE_ITEM, KEY_HYSTERESIS_INPUT, KEY_HYSTERESIS_UPPER_THRESHOLD,
KEY_HYSTERESIS_LOWER_THRESHOLD, ATTRIBUTE_SEPARATOR)


from lib.utils import Utils
Expand Down Expand Up @@ -380,6 +381,8 @@ def __init__(self, smarthome, parent, path, config, items_instance=None):
#############################################################
for attr, value in config.items():
if not isinstance(value, dict):
log_rules_keys = [KEY_LOG_RULES_LOWLIMIT, KEY_LOG_RULES_HIGHLIMIT, KEY_LOG_RULES_EXCLUDE,
KEY_LOG_RULES_FILTER, KEY_LOG_RULES_ITEMVALUE]
if attr in [KEY_NAME, KEY_DESCRIPTION, KEY_TYPE, KEY_STRUCT, KEY_VALUE, KEY_INITVALUE, KEY_EVAL_TRIGGER_ONLY]:
if attr == KEY_INITVALUE:
attr = KEY_VALUE
Expand Down Expand Up @@ -441,14 +444,26 @@ def __init__(self, smarthome, parent, path, config, items_instance=None):
setattr(self, '_log_level_name', 'INFO')
setattr(self, '_log_level', logging.getLevelName('INFO'))
elif attr in [KEY_LOG_MAPPING]:
if value != '':
if isinstance(value, list):
try:
value_dict = {k: v for od in value for k, v in od.items()}
setattr(self, '_log_mapping', value_dict)
except Exception as e:
logger.warning(f"Item {self._path}: Invalid list data for attribute '{KEY_LOG_MAPPING}': {value} - Exception: {e}")
elif value != '':
try:
value_dict = ast.literal_eval(value)
setattr(self, '_log_mapping', value_dict)
except Exception as e:
logger.warning(f"Item {self._path}: Invalid data for attribute '{KEY_LOG_MAPPING}': {value} - Exception: {e}")
elif attr in [KEY_LOG_RULES]:
if value != '':
if isinstance(value, list):
try:
value_dict = {k: v for od in value for k, v in od.items() if k in log_rules_keys}
setattr(self, '_log_rules', value_dict)
except Exception as e:
logger.warning(f"Item {self._path}: Invalid list data for attribute '{KEY_LOG_RULES}': {value} - Exception: {e}")
elif value != '':
try:
value_dict = ast.literal_eval(value)
setattr(self, '_log_rules', value_dict)
Expand Down

0 comments on commit 1787ecd

Please sign in to comment.