From 1787ecde395453415b7992528e97635859743cdb Mon Sep 17 00:00:00 2001 From: Onkel Andy Date: Sun, 20 Oct 2024 09:11:03 +0200 Subject: [PATCH] lib.item: log_mapping and log_rules are now expected to be stated as a list. However, putting a dict as string is still possible (non-breaking). --- .../items/standard_attribute/log_change.rst | 30 +++++++++---------- lib/constants.py | 14 +++++---- lib/item/item.py | 27 +++++++++++++---- 3 files changed, 44 insertions(+), 27 deletions(-) mode change 100644 => 100755 lib/item/item.py diff --git a/doc/user/source/referenz/items/standard_attribute/log_change.rst b/doc/user/source/referenz/items/standard_attribute/log_change.rst index 5dc53bbb50..df91ef0bb1 100644 --- a/doc/user/source/referenz/items/standard_attribute/log_change.rst +++ b/doc/user/source/referenz/items/standard_attribute/log_change.rst @@ -135,26 +135,25 @@ 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:** @@ -162,13 +161,12 @@ anzuwenden sind. Das Attribut **log_rules** enthält dazu in einem String die Be 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 diff --git a/lib/constants.py b/lib/constants.py index 11c6c358a3..fa80e71d03 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -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' diff --git a/lib/item/item.py b/lib/item/item.py old mode 100644 new mode 100755 index f69dea4e74..8dda7e028b --- a/lib/item/item.py +++ b/lib/item/item.py @@ -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 @@ -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 @@ -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)