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({
|
NOTIFY_SERVICE_SCHEMA = vol.Schema({
|
||||||
vol.Required(ATTR_MESSAGE): cv.template,
|
vol.Required(ATTR_MESSAGE): cv.template,
|
||||||
vol.Optional(ATTR_TITLE): 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,
|
vol.Optional(ATTR_DATA): dict,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -79,9 +79,6 @@ class AWSLambda(BaseNotificationService):
|
||||||
_LOGGER.info("At least 1 target is required")
|
_LOGGER.info("At least 1 target is required")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
cleaned_kwargs = dict((k, v) for k, v in kwargs.items() if v)
|
cleaned_kwargs = dict((k, v) for k, v in kwargs.items() if v)
|
||||||
payload = {"message": message}
|
payload = {"message": message}
|
||||||
|
|
|
@ -70,9 +70,6 @@ class AWSSNS(BaseNotificationService):
|
||||||
_LOGGER.info("At least 1 target is required")
|
_LOGGER.info("At least 1 target is required")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
message_attributes = {k: {"StringValue": json.dumps(v),
|
message_attributes = {k: {"StringValue": json.dumps(v),
|
||||||
"DataType": "String"}
|
"DataType": "String"}
|
||||||
for k, v in kwargs.items() if v}
|
for k, v in kwargs.items() if v}
|
||||||
|
|
|
@ -69,9 +69,6 @@ class AWSSQS(BaseNotificationService):
|
||||||
_LOGGER.info("At least 1 target is required")
|
_LOGGER.info("At least 1 target is required")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
cleaned_kwargs = dict((k, v) for k, v in kwargs.items() if v)
|
cleaned_kwargs = dict((k, v) for k, v in kwargs.items() if v)
|
||||||
message_body = {"message": message}
|
message_body = {"message": message}
|
||||||
|
|
|
@ -359,8 +359,6 @@ class HTML5NotificationService(BaseNotificationService):
|
||||||
|
|
||||||
if not targets:
|
if not targets:
|
||||||
targets = self.registrations.keys()
|
targets = self.registrations.keys()
|
||||||
elif not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
info = self.registrations.get(target)
|
info = self.registrations.get(target)
|
||||||
|
|
|
@ -58,9 +58,6 @@ class MessageBirdNotificationService(BaseNotificationService):
|
||||||
_LOGGER.error('No target specified.')
|
_LOGGER.error('No target specified.')
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
try:
|
try:
|
||||||
self.client.message_create(self.sender,
|
self.client.message_create(self.sender,
|
||||||
|
|
|
@ -87,10 +87,6 @@ class PushBulletNotificationService(BaseNotificationService):
|
||||||
_LOGGER.info('Sent notification to self')
|
_LOGGER.info('Sent notification to self')
|
||||||
return
|
return
|
||||||
|
|
||||||
# Make list if not so
|
|
||||||
if not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
# Main loop, Process all targets specified
|
# Main loop, Process all targets specified
|
||||||
for target in targets:
|
for target in targets:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -61,13 +61,15 @@ class PushoverNotificationService(BaseNotificationService):
|
||||||
|
|
||||||
data['title'] = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
data['title'] = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||||
|
|
||||||
target = kwargs.get(ATTR_TARGET)
|
targets = kwargs.get(ATTR_TARGET)
|
||||||
if target is not None:
|
|
||||||
data['device'] = target
|
|
||||||
|
|
||||||
try:
|
for target in targets:
|
||||||
self.pushover.send_message(message, **data)
|
if target is not None:
|
||||||
except ValueError as val_err:
|
data['device'] = target
|
||||||
_LOGGER.error(str(val_err))
|
|
||||||
except RequestError:
|
try:
|
||||||
_LOGGER.exception('Could not send pushover notification')
|
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,
|
data[self._title_param_name] = kwargs.get(ATTR_TITLE,
|
||||||
ATTR_TITLE_DEFAULT)
|
ATTR_TITLE_DEFAULT)
|
||||||
|
|
||||||
if self._target_param_name is not None:
|
if self._target_param_name is not None and ATTR_TARGET in kwargs:
|
||||||
data[self._target_param_name] = kwargs.get(ATTR_TARGET)
|
# 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':
|
if self._method == 'POST':
|
||||||
response = requests.post(self._resource, data=data, timeout=10)
|
response = requests.post(self._resource, data=data, timeout=10)
|
||||||
|
|
|
@ -11,9 +11,9 @@ notify:
|
||||||
example: 'Your Garage Door Friend'
|
example: 'Your Garage Door Friend'
|
||||||
|
|
||||||
target:
|
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
|
example: platform specific
|
||||||
|
|
||||||
data:
|
data:
|
||||||
description: Extended information for notification. Optional depending on the platform
|
description: Extended information for notification. Optional depending on the platform.
|
||||||
example: platform specific
|
example: platform specific
|
||||||
|
|
|
@ -9,7 +9,7 @@ import logging
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
PLATFORM_SCHEMA, BaseNotificationService)
|
ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY, CONF_USERNAME, CONF_ICON)
|
CONF_API_KEY, CONF_USERNAME, CONF_ICON)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
@ -68,16 +68,19 @@ class SlackNotificationService(BaseNotificationService):
|
||||||
"""Send a message to a user."""
|
"""Send a message to a user."""
|
||||||
import slacker
|
import slacker
|
||||||
|
|
||||||
channel = kwargs.get('target') or self._default_channel
|
targets = kwargs.get(ATTR_TARGET, [self._default_channel])
|
||||||
|
|
||||||
data = kwargs.get('data')
|
data = kwargs.get('data')
|
||||||
attachments = data.get('attachments') if data else None
|
attachments = data.get('attachments') if data else None
|
||||||
|
|
||||||
try:
|
for target in targets:
|
||||||
self.slack.chat.post_message(channel, message,
|
try:
|
||||||
as_user=self._as_user,
|
self.slack.chat.post_message(target, message,
|
||||||
username=self._username,
|
as_user=self._as_user,
|
||||||
icon_emoji=self._icon,
|
username=self._username,
|
||||||
attachments=attachments,
|
icon_emoji=self._icon,
|
||||||
link_names=True)
|
attachments=attachments,
|
||||||
except slacker.Error as err:
|
link_names=True)
|
||||||
_LOGGER.error("Could not send slack notification. Error: %s", err)
|
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")
|
_LOGGER.info("At least 1 target is required")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(targets, list):
|
|
||||||
targets = [targets]
|
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
self.client.messages.create(to=target, body=message,
|
self.client.messages.create(to=target, body=message,
|
||||||
from_=self.from_number)
|
from_=self.from_number)
|
||||||
|
|
|
@ -74,7 +74,7 @@ class TestNotifyGroup(unittest.TestCase):
|
||||||
data = self.events[-1].data
|
data = self.events[-1].data
|
||||||
assert {
|
assert {
|
||||||
'message': 'Hello',
|
'message': 'Hello',
|
||||||
'target': 'unnamed device',
|
'target': ['unnamed device'],
|
||||||
'title': 'Test notification',
|
'title': 'Test notification',
|
||||||
'data': {'hello': 'world', 'test': 'message'}
|
'data': {'hello': 'world', 'test': 'message'}
|
||||||
} == data
|
} == data
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue