From 8a9ca9bd9807f9c2dacca40611dd6dcb7c5721ae Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 19 Jan 2026 09:47:37 +0100 Subject: [PATCH] Use hass.data[TRIGGERS] when instantiating triggers --- homeassistant/helpers/trigger.py | 7 ++++--- tests/helpers/test_trigger.py | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 7bc74a52631..99214e28497 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -1073,9 +1073,10 @@ async def _async_get_trigger_platform( ) -> tuple[str, TriggerProtocol]: from homeassistant.components import automation # noqa: PLC0415 - platform_and_sub_type = trigger_key.split(".") - platform = platform_and_sub_type[0] - platform = _PLATFORM_ALIASES.get(platform, platform) + if not (platform := hass.data[TRIGGERS].get(trigger_key)): + platform = _PLATFORM_ALIASES.get(trigger_key) + if not platform: + raise vol.Invalid(f"Invalid trigger '{trigger_key}' specified") if automation.is_disabled_experimental_trigger(hass, platform): raise vol.Invalid( diff --git a/tests/helpers/test_trigger.py b/tests/helpers/test_trigger.py index 092ae05d96a..721d7eeda2a 100644 --- a/tests/helpers/test_trigger.py +++ b/tests/helpers/test_trigger.py @@ -66,6 +66,16 @@ async def test_bad_trigger_platform(hass: HomeAssistant) -> None: async def test_trigger_subtype(hass: HomeAssistant) -> None: """Test trigger subtypes.""" + + async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: + return { + "subtype": Mock(), + } + + mock_integration(hass, MockModule("test")) + mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers)) + await async_setup_component(hass, "test", {}) + with patch( "homeassistant.helpers.trigger.async_get_integration", return_value=MagicMock(async_get_platform=AsyncMock()), @@ -530,6 +540,7 @@ async def test_platform_multiple_triggers( mock_integration(hass, MockModule("test")) mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers)) + await async_setup_component(hass, "test", {}) config_1 = [{"platform": "test"}] config_2 = [{"platform": "test.trig_2", "options": {"x": 1}}] @@ -576,7 +587,9 @@ async def test_platform_multiple_triggers( } action_helper.action_calls.clear() - with pytest.raises(KeyError): + with pytest.raises( + vol.Invalid, match="Invalid trigger 'test.unknown_trig' specified" + ): await async_initialize_triggers( hass, config_3, action_method, "test", "", log_cb ) @@ -617,6 +630,7 @@ async def test_platform_migrate_trigger(hass: HomeAssistant) -> None: mock_integration(hass, MockModule("test")) mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers)) + await async_setup_component(hass, "test", {}) config_1 = [{"platform": "test", "option_1": "value_1", "option_2": 2}] config_2 = [{"platform": "test", "option_1": "value_1"}] @@ -646,6 +660,7 @@ async def test_platform_backwards_compatibility_for_new_style_configs( mock_integration(hass, MockModule("test")) mock_platform(hass, "test.trigger", MockTriggerPlatform()) + await async_setup_component(hass, "test", {}) config_old_style = [{"platform": "test", "option_1": "value_1", "option_2": 2}] result = await async_validate_trigger_config(hass, config_old_style) @@ -1255,6 +1270,7 @@ async def test_numerical_state_attribute_changed_trigger_config_validation( mock_integration(hass, MockModule("test")) mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers)) + await async_setup_component(hass, "test", {}) with expected_result: await async_validate_trigger_config( @@ -1283,6 +1299,7 @@ async def test_numerical_state_attribute_changed_error_handling( mock_integration(hass, MockModule("test")) mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers)) + await async_setup_component(hass, "test", {}) hass.states.async_set("test.test_entity", "on", {"test_attribute": 20}) @@ -1565,6 +1582,7 @@ async def test_numerical_state_attribute_crossed_threshold_trigger_config_valida mock_integration(hass, MockModule("test")) mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers)) + await async_setup_component(hass, "test", {}) with expected_result: await async_validate_trigger_config(