From 43fbf7db7ba6ea8585fdceddb34b062d8b1cdb25 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 9 Oct 2026 13:02:33 +0200 Subject: [PATCH] Fix Signal Messenger notify failing to load when the REST API is down at startup (#185225) --- .../components/signal_messenger/notify.py | 24 ++++++++++---- tests/components/signal_messenger/conftest.py | 8 +++-- .../signal_messenger/test_notify.py | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/signal_messenger/notify.py b/homeassistant/components/signal_messenger/notify.py index 0b14458c9dc8..37f0cafcc9da 100644 --- a/homeassistant/components/signal_messenger/notify.py +++ b/homeassistant/components/signal_messenger/notify.py @@ -84,9 +84,7 @@ def get_service( recp_nrs = config[CONF_RECP_NR] signal_cli_rest_api_url = config[CONF_SIGNAL_CLI_REST_API] - signal_cli_rest_api = SignalCliRestApi(signal_cli_rest_api_url, sender_nr) - - return SignalNotificationService(hass, recp_nrs, signal_cli_rest_api) + return SignalNotificationService(hass, recp_nrs, signal_cli_rest_api_url, sender_nr) class SignalNotificationService(BaseNotificationService): @@ -96,13 +94,27 @@ class SignalNotificationService(BaseNotificationService): self, hass: HomeAssistant, recp_nrs: list[str], - signal_cli_rest_api: SignalCliRestApi, + signal_cli_rest_api_url: str, + sender_nr: str, ) -> None: """Initialize the service.""" self._hass = hass self._recp_nrs = recp_nrs - self._signal_cli_rest_api = signal_cli_rest_api + self._signal_cli_rest_api_url = signal_cli_rest_api_url + self._sender_nr = sender_nr + self._signal_cli_rest_api: SignalCliRestApi | None = None + + @property + def signal_cli_rest_api(self) -> SignalCliRestApi: + """Return the REST API client, creating it on first use.""" + # Creating the client queries the REST API, which may not be up yet + # when Home Assistant starts. + if self._signal_cli_rest_api is None: + self._signal_cli_rest_api = SignalCliRestApi( + self._signal_cli_rest_api_url, self._sender_nr + ) + return self._signal_cli_rest_api @override def send_message(self, message: str = "", **kwargs: Any) -> None: @@ -125,7 +137,7 @@ class SignalNotificationService(BaseNotificationService): data, CONF_MAX_ALLOWED_DOWNLOAD_SIZE_BYTES, self._hass ) try: - self._signal_cli_rest_api.send_message( + self.signal_cli_rest_api.send_message( message, recipients, notify_self=True, diff --git a/tests/components/signal_messenger/conftest.py b/tests/components/signal_messenger/conftest.py index db6af1b0dda6..f2ad6d551466 100644 --- a/tests/components/signal_messenger/conftest.py +++ b/tests/components/signal_messenger/conftest.py @@ -1,5 +1,6 @@ """Signal notification test helpers.""" +from collections.abc import Generator from http import HTTPStatus from unittest.mock import patch @@ -20,14 +21,15 @@ URL_ATTACHMENT = f"{SIGNAL_BASE_URL}/image.jpg" @pytest.fixture -def signal_notification_service(hass: HomeAssistant) -> SignalNotificationService: +def signal_notification_service( + hass: HomeAssistant, +) -> Generator[SignalNotificationService]: """Set up signal notification service.""" hass.config.allowlist_external_urls.add(URL_ATTACHMENT) recipients = ["+435565656565"] number = "+43443434343" with patch.object(SignalCliRestApi, "mode", return_value="normal"): - client = SignalCliRestApi(SIGNAL_BASE_URL, number) - return SignalNotificationService(hass, recipients, client) + yield SignalNotificationService(hass, recipients, SIGNAL_BASE_URL, number) @pytest.fixture diff --git a/tests/components/signal_messenger/test_notify.py b/tests/components/signal_messenger/test_notify.py index 2c081233cbfd..ecf932d42f3d 100644 --- a/tests/components/signal_messenger/test_notify.py +++ b/tests/components/signal_messenger/test_notify.py @@ -10,6 +10,7 @@ from unittest.mock import patch import probatio from pysignalclirestapi.api import SignalCliRestApiError import pytest +import requests from requests_mock.mocker import Mocker from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN @@ -50,6 +51,38 @@ async def test_signal_messenger_init(hass: HomeAssistant) -> None: assert hass.services.has_service(NOTIFY_DOMAIN, "test") +async def test_signal_messenger_init_api_unavailable( + hass: HomeAssistant, + requests_mock: Mocker, + signal_requests_mock_factory: Mocker, +) -> None: + """Test that the service loads and sends once the REST API is available.""" + requests_mock.get( + f"{SIGNAL_BASE_URL}/v1/about", exc=requests.exceptions.ConnectionError + ) + config = { + NOTIFY_DOMAIN: { + "name": "test", + "platform": "signal_messenger", + "url": SIGNAL_BASE_URL, + "number": NUMBER_FROM, + "recipients": NUMBERS_TO, + } + } + + assert await async_setup_component(hass, NOTIFY_DOMAIN, config) + await hass.async_block_till_done() + + assert hass.services.has_service(NOTIFY_DOMAIN, "test") + + signal_requests_mock = signal_requests_mock_factory() + await hass.services.async_call( + NOTIFY_DOMAIN, "test", {"message": MESSAGE}, blocking=True + ) + + assert_sending_requests(signal_requests_mock) + + def test_send_message( signal_notification_service: SignalNotificationService, signal_requests_mock_factory: Mocker,