From 90e9216e9ac0b80041ef76952a4e40cf8ffb44a8 Mon Sep 17 00:00:00 2001 From: billsq Date: Mon, 28 Jun 2021 03:54:03 -0700 Subject: [PATCH] Add support for overriding SMTP recipient(s) in a service call (#47611) --- homeassistant/components/smtp/notify.py | 13 ++++++--- tests/components/smtp/test_notify.py | 35 +++++++++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/smtp/notify.py b/homeassistant/components/smtp/notify.py index f7d3415525d..29f0eb777ba 100644 --- a/homeassistant/components/smtp/notify.py +++ b/homeassistant/components/smtp/notify.py @@ -12,6 +12,7 @@ import voluptuous as vol from homeassistant.components.notify import ( ATTR_DATA, + ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, @@ -182,7 +183,11 @@ class MailNotificationService(BaseNotificationService): msg = _build_text_msg(message) msg["Subject"] = subject - msg["To"] = ",".join(self.recipients) + + recipients = kwargs.get(ATTR_TARGET) + if not recipients: + recipients = self.recipients + msg["To"] = recipients if isinstance(recipients, str) else ",".join(recipients) if self._sender_name: msg["From"] = f"{self._sender_name} <{self._sender}>" else: @@ -191,14 +196,14 @@ class MailNotificationService(BaseNotificationService): msg["Date"] = email.utils.format_datetime(dt_util.now()) msg["Message-Id"] = email.utils.make_msgid() - return self._send_email(msg) + return self._send_email(msg, recipients) - def _send_email(self, msg): + def _send_email(self, msg, recipients): """Send the message.""" mail = self.connect() for _ in range(self.tries): try: - mail.sendmail(self._sender, self.recipients, msg.as_string()) + mail.sendmail(self._sender, recipients, msg.as_string()) break except smtplib.SMTPServerDisconnected: _LOGGER.warning( diff --git a/tests/components/smtp/test_notify.py b/tests/components/smtp/test_notify.py index 46f8d0efd5f..5af1e5fcdbc 100644 --- a/tests/components/smtp/test_notify.py +++ b/tests/components/smtp/test_notify.py @@ -16,9 +16,9 @@ from homeassistant.setup import async_setup_component class MockSMTP(MailNotificationService): """Test SMTP object that doesn't need a working server.""" - def _send_email(self, msg): - """Just return string for testing.""" - return msg.as_string() + def _send_email(self, msg, recipients): + """Just return msg string and recipients for testing.""" + return msg.as_string(), recipients async def test_reload_notify(hass): @@ -140,7 +140,7 @@ def test_send_message(message_data, data, content_type, hass, message): """Verify if we can send messages of all types correctly.""" sample_email = "" with patch("email.utils.make_msgid", return_value=sample_email): - result = message.send_message(message_data, data=data) + result, _ = message.send_message(message_data, data=data) assert content_type in result @@ -162,5 +162,30 @@ def test_send_text_message(hass, message): sample_email = "" message_data = "Test msg" with patch("email.utils.make_msgid", return_value=sample_email): - result = message.send_message(message_data) + result, _ = message.send_message(message_data) assert re.search(expected, result) + + +@pytest.mark.parametrize( + "target", + [ + None, + "target@example.com", + ], + ids=[ + "Verify we can send email to default recipient.", + "Verify email recipient can be overwritten by target arg.", + ], +) +def test_send_target_message(target, hass, message): + """Verify if we can send email to correct recipient.""" + sample_email = "" + message_data = "Test msg" + with patch("email.utils.make_msgid", return_value=sample_email): + if not target: + expected_recipient = ["recip1@example.com", "testrecip@test.com"] + else: + expected_recipient = target + + _, recipient = message.send_message(message_data, target=target) + assert recipient == expected_recipient