Skip to content

Commit d4af1f3

Browse files
committed
[Apprise] Improve integration
... by propagating the mqttwarn data dictionary into the Apprise plugin template arguments.
1 parent 9517ea1 commit d4af1f3

File tree

5 files changed

+79
-10
lines changed

5 files changed

+79
-10
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ in progress
1111
- Add versioning based on Git tags, using ``versioningit``. This will aid in
1212
telling PR- and nightly releases apart from GA releases when running
1313
``mqttwarn --version``.
14+
- Improve Apprise integration by propagating the mqttwarn data dictionary into
15+
the Apprise plugin template arguments.
1416

1517

1618
2022-11-21 0.31.0

mqttwarn/services/apprise_multi.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
__license__ = 'Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)'
66

77
# https://github.com/caronc/apprise#developers
8-
from urllib.parse import urlencode
98
from collections import OrderedDict
109

1110
import apprise
1211

12+
from mqttwarn.services.apprise_util import obtain_apprise_arguments, add_url_params, get_all_template_argument_names
13+
14+
APPRISE_ALL_ARGUMENT_NAMES = get_all_template_argument_names()
15+
1316

1417
def plugin(srv, item):
1518
"""Send a message to multiple Apprise plugins."""
@@ -39,6 +42,10 @@ def plugin(srv, item):
3942
# Collect URL parameters.
4043
params = OrderedDict()
4144

45+
# Obtain and apply all possible Ntfy parameters from data dictionary.
46+
params.update(obtain_apprise_arguments(item, APPRISE_ALL_ARGUMENT_NAMES))
47+
48+
# Apply addressee information.
4249
if "recipients" in address:
4350
to = ','.join(address["recipients"])
4451
if to:
@@ -49,10 +56,8 @@ def plugin(srv, item):
4956
if "sender_name" in address:
5057
params["name"] = address["sender_name"]
5158

52-
# Add notification services by server url.
53-
uri = baseuri
54-
if params:
55-
uri += '?' + urlencode(params)
59+
# Add parameters to Apprise notification URL.
60+
uri = add_url_params(baseuri, params)
5661
srv.logging.info("Adding notification to: {}".format(uri))
5762
apobj.add(uri)
5863

mqttwarn/services/apprise_single.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
__license__ = 'Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)'
66

77
# https://github.com/caronc/apprise#developers
8-
from urllib.parse import urlencode
98
from collections import OrderedDict
109

1110
import apprise
1211

12+
from mqttwarn.services.apprise_util import obtain_apprise_arguments, add_url_params, get_all_template_argument_names
13+
14+
APPRISE_ALL_ARGUMENT_NAMES = get_all_template_argument_names()
15+
1316

1417
def plugin(srv, item):
1518
"""Send a message to a single Apprise plugin."""
@@ -39,17 +42,20 @@ def plugin(srv, item):
3942

4043
# Collect URL parameters.
4144
params = OrderedDict()
45+
46+
# Obtain and apply all possible Ntfy parameters from data dictionary.
47+
params.update(obtain_apprise_arguments(item, APPRISE_ALL_ARGUMENT_NAMES))
48+
49+
# Apply addressee information.
4250
if sender:
4351
params["from"] = sender
4452
if to:
4553
params["to"] = to
4654
if sender_name:
4755
params["name"] = sender_name
4856

49-
# Add notification services by server url.
50-
uri = baseuri
51-
if params:
52-
uri += '?' + urlencode(params)
57+
# Add parameters to Apprise notification URL.
58+
uri = add_url_params(baseuri, params)
5359
apobj.add(uri)
5460

5561
# Submit notification.

mqttwarn/services/apprise_util.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
# (c) 2021-2023 The mqttwarn developers
3+
from __future__ import absolute_import
4+
5+
from functools import lru_cache
6+
from urllib.parse import urlparse, urlencode
7+
8+
from apprise import Apprise, ContentLocation
9+
10+
from mqttwarn.model import ProcessorItem
11+
12+
13+
@lru_cache(maxsize=None)
14+
def get_all_template_argument_names():
15+
"""
16+
Inquire all possible parameter names from all Apprise plugins.
17+
"""
18+
a = Apprise(asset=None, location=ContentLocation.LOCAL)
19+
results = a.details()
20+
plugin_infos = results['schemas']
21+
22+
all_arg_names = []
23+
for plugin_info in plugin_infos:
24+
arg_names = plugin_info["details"]["args"].keys()
25+
all_arg_names += arg_names
26+
27+
return sorted(set(all_arg_names))
28+
29+
30+
def obtain_apprise_arguments(item: ProcessorItem, arg_names: list) -> dict:
31+
"""
32+
Obtain eventual Apprise parameters from data dictionary.
33+
34+
https://github.com/caronc/apprise/wiki/Notify_ntfy#parameter-breakdown
35+
"""
36+
params = dict()
37+
for arg_name in arg_names:
38+
if isinstance(item.data, dict) and arg_name in item.data:
39+
params[arg_name] = item.data[arg_name]
40+
return params
41+
42+
43+
def add_url_params(url: str, params: dict) -> str:
44+
"""
45+
Serialize query parameter dictionary and add it to URL.
46+
"""
47+
url_parsed = urlparse(url)
48+
if params:
49+
seperator = "?"
50+
if url_parsed.query:
51+
seperator = "&"
52+
url += seperator + urlencode(params)
53+
return url

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import pytest
77

8+
# Needed to make Apprise not be mocked too much.
9+
from mqttwarn.services.apprise_util import get_all_template_argument_names # noqa:F401
10+
811
# Import custom fixtures.
912
from mqttwarn.testing.fixtures import mqttwarn_service as srv # noqa:F401
1013

0 commit comments

Comments
 (0)