home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.29k stars 30.61k forks source link

SMTP Notification reported as broken during startup when SMTP server is offline #124878

Open ptr727 opened 2 months ago

ptr727 commented 2 months ago

The problem

On startup, HA reports that automations that use the SMTP notification are broken, if the SMTP server is not currently reachable.
On clicking fix, the issue goes away by itself without doing anything.

Screenshot 2024-08-29 111751

2024-08-28 22:20:42.438 ERROR (MainThread) [homeassistant.components.notify] Error setting up platform smtp
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/notify/legacy.py", line 104, in async_setup_platform
    notify_service = await hass.async_add_executor_job(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/smtp/notify.py", line 103, in get_service
    if mail_service.connection_is_valid():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/smtp/notify.py", line 165, in connection_is_valid
    server = self.connect()
             ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/smtp/notify.py", line 151, in connect
    mail = smtplib.SMTP(self._server, self._port, timeout=self._timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/socket.py", line 853, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.12/socket.py", line 838, in create_connection
    sock.connect(sa)
OSError: [Errno 113] Host is unreachable

Issue had been previously reported and supposedly fixed, but maybe not?
https://github.com/home-assistant/core/pull/27240

In my particular case the SMTP forwarder I use is started in a different docker compose stack, regardless, the notification should not cause errors on startup just because the SMTP server may be down at that particular time.

What version of Home Assistant Core has the issue?

core-2024.8.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

smtp

Link to integration documentation on our website

https://www.home-assistant.io/integrations/smtp/

Diagnostics information

No response

Example YAML snippet

notify:
  - name: "Email"
    platform: smtp
    sender: !secret smtp_sender
    server: !secret smtp_host
    port: !secret smtp_port
    recipient: "logs@foo.com"
    encryption: none

Anything in the logs that might be useful for us?

2024-08-28 22:20:42.438 ERROR (MainThread) [homeassistant.components.notify] Error setting up platform smtp
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/notify/legacy.py", line 104, in async_setup_platform
    notify_service = await hass.async_add_executor_job(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/smtp/notify.py", line 103, in get_service
    if mail_service.connection_is_valid():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/smtp/notify.py", line 165, in connection_is_valid
    server = self.connect()
             ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/smtp/notify.py", line 151, in connect
    mail = smtplib.SMTP(self._server, self._port, timeout=self._timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/socket.py", line 853, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.12/socket.py", line 838, in create_connection
    sock.connect(sa)
OSError: [Errno 113] Host is unreachable

Additional information

No response

home-assistant[bot] commented 2 months ago

smtp documentation smtp source