Skip to content

Commit ef3b7df

Browse files
authored
Reload automations when labs flag automation.new_triggers_conditions is set (home-assistant#157347)
1 parent 51241d9 commit ef3b7df

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

homeassistant/components/automation/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from homeassistant.components import labs, websocket_api
1616
from homeassistant.components.blueprint import CONF_USE_BLUEPRINT
17+
from homeassistant.components.labs import async_listen as async_labs_listen
1718
from homeassistant.const import (
1819
ATTR_ENTITY_ID,
1920
ATTR_MODE,
@@ -345,6 +346,20 @@ def reload_targets(service_call: ServiceCall) -> set[str | None]:
345346
schema=vol.Schema({vol.Optional(CONF_ID): str}),
346347
)
347348

349+
@callback
350+
def new_triggers_conditions_listener() -> None:
351+
"""Handle new_triggers_conditions flag change."""
352+
hass.async_create_task(
353+
reload_helper.execute_service(ServiceCall(hass, DOMAIN, SERVICE_RELOAD))
354+
)
355+
356+
async_labs_listen(
357+
hass,
358+
DOMAIN,
359+
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
360+
new_triggers_conditions_listener,
361+
)
362+
348363
websocket_api.async_register_command(hass, websocket_config)
349364

350365
return True

tests/components/automation/test_init.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from homeassistant.components import automation, input_boolean, script
11+
from homeassistant.components import automation, input_boolean, labs, script
1212
from homeassistant.components.automation import (
1313
ATTR_SOURCE,
1414
DOMAIN,
@@ -3493,3 +3493,92 @@ async def test_valid_configuration(
34933493
},
34943494
)
34953495
await hass.async_block_till_done()
3496+
3497+
3498+
async def test_reload_when_labs_flag_changes(
3499+
hass: HomeAssistant,
3500+
hass_ws_client: WebSocketGenerator,
3501+
calls: list[ServiceCall],
3502+
hass_admin_user: MockUser,
3503+
hass_read_only_user: MockUser,
3504+
) -> None:
3505+
"""Test automations are reloaded when labs flag changes."""
3506+
ws_client = await hass_ws_client(hass)
3507+
3508+
assert await async_setup_component(
3509+
hass,
3510+
automation.DOMAIN,
3511+
{
3512+
automation.DOMAIN: {
3513+
"alias": "hello",
3514+
"trigger": {"platform": "event", "event_type": "test_event"},
3515+
"action": {
3516+
"action": "test.automation",
3517+
"data_template": {"event": "{{ trigger.event.event_type }}"},
3518+
},
3519+
}
3520+
},
3521+
)
3522+
assert await async_setup_component(hass, labs.DOMAIN, {})
3523+
assert hass.states.get("automation.hello") is not None
3524+
assert hass.states.get("automation.bye") is None
3525+
listeners = hass.bus.async_listeners()
3526+
assert listeners.get("test_event") == 1
3527+
assert listeners.get("test_event2") is None
3528+
3529+
hass.bus.async_fire("test_event")
3530+
await hass.async_block_till_done()
3531+
3532+
assert len(calls) == 1
3533+
assert calls[0].data.get("event") == "test_event"
3534+
3535+
test_reload_event = async_capture_events(hass, EVENT_AUTOMATION_RELOADED)
3536+
3537+
# Check we reload whenever the labs flag is set, even if it's already enabled
3538+
for enabled in (True, True, False, False):
3539+
test_reload_event.clear()
3540+
calls.clear()
3541+
3542+
with patch(
3543+
"homeassistant.config.load_yaml_config_file",
3544+
autospec=True,
3545+
return_value={
3546+
automation.DOMAIN: {
3547+
"alias": "bye",
3548+
"trigger": {"platform": "event", "event_type": "test_event2"},
3549+
"action": {
3550+
"action": "test.automation",
3551+
"data_template": {"event": "{{ trigger.event.event_type }}"},
3552+
},
3553+
}
3554+
},
3555+
):
3556+
await ws_client.send_json_auto_id(
3557+
{
3558+
"type": "labs/update",
3559+
"domain": "automation",
3560+
"preview_feature": "new_triggers_conditions",
3561+
"enabled": enabled,
3562+
}
3563+
)
3564+
3565+
msg = await ws_client.receive_json()
3566+
assert msg["success"]
3567+
await hass.async_block_till_done()
3568+
3569+
assert len(test_reload_event) == 1
3570+
3571+
assert hass.states.get("automation.hello") is None
3572+
assert hass.states.get("automation.bye") is not None
3573+
listeners = hass.bus.async_listeners()
3574+
assert listeners.get("test_event") is None
3575+
assert listeners.get("test_event2") == 1
3576+
3577+
hass.bus.async_fire("test_event")
3578+
await hass.async_block_till_done()
3579+
assert len(calls) == 0
3580+
3581+
hass.bus.async_fire("test_event2")
3582+
await hass.async_block_till_done()
3583+
assert len(calls) == 1
3584+
assert calls[-1].data.get("event") == "test_event2"

0 commit comments

Comments
 (0)