Skip to content

Commit 5bccbe2

Browse files
committed
Unload scripts created by script entities
1 parent 96bd991 commit 5bccbe2

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

homeassistant/components/script/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ async def async_added_to_hass(self) -> None:
769769
async def async_will_remove_from_hass(self) -> None:
770770
"""Stop script and remove service when it will be removed from HA."""
771771
await self.script.async_stop()
772+
self.script.async_unload()
772773

773774
# remove service
774775
self.hass.services.async_remove(DOMAIN, self._attr_unique_id)

tests/components/script/test_init.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,3 +1929,40 @@ async def test_reload_when_labs_flag_changes(
19291929

19301930
assert hass.states.get(f"script.{active_object_id}") is not None
19311931
assert hass.services.has_service(script.DOMAIN, active_object_id)
1932+
1933+
1934+
async def test_remove_script_entity_unloads_script(hass: HomeAssistant) -> None:
1935+
"""Test that removing a script entity unloads its underlying script."""
1936+
assert await async_setup_component(
1937+
hass,
1938+
script.DOMAIN,
1939+
{
1940+
script.DOMAIN: {
1941+
"test_script": {
1942+
"sequence": [{"event": "test_event"}],
1943+
}
1944+
}
1945+
},
1946+
)
1947+
1948+
entity = hass.data[script.DOMAIN].get_entity("script.test_script")
1949+
assert entity is not None
1950+
assert isinstance(entity, ScriptEntity)
1951+
1952+
# Reload with empty config to remove the script
1953+
with (
1954+
patch(
1955+
"homeassistant.config.load_yaml_config_file",
1956+
autospec=True,
1957+
return_value={script.DOMAIN: {}},
1958+
),
1959+
patch.object(
1960+
entity.script,
1961+
"async_unload",
1962+
wraps=entity.script.async_unload,
1963+
) as script_unload,
1964+
):
1965+
await hass.services.async_call(script.DOMAIN, SERVICE_RELOAD, blocking=True)
1966+
await hass.async_block_till_done()
1967+
1968+
script_unload.assert_called_once()

0 commit comments

Comments
 (0)