Ensure device_automation can handle RequirementsNotFound (#92037)

pull/91995/head^2
J. Nick Koston 2023-04-26 18:43:38 +02:00 committed by GitHub
parent 330a7afdfc
commit ead761dfa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -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 "

View File

@ -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
)