Allow multiple recipients for XMPP (#45328)

* recipient is a string list instead of a single string now
* this does NOT break existing automations/etc using this component
This commit is contained in:
Andreas Oetken 2021-02-19 15:56:20 +01:00 committed by GitHub
parent 3e82509263
commit 250327fac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,7 +55,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_SENDER): cv.string, vol.Required(CONF_SENDER): cv.string,
vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_RECIPIENT): cv.string, vol.Required(CONF_RECIPIENT): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_RESOURCE, default=DEFAULT_RESOURCE): cv.string, vol.Optional(CONF_RESOURCE, default=DEFAULT_RESOURCE): cv.string,
vol.Optional(CONF_ROOM, default=""): cv.string, vol.Optional(CONF_ROOM, default=""): cv.string,
vol.Optional(CONF_TLS, default=True): cv.boolean, vol.Optional(CONF_TLS, default=True): cv.boolean,
@ -87,7 +87,7 @@ class XmppNotificationService(BaseNotificationService):
self._sender = sender self._sender = sender
self._resource = resource self._resource = resource
self._password = password self._password = password
self._recipient = recipient self._recipients = recipient
self._tls = tls self._tls = tls
self._verify = verify self._verify = verify
self._room = room self._room = room
@ -102,7 +102,7 @@ class XmppNotificationService(BaseNotificationService):
await async_send_message( await async_send_message(
f"{self._sender}/{self._resource}", f"{self._sender}/{self._resource}",
self._password, self._password,
self._recipient, self._recipients,
self._tls, self._tls,
self._verify, self._verify,
self._room, self._room,
@ -116,7 +116,7 @@ class XmppNotificationService(BaseNotificationService):
async def async_send_message( async def async_send_message(
sender, sender,
password, password,
recipient, recipients,
use_tls, use_tls,
verify_certificate, verify_certificate,
room, room,
@ -182,19 +182,21 @@ async def async_send_message(
url = await self.upload_file(timeout=timeout) url = await self.upload_file(timeout=timeout)
_LOGGER.info("Upload success") _LOGGER.info("Upload success")
if room: for recipient in recipients:
_LOGGER.info("Sending file to %s", room) if room:
message = self.Message(sto=room, stype="groupchat") _LOGGER.info("Sending file to %s", room)
else: message = self.Message(sto=room, stype="groupchat")
_LOGGER.info("Sending file to %s", recipient) else:
message = self.Message(sto=recipient, stype="chat") _LOGGER.info("Sending file to %s", recipient)
message = self.Message(sto=recipient, stype="chat")
message["body"] = url message["body"] = url
message["oob"]["url"] = url message["oob"]["url"] = url
try: try:
message.send() message.send()
except (IqError, IqTimeout, XMPPError) as ex: except (IqError, IqTimeout, XMPPError) as ex:
_LOGGER.error("Could not send image message %s", ex) _LOGGER.error("Could not send image message %s", ex)
if room:
break
except (IqError, IqTimeout, XMPPError) as ex: except (IqError, IqTimeout, XMPPError) as ex:
_LOGGER.error("Upload error, could not send message %s", ex) _LOGGER.error("Upload error, could not send message %s", ex)
except NotConnectedError as ex: except NotConnectedError as ex:
@ -336,8 +338,9 @@ async def async_send_message(
self.plugin["xep_0045"].join_muc(room, sender, wait=True) self.plugin["xep_0045"].join_muc(room, sender, wait=True)
self.send_message(mto=room, mbody=message, mtype="groupchat") self.send_message(mto=room, mbody=message, mtype="groupchat")
else: else:
_LOGGER.debug("Sending message to %s", recipient) for recipient in recipients:
self.send_message(mto=recipient, mbody=message, mtype="chat") _LOGGER.debug("Sending message to %s", recipient)
self.send_message(mto=recipient, mbody=message, mtype="chat")
except (IqError, IqTimeout, XMPPError) as ex: except (IqError, IqTimeout, XMPPError) as ex:
_LOGGER.error("Could not send text message %s", ex) _LOGGER.error("Could not send text message %s", ex)
except NotConnectedError as ex: except NotConnectedError as ex: