diff --git a/homeassistant/components/device_automation/__init__.py b/homeassistant/components/device_automation/__init__.py index 945774da0b4..89a3f8f6408 100644 --- a/homeassistant/components/device_automation/__init__.py +++ b/homeassistant/components/device_automation/__init__.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio from collections.abc import Iterable, Mapping from functools import wraps +import logging from types import ModuleType from typing import Any @@ -27,7 +28,6 @@ from .exceptions import DeviceNotFound, InvalidDeviceAutomationConfig DOMAIN = "device_automation" - DEVICE_TRIGGER_BASE_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( { vol.Required(CONF_PLATFORM): "device", @@ -174,6 +174,13 @@ async def _async_get_device_automations( device_results, InvalidDeviceAutomationConfig ): continue + if isinstance(device_results, Exception): + logging.getLogger(__name__).error( + "Unexpected error fetching device %ss", + automation_type, + exc_info=device_results, + ) + continue for automation in device_results: combined_results[automation["device_id"]].append(automation) diff --git a/tests/components/device_automation/test_init.py b/tests/components/device_automation/test_init.py index 13190ed4b32..93d64e97959 100644 --- a/tests/components/device_automation/test_init.py +++ b/tests/components/device_automation/test_init.py @@ -1,4 +1,6 @@ """The test for light device automation.""" +from unittest.mock import patch + import pytest from homeassistant.components import device_automation @@ -443,6 +445,28 @@ async def test_async_get_device_automations_all_devices_action( assert len(result[device_entry.id]) == 3 +async def test_async_get_device_automations_all_devices_action_exception_throw( + hass, device_reg, entity_reg, caplog +): + """Test we get can fetch all the actions when no device id is passed and can handle one throwing an exception.""" + await async_setup_component(hass, "device_automation", {}) + config_entry = MockConfigEntry(domain="test", data={}) + config_entry.add_to_hass(hass) + device_entry = device_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id) + with patch( + "homeassistant.components.light.device_trigger.async_get_triggers", + side_effect=KeyError, + ): + result = await device_automation.async_get_device_automations(hass, "trigger") + assert device_entry.id in result + assert len(result[device_entry.id]) == 0 + assert "KeyError" in caplog.text + + async def test_websocket_get_trigger_capabilities( hass, hass_ws_client, device_reg, entity_reg ):