From 4b232be04a15a44f468f3e1145857e4edae2043e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 29 Apr 2026 08:21:26 +0200 Subject: [PATCH] Unload scripts created by intent_script (#169363) --- .../components/intent_script/__init__.py | 5 ++- tests/components/intent_script/test_init.py | 38 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/intent_script/__init__.py b/homeassistant/components/intent_script/__init__.py index 8d58a0dd45b5..4005c294bcec 100644 --- a/homeassistant/components/intent_script/__init__.py +++ b/homeassistant/components/intent_script/__init__.py @@ -78,7 +78,10 @@ async def async_reload(hass: HomeAssistant, service_call: ServiceCall) -> None: new_config = await async_integration_yaml_config(hass, DOMAIN) existing_intents = hass.data[DOMAIN] - for intent_type in existing_intents: + for intent_type, conf in existing_intents.items(): + if isinstance(conf.get(CONF_ACTION), script.Script): + await conf[CONF_ACTION].async_stop() + conf[CONF_ACTION].async_unload() intent.async_remove(hass, intent_type) if not new_config or DOMAIN not in new_config: diff --git a/tests/components/intent_script/test_init.py b/tests/components/intent_script/test_init.py index e62761518941..22c9c805eaa6 100644 --- a/tests/components/intent_script/test_init.py +++ b/tests/components/intent_script/test_init.py @@ -5,7 +5,7 @@ from unittest.mock import patch import pytest from homeassistant import config as hass_config -from homeassistant.components.intent_script import DOMAIN +from homeassistant.components.intent_script import CONF_ACTION, DOMAIN from homeassistant.const import ATTR_FRIENDLY_NAME, SERVICE_RELOAD from homeassistant.core import HomeAssistant from homeassistant.helpers import ( @@ -13,6 +13,7 @@ from homeassistant.helpers import ( entity_registry as er, floor_registry as fr, intent, + script, ) from homeassistant.setup import async_setup_component @@ -462,3 +463,38 @@ async def test_reload(hass: HomeAssistant) -> None: assert len(intents) == 0 assert intents.get("NewIntent1") is None assert intents.get("NewIntent2") is None + + +async def test_reload_unloads_scripts(hass: HomeAssistant) -> None: + """Test that reloading intent scripts unloads the action scripts.""" + await async_setup_component( + hass, + "intent_script", + { + "intent_script": { + "TestIntent": { + "action": {"service": "test.service"}, + } + } + }, + ) + + existing_intents = hass.data[DOMAIN] + action_script = existing_intents["TestIntent"][CONF_ACTION] + assert isinstance(action_script, script.Script) + + yaml_path = get_fixture_path("configuration_no_entry.yaml", "intent_script") + with ( + patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path), + patch.object( + action_script, "async_stop", wraps=action_script.async_stop + ) as stop_mock, + patch.object( + action_script, "async_unload", wraps=action_script.async_unload + ) as unload_mock, + ): + await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True) + await hass.async_block_till_done() + + stop_mock.assert_called_once() + unload_mock.assert_called_once()