notify: add ability to notify using a remote action

Signed-off-by: Pieter Willekens <me@pataar.nl>
This commit is contained in:
pataar 2022-08-30 22:27:47 +02:00 committed by Eric Callahan
parent 5d856b9c49
commit 21bb17743f
2 changed files with 37 additions and 3 deletions

View File

@ -1924,7 +1924,8 @@ events: *
body: "Your printer status has changed to {event_name}" body: "Your printer status has changed to {event_name}"
# The body of the notification. This option accepts Jinja2 templates. # The body of the notification. This option accepts Jinja2 templates.
# You can use {event_name} to print the current event trigger name. And {event_args} for # You can use {event_name} to print the current event trigger name. And {event_args} for
# the arguments that came with it. # the arguments that came with it. When using the notify functionality in a macro context, you can
# use {event_message} to print out your message.
title: title:
# The optional title of the notification. Just as the body, this option accepts Jinja2 templates. # The optional title of the notification. Just as the body, this option accepts Jinja2 templates.
attach: attach:
@ -1953,6 +1954,25 @@ url: tgram://{bottoken}/{ChatID}
events: error events: error
body: {event_args[1].message} body: {event_args[1].message}
attach: http://192.168.1.100/webcam/?action=snapshot attach: http://192.168.1.100/webcam/?action=snapshot
[notifier gcode_telegram]
url: tgram://{bottoken}/{ChatID}
events: gcode
body: {event_message}
attach: http://192.168.1.100/webcam/?action=snapshot
```
#### Notifying from Klipper
It is possible to invoke your notifiers from the Klippy host, this can be done
with a gcode_macro, such as:
```ini
# printer.cfg
[gcode_macro NOTIFY_FILAMENT_CHANGE]
gcode:
{action_call_remote_method("notify",
name="telegram",
message="Filament change needed!")}
``` ```
### `[simplyprint]` ### `[simplyprint]`

View File

@ -35,6 +35,7 @@ class Notifier:
prefix_sections = config.get_prefix_sections("notifier") prefix_sections = config.get_prefix_sections("notifier")
self.register_events(config) self.register_events(config)
self.register_remote_actions()
for section in prefix_sections: for section in prefix_sections:
cfg = config[section] cfg = config[section]
@ -53,6 +54,16 @@ class Notifier:
continue continue
self.notifiers[notifier.get_name()] = notifier self.notifiers[notifier.get_name()] = notifier
def register_remote_actions(self):
self.server.register_remote_method("notify", self.notify_action)
async def notify_action(self, name: str, message: str = ""):
if name not in self.notifiers:
raise self.server.error(f"Notifier '{name}' not found", 404)
notifier = self.notifiers[name]
await notifier.notify("remote_action", [], message)
def register_events(self, config: ConfigHelper): def register_events(self, config: ConfigHelper):
self.events["started"] = NotifierEvent( self.events["started"] = NotifierEvent(
@ -145,10 +156,13 @@ class NotifierInstance:
self.apprise.add(self.url) self.apprise.add(self.url)
async def notify(self, event_name: str, event_args: List) -> None: async def notify(
self, event_name: str, event_args: List, message: str = ""
) -> None:
context = { context = {
"event_name": event_name, "event_name": event_name,
"event_args": event_args "event_args": event_args,
"event_message": message
} }
rendered_title = ( rendered_title = (