Skip to content

Commit f4e6e08

Browse files
committed
Allow dispatching of messages with no target address information
Background: Some service plugins, like `apprise-json` or `apprise-discord`, don't need/accept any additional information about single or multiple recipients at all. So, this patch makes the machinery accept items without any attached or resolved recipient information, i.e. without `target` and `addrs` attributes.
1 parent 338477e commit f4e6e08

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ mqttwarn changelog
66
in progress
77
===========
88

9+
- Allow dispatching of messages with no target address information.
10+
This helps for service plugins like Apprise to make the configuration
11+
snippet more compact. Now, service configurations can omit the ``targets``
12+
option altogether.
13+
914

1015
2021-10-17 0.27.0
1116
=================

HANDBOOK.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,6 @@ targets = {
543543
[config:apprise-json]
544544
module = 'apprise'
545545
baseuri = 'json://localhost:1234/mqtthook'
546-
; Surrogate for satisfying machinery.
547-
targets = {
548-
'n/a' : [''],
549-
}
550546
551547
[apprise-test]
552548
topic = apprise/#

mqttwarn/context.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,12 @@ def get_service_config(self, service):
102102
return dict(config)
103103

104104
def get_service_targets(self, service):
105+
# Be more graceful with jobs w/o any target address information (2021-10-18 [amo]).
105106
try:
106-
targets = self.config.getdict('config:' + service, 'targets')
107-
if type(targets) != dict:
108-
logger.error("No targets for service `%s'" % service)
107+
targets = self.config.getdict('config:' + service, 'targets') or [None]
108+
return targets
109109
except:
110-
logger.error("No targets for service `%s'" % service)
111-
112-
if targets is None:
113-
return {}
114-
115-
return dict(targets)
110+
logger.exception("Unable to access targets for service `%s'" % service)
116111

117112

118113
@attr.s

mqttwarn/core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,18 @@ def processor(worker_id=None):
454454
q_in.task_done()
455455
continue
456456

457+
# Be more graceful with jobs w/o any target address information (2021-10-18 [amo]).
458+
if target is None:
459+
addrs = []
460+
else:
461+
addrs = service_targets[target]
462+
457463
item = {
458464
'service' : service,
459465
'section' : section,
460466
'target' : target,
461467
'config' : service_config,
462-
'addrs' : service_targets[target],
468+
'addrs' : addrs,
463469
'topic' : topic,
464470
'payload' : job.payload,
465471
'data' : None,

mqttwarn/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# (c) 2021 The mqttwarn developers
3-
from dataclasses import dataclass
3+
from dataclasses import dataclass, field
44
from typing import Dict, List, Union
55

66

@@ -13,7 +13,7 @@ class ProcessorItem:
1313
service: str = None
1414
target: str = None
1515
config: Dict = None
16-
addrs: List[str] = None
16+
addrs: List[str] = field(default_factory=list)
1717
priority: int = None
1818
topic: str = None
1919
title: str = None

0 commit comments

Comments
 (0)