Skip to content

Commit 3073a99

Browse files
Petro31emontnemery
andauthored
Reload config entry templates when labs flag automation.new_triggers_conditions is set (home-assistant#157637)
Co-authored-by: Erik Montnemery <[email protected]>
1 parent 8b04ce1 commit 3073a99

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

homeassistant/components/template/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
from collections.abc import Coroutine
7+
from functools import partial
78
import logging
89
from typing import Any
910

@@ -133,6 +134,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
133134
await hass.config_entries.async_forward_entry_setups(
134135
entry, (entry.options["template_type"],)
135136
)
137+
138+
entry.async_on_unload(
139+
async_labs_listen(
140+
hass,
141+
AUTOMATION_DOMAIN,
142+
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
143+
partial(hass.config_entries.async_schedule_reload, entry.entry_id),
144+
)
145+
)
146+
136147
return True
137148

138149

tests/components/template/test_init.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ async def test_fail_non_numerical_number_settings(
596596
)
597597

598598

599-
async def test_reload_when_labs_flag_changes(
599+
async def test_yaml_reload_when_labs_flag_changes(
600600
hass: HomeAssistant,
601601
hass_ws_client: WebSocketGenerator,
602602
hass_admin_user: MockUser,
@@ -690,3 +690,67 @@ async def test_reload_when_labs_flag_changes(
690690
await hass.async_block_till_done()
691691
assert hass.states.get("sensor.bye").state == set_state
692692
last_state = set_state
693+
694+
695+
async def test_config_entry_reload_when_labs_flag_changes(
696+
hass: HomeAssistant,
697+
hass_ws_client: WebSocketGenerator,
698+
hass_admin_user: MockUser,
699+
hass_read_only_user: MockUser,
700+
) -> None:
701+
"""Test templates are reloaded when labs flag changes."""
702+
ws_client = await hass_ws_client(hass)
703+
704+
template_config_entry = MockConfigEntry(
705+
data={},
706+
domain=DOMAIN,
707+
options={
708+
"name": "hello",
709+
"template_type": "sensor",
710+
"state": "{{ 'foo' }}",
711+
},
712+
title="My template",
713+
)
714+
template_config_entry.add_to_hass(hass)
715+
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
716+
await hass.async_block_till_done()
717+
assert await async_setup_component(hass, labs.DOMAIN, {})
718+
719+
assert hass.states.get("sensor.hello") is not None
720+
assert hass.states.get("sensor.hello").state == "foo"
721+
722+
# Check we reload whenever the labs flag is set, even if it's already enabled
723+
for enabled, set_state in (
724+
(True, "beer"),
725+
(True, "is"),
726+
(False, "very"),
727+
(False, "good"),
728+
):
729+
hass.config_entries.async_update_entry(
730+
template_config_entry,
731+
options={
732+
"name": "hello",
733+
"template_type": "sensor",
734+
"state": f"{{{{ '{set_state}' }}}}",
735+
},
736+
)
737+
with patch(
738+
"homeassistant.config.load_yaml_config_file",
739+
autospec=True,
740+
return_value={},
741+
):
742+
await ws_client.send_json_auto_id(
743+
{
744+
"type": "labs/update",
745+
"domain": "automation",
746+
"preview_feature": "new_triggers_conditions",
747+
"enabled": enabled,
748+
}
749+
)
750+
751+
msg = await ws_client.receive_json()
752+
assert msg["success"]
753+
await hass.async_block_till_done()
754+
755+
assert hass.states.get("sensor.hello") is not None
756+
assert hass.states.get("sensor.hello").state == set_state

0 commit comments

Comments
 (0)