From ead761dfa23a50e88243d3f30a54e8263f00db84 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 26 Apr 2023 18:43:38 +0200 Subject: [PATCH] Ensure device_automation can handle RequirementsNotFound (#92037) --- .../components/device_automation/__init__.py | 9 ++++++- .../components/device_automation/test_init.py | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/device_automation/__init__.py b/homeassistant/components/device_automation/__init__.py index 3465e9ae273..d5017ac2329 100644 --- a/homeassistant/components/device_automation/__init__.py +++ b/homeassistant/components/device_automation/__init__.py @@ -29,7 +29,10 @@ from homeassistant.helpers import ( ) from homeassistant.helpers.typing import ConfigType from homeassistant.loader import IntegrationNotFound -from homeassistant.requirements import async_get_integration_with_requirements +from homeassistant.requirements import ( + RequirementsNotFound, + async_get_integration_with_requirements, +) from .const import ( # noqa: F401 CONF_IS_OFF, @@ -171,6 +174,10 @@ async def async_get_device_automation_platform( raise InvalidDeviceAutomationConfig( f"Integration '{domain}' not found" ) from err + except RequirementsNotFound as err: + raise InvalidDeviceAutomationConfig( + f"Integration '{domain}' could not be loaded" + ) from err except ImportError as err: raise InvalidDeviceAutomationConfig( f"Integration '{domain}' does not support device automation " diff --git a/tests/components/device_automation/test_init.py b/tests/components/device_automation/test_init.py index d84b64955e1..2fdea0a0bb1 100644 --- a/tests/components/device_automation/test_init.py +++ b/tests/components/device_automation/test_init.py @@ -17,6 +17,8 @@ from homeassistant.const import CONF_PLATFORM, STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.typing import ConfigType +from homeassistant.loader import IntegrationNotFound +from homeassistant.requirements import RequirementsNotFound from homeassistant.setup import async_setup_component from tests.common import ( @@ -1554,3 +1556,25 @@ async def test_automation_with_device_component_not_loaded( ) module.async_validate_trigger_config.assert_not_awaited() + + +@pytest.mark.parametrize( + "exc", + [ + IntegrationNotFound("test"), + RequirementsNotFound("test", []), + ImportError("test"), + ], +) +async def test_async_get_device_automations_platform_reraises_exceptions( + hass: HomeAssistant, exc: Exception +) -> None: + """Test InvalidDeviceAutomationConfig is raised when async_get_integration_with_requirements fails.""" + await async_setup_component(hass, "device_automation", {}) + with patch( + "homeassistant.components.device_automation.async_get_integration_with_requirements", + side_effect=exc, + ), pytest.raises(InvalidDeviceAutomationConfig): + await device_automation.async_get_device_automation_platform( + hass, "test", device_automation.DeviceAutomationType.TRIGGER + )