Skip to content

Commit 0282865

Browse files
committed
Add priority string mappings and message blacklist
1 parent 13fa28f commit 0282865

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ notify:
1717
url: <gotify_url>
1818
token: <gotify_token>
1919
verify_ssl: true # optional, default true
20+
msg_blacklist: # optional, discard message if the text equals one of this strings
21+
- TTS
2022
```
2123
Replace `<gotify_url>` and `<gotify_token>` with the url of your Gotify instance and the application token to be used.
2224

custom_components/gotify/notify.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,35 @@
1616
BaseNotificationService,
1717
)
1818

19+
PRIORITIES = {
20+
"high": 10,
21+
"normal": 5,
22+
"default": 5,
23+
"low": 2,
24+
"min": 0,
25+
"none": 0
26+
}
1927
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_URL): cv.url, vol.Required(CONF_TOKEN): cv.string})
2028
_LOGGER = logging.getLogger(__name__)
2129

2230
def get_service(hass, config, discovery_info=None):
2331
url = config.get(CONF_URL)
2432
token = config.get(CONF_TOKEN)
2533
verify_ssl = config.get(CONF_VERIFY_SSL, True)
34+
msg_blacklist = config.get('msg_blacklist', [])
2635

2736
_LOGGER.info('Service created')
2837

29-
return HassAgentNotificationService(hass, url, token, verify_ssl)
38+
return HassAgentNotificationService(hass, url, token, verify_ssl, msg_blacklist)
3039

3140

3241
class HassAgentNotificationService(BaseNotificationService):
33-
def __init__(self, hass, url, token, verify_ssl):
42+
def __init__(self, hass, url, token, verify_ssl, msg_blacklist):
3443
self._url = url
3544
self._token = token
3645
self._hass = hass
3746
self._verify_ssl = verify_ssl
47+
self._msg_blacklist = msg_blacklist
3848

3949
if not self._url.endswith('/'):
4050
self._url += '/'
@@ -44,15 +54,22 @@ def send_request(self, data):
4454
return requests.post(self._url, headers={'X-Gotify-Key': self._token}, json=data, verify=self._verify_ssl, timeout=10)
4555

4656
async def async_send_message(self, message: str, **kwargs: Any):
57+
if message.strip() in self._msg_blacklist:
58+
return
59+
4760
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
4861
data = kwargs.get(ATTR_DATA, None)
4962
if data is None:
5063
data = dict()
5164

65+
priority = data.get('priority', 5)
66+
if type(priority) is str:
67+
priority = PRIORITIES.get(priority, 5)
68+
5269
payload = {
5370
'title': title,
5471
'message': message,
55-
'priority': data.get('priority', 5)
72+
'priority': priority
5673
}
5774
if 'extras' in data:
5875
payload['extras'] = data.get('extras')

0 commit comments

Comments
 (0)