From b6cb946ae23a2724e7bb15a7f917b9e8d41e0aa1 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Thu, 14 Jul 2022 12:29:12 -0700 Subject: [PATCH] Add json support for MQTT notifications --- plexpy/notification_handler.py | 14 ++++++++------ plexpy/notifiers.py | 28 ++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 747a860f..97e0ba1a 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -402,7 +402,8 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data body=body_string, notify_action=notify_action, parameters=parameters, - agent_id=notifier_config['agent_id']) + agent_id=notifier_config['agent_id'], + as_json=notifier_config['config'].get('as_json', False)) # Set the notification state in the db notification_id = set_notify_state(session=stream_data or timeline_data, @@ -1259,7 +1260,7 @@ def build_server_notify_params(notify_action=None, **kwargs): return available_params -def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None, test=False): +def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None, test=False, as_json=False): # Default subject and body text if agent_id == 15: default_subject = default_body = '' @@ -1320,21 +1321,22 @@ def build_notify_text(subject='', body='', notify_action=None, parameters=None, logger.exception("Tautulli NotificationHandler :: Unable to parse custom script arguments: %s. Using fallback." % e) script_args = [] - elif agent_id == 25: + elif agent_id == 25 or as_json: + agent = 'MQTT' if agent_id == 23 else 'webhook' if subject: try: subject = json.loads(subject) except ValueError as e: - logger.error("Tautulli NotificationHandler :: Unable to parse custom webhook json header data: %s. Using fallback." % e) + logger.error("Tautulli NotificationHandler :: Unable to parse custom %s json header data: %s. Using fallback." % (agent, e)) subject = '' if subject: try: subject = json.dumps(helpers.traverse_map(subject, str_formatter)) except LookupError as e: - logger.error("Tautulli NotificationHandler :: Unable to parse parameter %s in webhook header data. Using fallback." % e) + logger.error("Tautulli NotificationHandler :: Unable to parse parameter %s in %s header data. Using fallback." % (e, agent)) subject = '' except Exception as e: - logger.exception("Tautulli NotificationHandler :: Unable to parse custom webhook header data: %s. Using fallback." % e) + logger.exception("Tautulli NotificationHandler :: Unable to parse custom %s header data: %s. Using fallback." % (agent, e)) subject = '' if body: diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 7c9627f0..b7ee4f17 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -2367,17 +2367,25 @@ class MQTT(Notifier): 'topic': '', 'qos': 1, 'retain': 0, - 'keep_alive': 60 + 'keep_alive': 60, + 'as_json': 0 } def agent_notify(self, subject='', body='', action='', **kwargs): - if not self.config['topic']: + topic = self.config['topic'] + if not topic: logger.error("Tautulli Notifiers :: MQTT topic not specified.") return + topic = topic.format(**kwargs.get('parameters', {})) + + if self.config['as_json']: + subject = json.loads(subject) + body = json.loads(body) + data = {'subject': subject, 'body': body, - 'topic': self.config['topic']} + 'topic': topic} auth = {} if self.config['username']: @@ -2390,7 +2398,7 @@ class MQTT(Notifier): logger.info("Tautulli Notifiers :: Sending {name} notification...".format(name=self.NAME)) paho.mqtt.publish.single( - self.config['topic'], payload=json.dumps(data), qos=self.config['qos'], retain=bool(self.config['retain']), + topic, payload=json.dumps(data), qos=self.config['qos'], retain=bool(self.config['retain']), hostname=self.config['broker'], port=self.config['port'], client_id=self.config['clientid'], keepalive=self.config['keep_alive'], auth=auth or None, protocol=protocol ) @@ -2443,7 +2451,9 @@ class MQTT(Notifier): {'label': 'Topic', 'value': self.config['topic'], 'name': 'mqtt_topic', - 'description': 'The topic to publish notifications to.', + 'description': 'The topic to publish notifications to. You can include' + ' notification parameters' + ' to be substituted.', 'input_type': 'text' }, {'label': 'Quality of Service', @@ -2467,7 +2477,13 @@ class MQTT(Notifier): 'name': 'mqtt_keep_alive', 'description': 'Maximum period in seconds before timing out the connection with the broker.', 'input_type': 'number' - } + }, + {'label': 'Send Message as JSON', + 'value': self.config['as_json'], + 'name': 'mqtt_as_json', + 'description': 'Parse and send the subject and body as JSON instead of as a raw string.', + 'input_type': 'checkbox' + }, ] return config_option