Skip to content

Commit ec77add

Browse files
authored
Reload scripts when labs flag automation.new_triggers_conditions is set (home-assistant#157348)
1 parent ef3b7df commit ec77add

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

homeassistant/components/script/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
from propcache.api import cached_property
1212
import voluptuous as vol
1313

14-
from homeassistant.components import websocket_api
14+
from homeassistant.components import automation, websocket_api
1515
from homeassistant.components.blueprint import CONF_USE_BLUEPRINT
16+
from homeassistant.components.labs import async_listen as async_labs_listen
1617
from homeassistant.const import (
1718
ATTR_ENTITY_ID,
1819
ATTR_MODE,
@@ -280,6 +281,21 @@ async def toggle_service(service: ServiceCall) -> None:
280281
hass.services.async_register(
281282
DOMAIN, SERVICE_TOGGLE, toggle_service, schema=SCRIPT_TURN_ONOFF_SCHEMA
282283
)
284+
285+
@callback
286+
def new_triggers_conditions_listener() -> None:
287+
"""Handle new_triggers_conditions flag change."""
288+
hass.async_create_task(
289+
reload_service(ServiceCall(hass, DOMAIN, SERVICE_RELOAD))
290+
)
291+
292+
async_labs_listen(
293+
hass,
294+
automation.DOMAIN,
295+
automation.NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
296+
new_triggers_conditions_listener,
297+
)
298+
283299
websocket_api.async_register_command(hass, websocket_config)
284300

285301
return True

tests/components/script/test_init.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from homeassistant.components import script
10+
from homeassistant.components import labs, script
1111
from homeassistant.components.script import DOMAIN, EVENT_SCRIPT_STARTED, ScriptEntity
1212
from homeassistant.config_entries import ConfigEntryState
1313
from homeassistant.const import (
@@ -1868,3 +1868,64 @@ async def async_service_handler(*args, **kwargs) -> None:
18681868

18691869
await hass.services.async_call("script", "test_main", blocking=True)
18701870
assert calls == 4
1871+
1872+
1873+
async def test_reload_when_labs_flag_changes(
1874+
hass: HomeAssistant,
1875+
hass_ws_client: WebSocketGenerator,
1876+
) -> None:
1877+
"""Test scripts are reloaded when labs flag changes."""
1878+
event = "test_event"
1879+
hass.states.async_set("test.script", "off")
1880+
1881+
ws_client = await hass_ws_client(hass)
1882+
1883+
assert await async_setup_component(
1884+
hass,
1885+
"script",
1886+
{
1887+
"script": {
1888+
"test": {
1889+
"sequence": [
1890+
{"event": event},
1891+
{"wait_template": "{{ is_state('test.script', 'on') }}"},
1892+
]
1893+
}
1894+
}
1895+
},
1896+
)
1897+
assert await async_setup_component(hass, labs.DOMAIN, {})
1898+
1899+
assert hass.states.get(ENTITY_ID) is not None
1900+
assert hass.services.has_service(script.DOMAIN, "test")
1901+
1902+
for enabled, active_object_id, inactive_object_ids in (
1903+
(False, "test2", ("test",)),
1904+
(True, "test3", ("test", "test2")),
1905+
):
1906+
with patch(
1907+
"homeassistant.config.load_yaml_config_file",
1908+
return_value={
1909+
"script": {active_object_id: {"sequence": [{"delay": {"seconds": 5}}]}}
1910+
},
1911+
):
1912+
await ws_client.send_json_auto_id(
1913+
{
1914+
"type": "labs/update",
1915+
"domain": "automation",
1916+
"preview_feature": "new_triggers_conditions",
1917+
"enabled": enabled,
1918+
}
1919+
)
1920+
1921+
msg = await ws_client.receive_json()
1922+
assert msg["success"]
1923+
await hass.async_block_till_done()
1924+
1925+
for inactive_object_id in inactive_object_ids:
1926+
state = hass.states.get(f"script.{inactive_object_id}")
1927+
assert state.attributes["restored"] is True
1928+
assert not hass.services.has_service(script.DOMAIN, inactive_object_id)
1929+
1930+
assert hass.states.get(f"script.{active_object_id}") is not None
1931+
assert hass.services.has_service(script.DOMAIN, active_object_id)

0 commit comments

Comments
 (0)