Skip to content

Commit 010aea9

Browse files
authored
Reload templates when labs flag automation.new_triggers_conditions is set (home-assistant#157368)
1 parent 563678d commit 010aea9

File tree

2 files changed

+126
-3
lines changed

2 files changed

+126
-3
lines changed

homeassistant/components/template/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
from typing import Any
99

1010
from homeassistant import config as conf_util
11+
from homeassistant.components.automation import (
12+
DOMAIN as AUTOMATION_DOMAIN,
13+
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
14+
)
15+
from homeassistant.components.labs import async_listen as async_labs_listen
1116
from homeassistant.config_entries import ConfigEntry
1217
from homeassistant.const import (
1318
CONF_DEVICE_ID,
@@ -16,7 +21,7 @@
1621
CONF_UNIQUE_ID,
1722
SERVICE_RELOAD,
1823
)
19-
from homeassistant.core import Event, HomeAssistant, ServiceCall
24+
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
2025
from homeassistant.exceptions import ConfigEntryError, HomeAssistantError
2126
from homeassistant.helpers import discovery, issue_registry as ir
2227
from homeassistant.helpers.device import (
@@ -90,6 +95,20 @@ async def _reload_config(call: Event | ServiceCall) -> None:
9095

9196
async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config)
9297

98+
@callback
99+
def new_triggers_conditions_listener() -> None:
100+
"""Handle new_triggers_conditions flag change."""
101+
hass.async_create_task(
102+
_reload_config(ServiceCall(hass, DOMAIN, SERVICE_RELOAD))
103+
)
104+
105+
async_labs_listen(
106+
hass,
107+
AUTOMATION_DOMAIN,
108+
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
109+
new_triggers_conditions_listener,
110+
)
111+
93112
return True
94113

95114

tests/components/template/test_init.py

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import pytest
77

88
from homeassistant import config
9+
from homeassistant.components import labs
910
from homeassistant.components.template import DOMAIN
1011
from homeassistant.const import SERVICE_RELOAD
11-
from homeassistant.core import HomeAssistant
12+
from homeassistant.core import Context, HomeAssistant
1213
from homeassistant.helpers import (
1314
device_registry as dr,
1415
entity_registry as er,
@@ -17,7 +18,14 @@
1718
from homeassistant.setup import async_setup_component
1819
from homeassistant.util import dt as dt_util
1920

20-
from tests.common import MockConfigEntry, async_fire_time_changed, get_fixture_path
21+
from tests.common import (
22+
MockConfigEntry,
23+
MockUser,
24+
async_capture_events,
25+
async_fire_time_changed,
26+
get_fixture_path,
27+
)
28+
from tests.typing import WebSocketGenerator
2129

2230

2331
@pytest.mark.parametrize(("count", "domain"), [(1, "sensor")])
@@ -586,3 +594,99 @@ async def test_fail_non_numerical_number_settings(
586594
"The 'My template' number template needs to be reconfigured, "
587595
"max must be a number, got '{{ 100 }}'" in caplog.text
588596
)
597+
598+
599+
async def test_reload_when_labs_flag_changes(
600+
hass: HomeAssistant,
601+
hass_ws_client: WebSocketGenerator,
602+
hass_admin_user: MockUser,
603+
hass_read_only_user: MockUser,
604+
) -> None:
605+
"""Test templates are reloaded when labs flag changes."""
606+
ws_client = await hass_ws_client(hass)
607+
608+
assert await async_setup_component(
609+
hass,
610+
DOMAIN,
611+
{
612+
DOMAIN: {
613+
"triggers": {
614+
"trigger": "event",
615+
"event_type": "test_event",
616+
},
617+
"sensor": {
618+
"name": "hello",
619+
"state": "{{ trigger.event.data.stuff }}",
620+
},
621+
}
622+
},
623+
)
624+
assert await async_setup_component(hass, labs.DOMAIN, {})
625+
assert hass.states.get("sensor.hello") is not None
626+
assert hass.states.get("sensor.bye") is None
627+
listeners = hass.bus.async_listeners()
628+
assert listeners.get("test_event") == 1
629+
assert listeners.get("test_event2") is None
630+
631+
context = Context()
632+
hass.bus.async_fire("test_event", {"stuff": "foo"}, context=context)
633+
await hass.async_block_till_done()
634+
assert hass.states.get("sensor.hello").state == "foo"
635+
636+
test_reload_event = async_capture_events(hass, "event_template_reloaded")
637+
638+
# Check we reload whenever the labs flag is set, even if it's already enabled
639+
last_state = "unknown"
640+
for enabled, set_state in (
641+
(True, "foo"),
642+
(True, "bar"),
643+
(False, "beer"),
644+
(False, "good"),
645+
):
646+
test_reload_event.clear()
647+
648+
with patch(
649+
"homeassistant.config.load_yaml_config_file",
650+
autospec=True,
651+
return_value={
652+
DOMAIN: {
653+
"triggers": {
654+
"trigger": "event",
655+
"event_type": "test_event2",
656+
},
657+
"sensor": {
658+
"name": "bye",
659+
"state": "{{ trigger.event.data.stuff }}",
660+
},
661+
}
662+
},
663+
):
664+
await ws_client.send_json_auto_id(
665+
{
666+
"type": "labs/update",
667+
"domain": "automation",
668+
"preview_feature": "new_triggers_conditions",
669+
"enabled": enabled,
670+
}
671+
)
672+
673+
msg = await ws_client.receive_json()
674+
assert msg["success"]
675+
await hass.async_block_till_done()
676+
677+
assert len(test_reload_event) == 1
678+
679+
assert hass.states.get("sensor.hello") is None
680+
assert hass.states.get("sensor.bye") is not None
681+
listeners = hass.bus.async_listeners()
682+
assert listeners.get("test_event") is None
683+
assert listeners.get("test_event2") == 1
684+
685+
hass.bus.async_fire("test_event", {"stuff": "foo"}, context=context)
686+
await hass.async_block_till_done()
687+
assert hass.states.get("sensor.bye").state == last_state
688+
689+
hass.bus.async_fire("test_event2", {"stuff": set_state}, context=context)
690+
await hass.async_block_till_done()
691+
assert hass.states.get("sensor.bye").state == set_state
692+
last_state = set_state

0 commit comments

Comments
 (0)