From 56e76a326565a34153c3c13f8f793dfa64d67872 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 2 Sep 2020 19:43:07 -0500 Subject: [PATCH] Support reloading the smtp notify platform (#39530) * Support reloading the smtp notify platform * patch test --- homeassistant/components/smtp/__init__.py | 3 ++ homeassistant/components/smtp/notify.py | 4 ++ homeassistant/components/smtp/services.yaml | 2 + tests/components/smtp/test_notify.py | 54 +++++++++++++++++++++ tests/fixtures/smtp/configuration.yaml | 5 ++ 5 files changed, 68 insertions(+) create mode 100644 homeassistant/components/smtp/services.yaml create mode 100644 tests/fixtures/smtp/configuration.yaml diff --git a/homeassistant/components/smtp/__init__.py b/homeassistant/components/smtp/__init__.py index 5e7fb41c212..7461dd2b6c3 100644 --- a/homeassistant/components/smtp/__init__.py +++ b/homeassistant/components/smtp/__init__.py @@ -1 +1,4 @@ """The smtp component.""" + +DOMAIN = "smtp" +PLATFORMS = ["notify"] diff --git a/homeassistant/components/smtp/notify.py b/homeassistant/components/smtp/notify.py index 296a419c5d0..f7d3415525d 100644 --- a/homeassistant/components/smtp/notify.py +++ b/homeassistant/components/smtp/notify.py @@ -26,8 +26,11 @@ from homeassistant.const import ( CONF_USERNAME, ) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import setup_reload_service import homeassistant.util.dt as dt_util +from . import DOMAIN, PLATFORMS + _LOGGER = logging.getLogger(__name__) ATTR_IMAGES = "images" # optional embedded image file attachments @@ -67,6 +70,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( def get_service(hass, config, discovery_info=None): """Get the mail notification service.""" + setup_reload_service(hass, DOMAIN, PLATFORMS) mail_service = MailNotificationService( config.get(CONF_SERVER), config.get(CONF_PORT), diff --git a/homeassistant/components/smtp/services.yaml b/homeassistant/components/smtp/services.yaml new file mode 100644 index 00000000000..77ff7d22adf --- /dev/null +++ b/homeassistant/components/smtp/services.yaml @@ -0,0 +1,2 @@ +reload: + description: Reload smtp notify services. diff --git a/tests/components/smtp/test_notify.py b/tests/components/smtp/test_notify.py index d99ee82d4ef..6320614c059 100644 --- a/tests/components/smtp/test_notify.py +++ b/tests/components/smtp/test_notify.py @@ -1,8 +1,14 @@ """The tests for the notify smtp platform.""" +from os import path import re import unittest +from homeassistant import config as hass_config +import homeassistant.components.notify as notify +from homeassistant.components.smtp import DOMAIN from homeassistant.components.smtp.notify import MailNotificationService +from homeassistant.const import SERVICE_RELOAD +from homeassistant.setup import async_setup_component from tests.async_mock import patch from tests.common import get_test_home_assistant @@ -85,3 +91,51 @@ class TestNotifySmtp(unittest.TestCase): "Test msg", data={"html": html, "images": ["test.jpg"]} ) assert "Content-Type: multipart/related" in msg + + +async def test_reload_notify(hass): + """Verify we can reload the notify service.""" + + with patch( + "homeassistant.components.smtp.notify.MailNotificationService.connection_is_valid" + ): + assert await async_setup_component( + hass, + notify.DOMAIN, + { + notify.DOMAIN: [ + { + "name": DOMAIN, + "platform": DOMAIN, + "recipient": "test@example.com", + "sender": "test@example.com", + }, + ] + }, + ) + await hass.async_block_till_done() + + assert hass.services.has_service(notify.DOMAIN, DOMAIN) + + yaml_path = path.join( + _get_fixtures_base_path(), + "fixtures", + "smtp/configuration.yaml", + ) + with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path), patch( + "homeassistant.components.smtp.notify.MailNotificationService.connection_is_valid" + ): + await hass.services.async_call( + DOMAIN, + SERVICE_RELOAD, + {}, + blocking=True, + ) + await hass.async_block_till_done() + + assert not hass.services.has_service(notify.DOMAIN, DOMAIN) + assert hass.services.has_service(notify.DOMAIN, "smtp_reloaded") + + +def _get_fixtures_base_path(): + return path.dirname(path.dirname(path.dirname(__file__))) diff --git a/tests/fixtures/smtp/configuration.yaml b/tests/fixtures/smtp/configuration.yaml new file mode 100644 index 00000000000..03bed686909 --- /dev/null +++ b/tests/fixtures/smtp/configuration.yaml @@ -0,0 +1,5 @@ +notify: + - name: smtp_reloaded + platform: smtp + sender: test@example.com + recipient: test@example.com