From 1088341426eb2417195f59c4cf7740db119c6b34 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sun, 11 Feb 2024 14:31:34 +0100 Subject: [PATCH] add option to filter alerts by component --- conf.py | 1 + config.example.toml | 5 +++++ mqtt_queue.py | 23 +++++++++++++++++------ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/conf.py b/conf.py index d431420..e163d39 100644 --- a/conf.py +++ b/conf.py @@ -45,6 +45,7 @@ def load_and_validate_config(path): ), alerts=ConfigWrapper( brightness=max(int(config.get('alerts', {}).get('brightness', 255)), 10), + filters=sorted(config.get('alerts', {}).get('filters', set())), ), rainbow=ConfigWrapper( enable=bool(config.get('rainbow', {}).get('enable', True) is True), diff --git a/config.example.toml b/config.example.toml index 8e449f0..5f94264 100644 --- a/config.example.toml +++ b/config.example.toml @@ -30,6 +30,11 @@ universe = 1 # rainbow brightness (see below). brightness = 255 +# Filter by specific components. If this list is non-empty, the message +# will get shown if atleast one of these filters match. The filters are +# applied by using re.search() on the component part of the message. +filters = [] + [rainbow] # Wether to enable the rainbow 'no alerts' loop. If false, all other diff --git a/mqtt_queue.py b/mqtt_queue.py index 2120bc4..f567628 100644 --- a/mqtt_queue.py +++ b/mqtt_queue.py @@ -46,12 +46,23 @@ class MQTTQueue: text = re.sub(r'\<[a-z\/]+\>', '', data['msg']) - self.queue.put( - ( - data['level'].lower(), - data['component'], - text, + add_to_queue = True + if self.config.alerts.filters: + add_to_queue = False + for f in self.config.alerts.filters: + if re.search(f, data['component'], re.IGNORECASE): + add_to_queue = True + break # no point in searching further + + if add_to_queue: + self.queue.put( + ( + data['level'].lower(), + data['component'], + text, + ) ) - ) + else: + LOG.info(f'Ignoring message for {data["component"]} because it was filtered') except Exception as e: LOG.exception(msg.payload)