diff --git a/homeassistant/components/ifttt/__init__.py b/homeassistant/components/ifttt/__init__.py index 24ced8070f66..4d9e1126390f 100644 --- a/homeassistant/components/ifttt/__init__.py +++ b/homeassistant/components/ifttt/__init__.py @@ -13,6 +13,7 @@ from homeassistant.components import webhook from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_WEBHOOK_ID from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_entry_flow, config_validation as cv from homeassistant.helpers.typing import ConfigType @@ -86,9 +87,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: res = pyfttt.send_event(key, event, value1, value2, value3) if res.status_code != HTTPStatus.OK: _LOGGER.error("IFTTT reported error sending event to %s", target) - # pylint: disable-next=home-assistant-action-swallowed-exception - except requests.exceptions.RequestException: - _LOGGER.exception("Error communicating with IFTTT") + except requests.exceptions.RequestException as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="trigger_failed", + ) from err hass.services.async_register( DOMAIN, SERVICE_TRIGGER, trigger_service, schema=SERVICE_TRIGGER_SCHEMA diff --git a/homeassistant/components/ifttt/strings.json b/homeassistant/components/ifttt/strings.json index 4030480c9235..c9fb89907570 100644 --- a/homeassistant/components/ifttt/strings.json +++ b/homeassistant/components/ifttt/strings.json @@ -18,6 +18,11 @@ } } }, + "exceptions": { + "trigger_failed": { + "message": "Failed to trigger the IFTTT webhook." + } + }, "services": { "push_alarm_state": { "description": "Updates the alarm state to the specified value.", diff --git a/tests/components/ifttt/test_init.py b/tests/components/ifttt/test_init.py index ad4f283d5af3..bc7f7e1b00f7 100644 --- a/tests/components/ifttt/test_init.py +++ b/tests/components/ifttt/test_init.py @@ -1,12 +1,19 @@ """Test the init file of IFTTT.""" +from unittest.mock import patch + +import pytest +import requests + from homeassistant import config_entries from homeassistant.components import ifttt -from homeassistant.components.ifttt import DOMAIN +from homeassistant.components.ifttt import CONF_KEY, DOMAIN from homeassistant.core import HomeAssistant, callback from homeassistant.core_config import async_process_ha_core_config from homeassistant.data_entry_flow import FlowResultType +from homeassistant.exceptions import HomeAssistantError +from tests.common import async_setup_component from tests.typing import ClientSessionGenerator @@ -51,3 +58,27 @@ async def test_config_flow_registers_webhook( # Not a dict await client.post(f"/api/webhook/{webhook_id}", json="not a dict") assert len(ifttt_events) == 1 + + +async def test_trigger_service_raises_when_ifttt_unreachable( + hass: HomeAssistant, +) -> None: + """Test trigger_service raises when IFTTT cannot be reached.""" + await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_KEY: "secret"}}) + + with ( + patch( + "homeassistant.components.ifttt.pyfttt.send_event", + side_effect=requests.exceptions.ConnectionError, + ), + pytest.raises(HomeAssistantError) as exc_info, + ): + await hass.services.async_call( + DOMAIN, + ifttt.SERVICE_TRIGGER, + {"event": "test_event"}, + blocking=True, + ) + + assert exc_info.value.translation_domain == DOMAIN + assert exc_info.value.translation_key == "trigger_failed"