-
Notifications
You must be signed in to change notification settings - Fork 182
Modernize Desktop-Notifier #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,20 +8,30 @@ | |
|
|
||
| import json | ||
| import typing as t | ||
| import asyncio | ||
|
|
||
| from desktop_notifier import DesktopNotifier, Urgency, Button, ReplyField | ||
|
|
||
| from mqttwarn.model import Service, ProcessorItem, Struct | ||
|
|
||
|
|
||
| notify = DesktopNotifier() | ||
|
|
||
|
|
||
| def is_json(msg: t.Union[str, bytes]) -> bool: | ||
| try: | ||
| json.loads(msg) | ||
| except ValueError as e: | ||
| return False | ||
| except TypeError as e: | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| async def send_notification(message, title, sound=True): | ||
| await notify.send(message=message, title=title, sound=sound) | ||
|
|
||
|
|
||
| def plugin(srv: Service, item: ProcessorItem): | ||
| # Log | ||
| srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target) | ||
|
|
@@ -30,27 +40,36 @@ def plugin(srv: Service, item: ProcessorItem): | |
| config = item.config | ||
|
|
||
| # Play Sound ? | ||
| playSound = True | ||
| playSound = False | ||
| if isinstance(config, dict): | ||
| playSound = config.get('sound', True) | ||
| playSound = config.get('sound', False) | ||
|
|
||
| # Get Message | ||
| message = item.message | ||
| if message and is_json(message): | ||
| data = json.loads(message) | ||
| else: | ||
| if message is None: | ||
| raise ValueError("Message is required") | ||
| if is_json(message): | ||
| parsed = json.loads(message) | ||
| data = parsed if isinstance(parsed, dict) else { | ||
| "title": item.get("title", item.topic), | ||
| "message": parsed, | ||
| } | ||
| else: | ||
| try: | ||
| msg = t.cast(t.Dict, message).get('message') | ||
| except: | ||
| msg = str(message) | ||
|
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Handle dictionary payloads explicitly. For a dictionary without 🧰 Tools🪛 Ruff (0.15.20)[error] 56-56: Do not use bare (E722) Source: Linters/SAST tools |
||
| data = { | ||
| "title" : item.get('title', item.topic), | ||
| "message": message | ||
| "message": msg | ||
| } | ||
|
amotl marked this conversation as resolved.
|
||
|
|
||
| srv.logging.debug("Sending desktop notification") | ||
| message = str(data.get('message', '')) | ||
| title = str(data.get('title', '')) | ||
| try: | ||
| # Synchronous Notification (allows no callbacks in OSX) | ||
| # Asynchronous would require asyncio and require some changes to the plugin handler | ||
| notify.send_sync(message=message, title=title, sound=playSound) | ||
| # TODO: Fix issue where this keeps running until the notification is closed. | ||
| asyncio.run(send_notification(message=message, title=title, sound=playSound)) | ||
|
|
||
| except Exception as e: | ||
| srv.logging.warning("Invoking desktop notifier failed: %s" % e) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.