Update notify to expect a list of string targets instead of a single … (#3548)
* Update notify to expect a list of string targets instead of a single string * Actually do the thing I set out to do * Fix notify.group test to expect an array of targets * REST platform will only use the first target in the list * Update notify platforms to expect a list of targets * Update notify services.yaml
This commit is contained in:
parent
c189c05676
commit
646eaccd4d
13 changed files with 33 additions and 47 deletions
|
@ -42,7 +42,7 @@ PLATFORM_SCHEMA = vol.Schema({
|
|||
NOTIFY_SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Required(ATTR_MESSAGE): cv.template,
|
||||
vol.Optional(ATTR_TITLE): cv.template,
|
||||
vol.Optional(ATTR_TARGET): cv.string,
|
||||
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(ATTR_DATA): dict,
|
||||
})
|
||||
|
||||
|
|
|
@ -79,9 +79,6 @@ class AWSLambda(BaseNotificationService):
|
|||
_LOGGER.info("At least 1 target is required")
|
||||
return
|
||||
|
||||
if not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
for target in targets:
|
||||
cleaned_kwargs = dict((k, v) for k, v in kwargs.items() if v)
|
||||
payload = {"message": message}
|
||||
|
|
|
@ -70,9 +70,6 @@ class AWSSNS(BaseNotificationService):
|
|||
_LOGGER.info("At least 1 target is required")
|
||||
return
|
||||
|
||||
if not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
message_attributes = {k: {"StringValue": json.dumps(v),
|
||||
"DataType": "String"}
|
||||
for k, v in kwargs.items() if v}
|
||||
|
|
|
@ -69,9 +69,6 @@ class AWSSQS(BaseNotificationService):
|
|||
_LOGGER.info("At least 1 target is required")
|
||||
return
|
||||
|
||||
if not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
for target in targets:
|
||||
cleaned_kwargs = dict((k, v) for k, v in kwargs.items() if v)
|
||||
message_body = {"message": message}
|
||||
|
|
|
@ -359,8 +359,6 @@ class HTML5NotificationService(BaseNotificationService):
|
|||
|
||||
if not targets:
|
||||
targets = self.registrations.keys()
|
||||
elif not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
for target in targets:
|
||||
info = self.registrations.get(target)
|
||||
|
|
|
@ -58,9 +58,6 @@ class MessageBirdNotificationService(BaseNotificationService):
|
|||
_LOGGER.error('No target specified.')
|
||||
return
|
||||
|
||||
if not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
for target in targets:
|
||||
try:
|
||||
self.client.message_create(self.sender,
|
||||
|
|
|
@ -87,10 +87,6 @@ class PushBulletNotificationService(BaseNotificationService):
|
|||
_LOGGER.info('Sent notification to self')
|
||||
return
|
||||
|
||||
# Make list if not so
|
||||
if not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
# Main loop, Process all targets specified
|
||||
for target in targets:
|
||||
try:
|
||||
|
|
|
@ -61,13 +61,15 @@ class PushoverNotificationService(BaseNotificationService):
|
|||
|
||||
data['title'] = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
|
||||
target = kwargs.get(ATTR_TARGET)
|
||||
if target is not None:
|
||||
data['device'] = target
|
||||
targets = kwargs.get(ATTR_TARGET)
|
||||
|
||||
try:
|
||||
self.pushover.send_message(message, **data)
|
||||
except ValueError as val_err:
|
||||
_LOGGER.error(str(val_err))
|
||||
except RequestError:
|
||||
_LOGGER.exception('Could not send pushover notification')
|
||||
for target in targets:
|
||||
if target is not None:
|
||||
data['device'] = target
|
||||
|
||||
try:
|
||||
self.pushover.send_message(message, **data)
|
||||
except ValueError as val_err:
|
||||
_LOGGER.error(str(val_err))
|
||||
except RequestError:
|
||||
_LOGGER.exception('Could not send pushover notification')
|
||||
|
|
|
@ -75,8 +75,10 @@ class RestNotificationService(BaseNotificationService):
|
|||
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)
|
||||
if self._target_param_name is not None and ATTR_TARGET in kwargs:
|
||||
# Target is a list as of 0.29 and we don't want to break existing
|
||||
# integrations, so just return the first target in the list.
|
||||
data[self._target_param_name] = kwargs[ATTR_TARGET][0]
|
||||
|
||||
if self._method == 'POST':
|
||||
response = requests.post(self._resource, data=data, timeout=10)
|
||||
|
|
|
@ -11,9 +11,9 @@ notify:
|
|||
example: 'Your Garage Door Friend'
|
||||
|
||||
target:
|
||||
description: Target of the notification. Optional depending on the platform
|
||||
description: An array of targets to send the notification to. Optional depending on the platform.
|
||||
example: platform specific
|
||||
|
||||
data:
|
||||
description: Extended information for notification. Optional depending on the platform
|
||||
description: Extended information for notification. Optional depending on the platform.
|
||||
example: platform specific
|
||||
|
|
|
@ -9,7 +9,7 @@ import logging
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
PLATFORM_SCHEMA, BaseNotificationService)
|
||||
ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService)
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY, CONF_USERNAME, CONF_ICON)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
@ -68,16 +68,19 @@ class SlackNotificationService(BaseNotificationService):
|
|||
"""Send a message to a user."""
|
||||
import slacker
|
||||
|
||||
channel = kwargs.get('target') or self._default_channel
|
||||
targets = kwargs.get(ATTR_TARGET, [self._default_channel])
|
||||
|
||||
data = kwargs.get('data')
|
||||
attachments = data.get('attachments') if data else None
|
||||
|
||||
try:
|
||||
self.slack.chat.post_message(channel, message,
|
||||
as_user=self._as_user,
|
||||
username=self._username,
|
||||
icon_emoji=self._icon,
|
||||
attachments=attachments,
|
||||
link_names=True)
|
||||
except slacker.Error as err:
|
||||
_LOGGER.error("Could not send slack notification. Error: %s", err)
|
||||
for target in targets:
|
||||
try:
|
||||
self.slack.chat.post_message(target, message,
|
||||
as_user=self._as_user,
|
||||
username=self._username,
|
||||
icon_emoji=self._icon,
|
||||
attachments=attachments,
|
||||
link_names=True)
|
||||
except slacker.Error as err:
|
||||
_LOGGER.error("Could not send slack notification. Error: %s",
|
||||
err)
|
||||
|
|
|
@ -57,9 +57,6 @@ class TwilioSMSNotificationService(BaseNotificationService):
|
|||
_LOGGER.info("At least 1 target is required")
|
||||
return
|
||||
|
||||
if not isinstance(targets, list):
|
||||
targets = [targets]
|
||||
|
||||
for target in targets:
|
||||
self.client.messages.create(to=target, body=message,
|
||||
from_=self.from_number)
|
||||
|
|
|
@ -74,7 +74,7 @@ class TestNotifyGroup(unittest.TestCase):
|
|||
data = self.events[-1].data
|
||||
assert {
|
||||
'message': 'Hello',
|
||||
'target': 'unnamed device',
|
||||
'target': ['unnamed device'],
|
||||
'title': 'Test notification',
|
||||
'data': {'hello': 'world', 'test': 'message'}
|
||||
} == data
|
||||
|
|
Loading…
Add table
Reference in a new issue