diff --git a/homeassistant/components/smtp/__init__.py b/homeassistant/components/smtp/__init__.py index eda28fcebe7a..8396bdb05aae 100644 --- a/homeassistant/components/smtp/__init__.py +++ b/homeassistant/components/smtp/__init__.py @@ -24,6 +24,7 @@ from homeassistant.util.ssl import create_client_context from .const import ( CONF_ENCRYPTION, + CONF_ENTRY, CONF_SENDER_NAME, CONF_SERVER, DEFAULT_TIMEOUT, @@ -57,12 +58,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmtpConfigEntry) -> bool Platform.NOTIFY, DOMAIN, { - **entry.data, CONF_NAME: entry.title, CONF_RECIPIENT: [ subentry.unique_id for subentry in entry.subentries.values() ], - **entry.options, + CONF_ENTRY: entry, }, {}, ) diff --git a/homeassistant/components/smtp/const.py b/homeassistant/components/smtp/const.py index 35ca923b413e..252a6571211b 100644 --- a/homeassistant/components/smtp/const.py +++ b/homeassistant/components/smtp/const.py @@ -26,3 +26,5 @@ DEFAULT_ENCRYPTION: Final = "starttls" ENCRYPTION_OPTIONS: Final = ["tls", "starttls", "none"] SUBENTRY_TYPE_RECIPIENT: Final = "recipient" + +CONF_ENTRY = "entry" diff --git a/homeassistant/components/smtp/notify.py b/homeassistant/components/smtp/notify.py index d3b740fb7a21..c6e5eba651c5 100644 --- a/homeassistant/components/smtp/notify.py +++ b/homeassistant/components/smtp/notify.py @@ -68,6 +68,7 @@ from .const import ( ATTR_IMAGES, ATTR_MEDIA_SOURCE, CONF_ENCRYPTION, + CONF_ENTRY, CONF_SENDER_NAME, CONF_SERVER, DEFAULT_DEBUG, @@ -136,12 +137,15 @@ async def async_get_service( ssl_context = ( await hass.async_add_executor_job(create_client_context) - if discovery_info[CONF_VERIFY_SSL] + if discovery_info[CONF_ENTRY].data[CONF_VERIFY_SSL] else None ) mail_service = MailNotificationService(discovery_info, ssl_context) + entry: SmtpConfigEntry = discovery_info[CONF_ENTRY] + if await hass.async_add_executor_job(mail_service.connection_is_valid): + entry.async_on_unload(mail_service.async_unregister_services) return mail_service return None @@ -345,16 +349,18 @@ class MailNotificationService(SmtpClient, BaseNotificationService): ) -> None: """Initialize the SMTP service.""" self.recipients = config[CONF_RECIPIENT] + entry: SmtpConfigEntry = config[CONF_ENTRY] + super().__init__( - server=config[CONF_SERVER], - port=config[CONF_PORT], - timeout=config.get(CONF_TIMEOUT, DEFAULT_TIMEOUT), - sender=config[CONF_SENDER], - encryption=config[CONF_ENCRYPTION], - username=config.get(CONF_USERNAME), - password=config.get(CONF_PASSWORD), - sender_name=config.get(CONF_SENDER_NAME), - verify_ssl=config[CONF_VERIFY_SSL], + server=entry.data[CONF_SERVER], + port=entry.data[CONF_PORT], + timeout=entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT), + sender=entry.data[CONF_SENDER], + encryption=entry.data[CONF_ENCRYPTION], + username=entry.data.get(CONF_USERNAME), + password=entry.data.get(CONF_PASSWORD), + sender_name=entry.data.get(CONF_SENDER_NAME), + verify_ssl=entry.data[CONF_VERIFY_SSL], ssl_context=ssl_context, ) diff --git a/tests/components/smtp/test_init.py b/tests/components/smtp/test_init.py index 2a728ca36af2..065bef21636a 100644 --- a/tests/components/smtp/test_init.py +++ b/tests/components/smtp/test_init.py @@ -45,10 +45,14 @@ async def test_entry_setup_unload( assert config_entry.state is ConfigEntryState.LOADED + assert hass.services.has_service(NOTIFY_DOMAIN, "home_assistant") + assert await hass.config_entries.async_unload(config_entry.entry_id) assert config_entry.state is ConfigEntryState.NOT_LOADED + assert not hass.services.has_service(NOTIFY_DOMAIN, "home_assistant") + @pytest.mark.parametrize( ("exception", "state"), diff --git a/tests/components/smtp/test_notify.py b/tests/components/smtp/test_notify.py index c617d37f5243..189cf7745230 100644 --- a/tests/components/smtp/test_notify.py +++ b/tests/components/smtp/test_notify.py @@ -28,24 +28,12 @@ from homeassistant.components.smtp.const import ( ATTR_FILENAME, ATTR_HTML, ATTR_MEDIA_SOURCE, - CONF_ENCRYPTION, - CONF_SENDER_NAME, - CONF_SERVER, + CONF_ENTRY, DOMAIN, ) from homeassistant.components.smtp.notify import MailNotificationService from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import ( - ATTR_ENTITY_ID, - CONF_PASSWORD, - CONF_PORT, - CONF_RECIPIENT, - CONF_SENDER, - CONF_TIMEOUT, - CONF_USERNAME, - CONF_VERIFY_SSL, - STATE_UNKNOWN, -) +from homeassistant.const import ATTR_ENTITY_ID, CONF_NAME, CONF_RECIPIENT, STATE_UNKNOWN from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import entity_registry as er, issue_registry as ir @@ -64,20 +52,15 @@ class MockSMTP(MailNotificationService): @pytest.fixture -def message(): +def message( + config_entry: MockConfigEntry, +): """Return MockSMTP object with test data.""" return MockSMTP( config={ - CONF_SERVER: "localhost", - CONF_PORT: 25, - CONF_TIMEOUT: 5, - CONF_SENDER: "test@test.com", - CONF_ENCRYPTION: 1, - CONF_USERNAME: "testuser", - CONF_PASSWORD: "testpass", + CONF_NAME: config_entry.title, + CONF_ENTRY: config_entry, CONF_RECIPIENT: ["recip1@example.com", "testrecip@test.com"], - CONF_SENDER_NAME: "Home Assistant", - CONF_VERIFY_SSL: True, }, ssl_context=create_client_context(), ) @@ -188,7 +171,7 @@ def test_send_text_message(hass: HomeAssistant, message) -> None: "Content-Transfer-Encoding: 7bit\n" "Subject: Home Assistant\n" "To: recip1@example.com,testrecip@test.com\n" - "From: Home Assistant \n" + "From: Home Assistant \n" "X-Mailer: Home Assistant\n" "Date: [^\n]+\n" "Message-Id: <[^@]+@[^>]+>\n"