From 9addff8cfbed80785f604b961fa6ca0a4563e30e Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Fri, 23 Aug 2024 16:14:42 -0400 Subject: [PATCH] mqtt:// support for publishing retain flag (#1185) --- apprise/plugins/mqtt.py | 18 ++++++++++++++++-- test/test_plugin_mqtt.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/apprise/plugins/mqtt.py b/apprise/plugins/mqtt.py index 22ab9bfb2d..4b21bf2883 100644 --- a/apprise/plugins/mqtt.py +++ b/apprise/plugins/mqtt.py @@ -204,10 +204,15 @@ class NotifyMQTT(NotifyBase): 'type': 'bool', 'default': False, }, + 'retain': { + 'name': _('Retain Messages'), + 'type': 'bool', + 'default': False, + }, }) def __init__(self, targets=None, version=None, qos=None, - client_id=None, session=None, **kwargs): + client_id=None, session=None, retain=None, **kwargs): """ Initialize MQTT Object """ @@ -230,6 +235,10 @@ def __init__(self, targets=None, version=None, qos=None, if session is None or not self.client_id \ else parse_bool(session) + # Our Retain Message Flag + self.retain = self.template_args['retain']['default'] \ + if retain is None else parse_bool(retain) + # Set up our Quality of Service (QoS) try: self.qos = self.template_args['qos']['default'] \ @@ -376,7 +385,7 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs): self.logger.debug('MQTT Payload: %s' % str(body)) result = self.client.publish( - topic, payload=body, qos=self.qos, retain=False) + topic, payload=body, qos=self.qos, retain=self.retain) if result.rc != mqtt.MQTT_ERR_SUCCESS: # Toggle our status @@ -456,6 +465,7 @@ def url(self, privacy=False, *args, **kwargs): 'version': self.version, 'qos': str(self.qos), 'session': 'yes' if self.session else 'no', + 'retain': 'yes' if self.retain else 'no', } if self.client_id: @@ -535,6 +545,10 @@ def parse_url(url): if 'session' in results['qsd'] and len(results['qsd']['session']): results['session'] = parse_bool(results['qsd']['session']) + # Message Retain Flag + if 'retain' in results['qsd'] and len(results['qsd']['retain']): + results['retain'] = parse_bool(results['qsd']['retain']) + # The MQTT Quality of Service to use if 'qos' in results['qsd'] and len(results['qsd']['qos']): results['qos'] = \ diff --git a/test/test_plugin_mqtt.py b/test/test_plugin_mqtt.py index 3948b95b29..78b8900def 100644 --- a/test/test_plugin_mqtt.py +++ b/test/test_plugin_mqtt.py @@ -320,6 +320,24 @@ def test_plugin_mqtt_session_client_id_success(mqtt_client_mock): assert re.search(r'my/topic', obj.url()) assert re.search(r'client_id=apprise', obj.url()) assert re.search(r'session=yes', obj.url()) + assert re.search(r'retain=no', obj.url()) + assert obj.notify(body="test=test") is True + + +def test_plugin_mqtt_retain(mqtt_client_mock): + """ + Verify handling of Retain Message Flag + """ + + obj = apprise.Apprise.instantiate( + 'mqtt://user@localhost/my/topic?retain=yes', + suppress_exceptions=False) + + assert isinstance(obj, NotifyMQTT) + assert obj.url().startswith('mqtt://user@localhost') + assert re.search(r'my/topic', obj.url()) + assert re.search(r'session=no', obj.url()) + assert re.search(r'retain=yes', obj.url()) assert obj.notify(body="test=test") is True