Guard for unexpected exceptions in device automation (#55639)

* Guard for unexpected exceptions in device automation

* merge

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Paulus Schoutsen 2021-09-03 09:04:50 -07:00 committed by GitHub
parent a234f2ab31
commit 418d6a6a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio import asyncio
from collections.abc import Iterable, Mapping from collections.abc import Iterable, Mapping
from functools import wraps from functools import wraps
import logging
from types import ModuleType from types import ModuleType
from typing import Any from typing import Any
@ -27,7 +28,6 @@ from .exceptions import DeviceNotFound, InvalidDeviceAutomationConfig
DOMAIN = "device_automation" DOMAIN = "device_automation"
DEVICE_TRIGGER_BASE_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( DEVICE_TRIGGER_BASE_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_PLATFORM): "device", vol.Required(CONF_PLATFORM): "device",
@ -174,6 +174,13 @@ async def _async_get_device_automations(
device_results, InvalidDeviceAutomationConfig device_results, InvalidDeviceAutomationConfig
): ):
continue 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: for automation in device_results:
combined_results[automation["device_id"]].append(automation) combined_results[automation["device_id"]].append(automation)

View File

@ -1,4 +1,6 @@
"""The test for light device automation.""" """The test for light device automation."""
from unittest.mock import patch
import pytest import pytest
from homeassistant.components import device_automation 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 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( async def test_websocket_get_trigger_capabilities(
hass, hass_ws_client, device_reg, entity_reg hass, hass_ws_client, device_reg, entity_reg
): ):