mirror of
https://github.com/home-assistant/core.git
synced 2026-09-26 09:23:17 -04:00
Fix swallowed exceptions in ifttt action handlers (#182747)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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.",
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user