Refactor notification titles to allow for them to be None, this also includes a change in Telegram to only include the title if it's present, and to use a Markdown parse mode for messages (#3100)
parent
5036bb0bc6
commit
0bcfb65a30
|
@ -41,7 +41,7 @@ PLATFORM_SCHEMA = vol.Schema({
|
|||
|
||||
NOTIFY_SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Required(ATTR_MESSAGE): cv.template,
|
||||
vol.Optional(ATTR_TITLE, default=ATTR_TITLE_DEFAULT): cv.string,
|
||||
vol.Optional(ATTR_TITLE): cv.string,
|
||||
vol.Optional(ATTR_TARGET): cv.string,
|
||||
vol.Optional(ATTR_DATA): dict,
|
||||
})
|
||||
|
|
|
@ -11,7 +11,7 @@ import voluptuous as vol
|
|||
from homeassistant.const import (
|
||||
CONF_PLATFORM, CONF_NAME)
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, ATTR_TARGET, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_TARGET, BaseNotificationService)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
REQUIREMENTS = ["boto3==1.3.1"]
|
||||
|
@ -76,5 +76,6 @@ class AWSSNS(BaseNotificationService):
|
|||
for k, v in kwargs.items() if v}
|
||||
for target in targets:
|
||||
self.client.publish(TargetArn=target, Message=message,
|
||||
Subject=kwargs.get(ATTR_TITLE),
|
||||
Subject=kwargs.get(ATTR_TITLE,
|
||||
ATTR_TITLE_DEFAULT),
|
||||
MessageAttributes=message_attributes)
|
||||
|
|
|
@ -11,7 +11,7 @@ import voluptuous as vol
|
|||
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, PLATFORM_SCHEMA, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
|
||||
from homeassistant.const import CONF_FILENAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -47,7 +47,7 @@ class FileNotificationService(BaseNotificationService):
|
|||
with open(self.filepath, 'a') as file:
|
||||
if os.stat(self.filepath).st_size == 0:
|
||||
title = '{} notifications (Log started: {})\n{}\n'.format(
|
||||
kwargs.get(ATTR_TITLE),
|
||||
kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
|
||||
dt_util.utcnow().isoformat(),
|
||||
'-' * 80)
|
||||
file.write(title)
|
||||
|
|
|
@ -8,7 +8,7 @@ import logging
|
|||
import os
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, BaseNotificationService)
|
||||
|
||||
REQUIREMENTS = ['gntp==1.0.3']
|
||||
|
||||
|
@ -59,5 +59,6 @@ class GNTPNotificationService(BaseNotificationService):
|
|||
|
||||
def send_message(self, message="", **kwargs):
|
||||
"""Send a message to a user."""
|
||||
self.gntp.notify(noteType="Notification", title=kwargs.get(ATTR_TITLE),
|
||||
self.gntp.notify(noteType="Notification",
|
||||
title=kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
|
||||
description=message)
|
||||
|
|
|
@ -18,8 +18,8 @@ from homeassistant.const import (HTTP_BAD_REQUEST, HTTP_INTERNAL_SERVER_ERROR,
|
|||
HTTP_UNAUTHORIZED, URL_ROOT)
|
||||
from homeassistant.util import ensure_unique_string
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TARGET, ATTR_TITLE, ATTR_DATA, BaseNotificationService,
|
||||
PLATFORM_SCHEMA)
|
||||
ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA,
|
||||
BaseNotificationService, PLATFORM_SCHEMA)
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components.frontend import add_manifest_json_key
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
@ -332,7 +332,7 @@ class HTML5NotificationService(BaseNotificationService):
|
|||
'icon': '/static/icons/favicon-192x192.png',
|
||||
ATTR_TAG: tag,
|
||||
'timestamp': (timestamp*1000), # Javascript ms since epoch
|
||||
ATTR_TITLE: kwargs.get(ATTR_TITLE)
|
||||
ATTR_TITLE: kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
}
|
||||
|
||||
data = kwargs.get(ATTR_DATA)
|
||||
|
|
|
@ -10,7 +10,7 @@ import logging
|
|||
import requests
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, DOMAIN, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
|
@ -70,7 +70,7 @@ class InstapushNotificationService(BaseNotificationService):
|
|||
|
||||
def send_message(self, message="", **kwargs):
|
||||
"""Send a message to a user."""
|
||||
title = kwargs.get(ATTR_TITLE)
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
data = {"event": self._event,
|
||||
"trackers": {self._tracker: title + " : " + message}}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ https://home-assistant.io/components/notify.join/
|
|||
import logging
|
||||
import voluptuous as vol
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_DATA, ATTR_TITLE, BaseNotificationService)
|
||||
ATTR_DATA, ATTR_TITLE, ATTR_TITLE_DEFAULT, BaseNotificationService)
|
||||
from homeassistant.const import CONF_PLATFORM, CONF_NAME, CONF_API_KEY
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -52,7 +52,7 @@ class JoinNotificationService(BaseNotificationService):
|
|||
def send_message(self, message="", **kwargs):
|
||||
"""Send a message to a user."""
|
||||
from pyjoin import send_notification
|
||||
title = kwargs.get(ATTR_TITLE)
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
data = kwargs.get(ATTR_DATA) or {}
|
||||
send_notification(device_id=self._device_id,
|
||||
text=message,
|
||||
|
|
|
@ -10,7 +10,7 @@ import xml.etree.ElementTree as ET
|
|||
import requests
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, DOMAIN, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
|
@ -49,7 +49,7 @@ class NmaNotificationService(BaseNotificationService):
|
|||
data = {
|
||||
"apikey": self._api_key,
|
||||
"application": 'home-assistant',
|
||||
"event": kwargs.get(ATTR_TITLE),
|
||||
"event": kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
|
||||
"description": message,
|
||||
"priority": 0,
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ https://home-assistant.io/components/notify.pushbullet/
|
|||
import logging
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TARGET, ATTR_TITLE, BaseNotificationService)
|
||||
ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT, BaseNotificationService)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -73,7 +73,7 @@ class PushBulletNotificationService(BaseNotificationService):
|
|||
call which doesn't require a push object.
|
||||
"""
|
||||
targets = kwargs.get(ATTR_TARGET)
|
||||
title = kwargs.get(ATTR_TITLE)
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
refreshed = False
|
||||
|
||||
if not targets:
|
||||
|
|
|
@ -7,7 +7,7 @@ https://home-assistant.io/components/notify.pushetta/
|
|||
import logging
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, DOMAIN, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
|
@ -52,6 +52,6 @@ class PushettaNotificationService(BaseNotificationService):
|
|||
|
||||
def send_message(self, message="", **kwargs):
|
||||
"""Send a message to a user."""
|
||||
title = kwargs.get(ATTR_TITLE)
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
self.pushetta.pushMessage(self._channel_name,
|
||||
"{} {}".format(title, message))
|
||||
|
|
|
@ -9,7 +9,8 @@ import logging
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, ATTR_TARGET, ATTR_DATA, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_TARGET, ATTR_DATA,
|
||||
BaseNotificationService)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -56,7 +57,7 @@ class PushoverNotificationService(BaseNotificationService):
|
|||
# Make a copy and use empty dict if necessary
|
||||
data = dict(kwargs.get(ATTR_DATA) or {})
|
||||
|
||||
data['title'] = kwargs.get(ATTR_TITLE)
|
||||
data['title'] = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
|
||||
target = kwargs.get(ATTR_TARGET)
|
||||
if target is not None:
|
||||
|
|
|
@ -10,7 +10,8 @@ import requests
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TARGET, ATTR_TITLE, BaseNotificationService, PLATFORM_SCHEMA)
|
||||
ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT, BaseNotificationService,
|
||||
PLATFORM_SCHEMA)
|
||||
from homeassistant.const import (CONF_RESOURCE, CONF_METHOD, CONF_NAME)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -71,7 +72,8 @@ class RestNotificationService(BaseNotificationService):
|
|||
}
|
||||
|
||||
if self._title_param_name is not None:
|
||||
data[self._title_param_name] = kwargs.get(ATTR_TITLE)
|
||||
data[self._title_param_name] = kwargs.get(ATTR_TITLE,
|
||||
ATTR_TITLE_DEFAULT)
|
||||
|
||||
if self._target_param_name is not None:
|
||||
data[self._target_param_name] = kwargs.get(ATTR_TARGET)
|
||||
|
|
|
@ -7,7 +7,7 @@ https://home-assistant.io/components/notify.sendgrid/
|
|||
import logging
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, DOMAIN, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
REQUIREMENTS = ['sendgrid==3.2.10']
|
||||
|
@ -44,7 +44,7 @@ class SendgridNotificationService(BaseNotificationService):
|
|||
|
||||
def send_message(self, message='', **kwargs):
|
||||
"""Send an email to a user via SendGrid."""
|
||||
subject = kwargs.get(ATTR_TITLE)
|
||||
subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
|
||||
data = {
|
||||
"personalizations": [
|
||||
|
|
|
@ -14,7 +14,8 @@ import voluptuous as vol
|
|||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, ATTR_DATA, PLATFORM_SCHEMA, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA,
|
||||
BaseNotificationService)
|
||||
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, CONF_PORT)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -120,7 +121,7 @@ class MailNotificationService(BaseNotificationService):
|
|||
Will send plain text normally, or will build a multipart HTML message
|
||||
with inline image attachments if images config is defined.
|
||||
"""
|
||||
subject = kwargs.get(ATTR_TITLE)
|
||||
subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
data = kwargs.get(ATTR_DATA)
|
||||
|
||||
if data:
|
||||
|
|
|
@ -7,7 +7,7 @@ https://home-assistant.io/components/notify.syslog/
|
|||
import logging
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, DOMAIN, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -80,7 +80,7 @@ class SyslogNotificationService(BaseNotificationService):
|
|||
"""Send a message to a user."""
|
||||
import syslog
|
||||
|
||||
title = kwargs.get(ATTR_TITLE)
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
|
||||
syslog.openlog(title, self._option, self._facility)
|
||||
syslog.syslog(self._priority, message)
|
||||
|
|
|
@ -106,10 +106,20 @@ class TelegramNotificationService(BaseNotificationService):
|
|||
elif data is not None and ATTR_DOCUMENT in data:
|
||||
return self.send_document(data.get(ATTR_DOCUMENT))
|
||||
|
||||
text = ''
|
||||
|
||||
if title:
|
||||
text = '{} {}'.format(title, message)
|
||||
else:
|
||||
text = message
|
||||
|
||||
parse_mode = telegram.parsemode.ParseMode.MARKDOWN
|
||||
|
||||
# send message
|
||||
try:
|
||||
self.bot.sendMessage(chat_id=self._chat_id,
|
||||
text=title + " " + message)
|
||||
text=text,
|
||||
parse_mode=parse_mode)
|
||||
except telegram.error.TelegramError:
|
||||
_LOGGER.exception("Error sending message.")
|
||||
return
|
||||
|
|
|
@ -7,7 +7,7 @@ https://home-assistant.io/components/notify.xmpp/
|
|||
import logging
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_TITLE, DOMAIN, BaseNotificationService)
|
||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
REQUIREMENTS = ['sleekxmpp==1.3.1',
|
||||
|
@ -45,7 +45,7 @@ class XmppNotificationService(BaseNotificationService):
|
|||
|
||||
def send_message(self, message="", **kwargs):
|
||||
"""Send a message to a user."""
|
||||
title = kwargs.get(ATTR_TITLE)
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
data = "{}: {}".format(title, message) if title else message
|
||||
|
||||
send_message(self._sender + '/home-assistant', self._password,
|
||||
|
|
|
@ -37,7 +37,7 @@ class TestNotifySmtp(unittest.TestCase):
|
|||
expected = ('Content-Type: text/plain; charset="us-ascii"\n'
|
||||
'MIME-Version: 1.0\n'
|
||||
'Content-Transfer-Encoding: 7bit\n'
|
||||
'Subject: \n'
|
||||
'Subject: Home Assistant\n'
|
||||
'To: testrecip@test.com\n'
|
||||
'From: test@test.com\n'
|
||||
'X-Mailer: HomeAssistant\n'
|
||||
|
|
Loading…
Reference in New Issue