1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Reload config entry templates when labs flag automation.new_triggers_conditions is set (#157637)

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Petro31
2025-12-01 13:27:30 -05:00
committed by Franck Nijhof
parent 7dd9953345
commit a46dc7e05f
2 changed files with 76 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
from collections.abc import Coroutine
from functools import partial
import logging
from typing import Any
@@ -133,6 +134,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(
entry, (entry.options["template_type"],)
)
entry.async_on_unload(
async_labs_listen(
hass,
AUTOMATION_DOMAIN,
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
partial(hass.config_entries.async_schedule_reload, entry.entry_id),
)
)
return True

View File

@@ -596,7 +596,7 @@ async def test_fail_non_numerical_number_settings(
)
async def test_reload_when_labs_flag_changes(
async def test_yaml_reload_when_labs_flag_changes(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
hass_admin_user: MockUser,
@@ -690,3 +690,67 @@ async def test_reload_when_labs_flag_changes(
await hass.async_block_till_done()
assert hass.states.get("sensor.bye").state == set_state
last_state = set_state
async def test_config_entry_reload_when_labs_flag_changes(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
hass_admin_user: MockUser,
hass_read_only_user: MockUser,
) -> None:
"""Test templates are reloaded when labs flag changes."""
ws_client = await hass_ws_client(hass)
template_config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"name": "hello",
"template_type": "sensor",
"state": "{{ 'foo' }}",
},
title="My template",
)
template_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
await hass.async_block_till_done()
assert await async_setup_component(hass, labs.DOMAIN, {})
assert hass.states.get("sensor.hello") is not None
assert hass.states.get("sensor.hello").state == "foo"
# Check we reload whenever the labs flag is set, even if it's already enabled
for enabled, set_state in (
(True, "beer"),
(True, "is"),
(False, "very"),
(False, "good"),
):
hass.config_entries.async_update_entry(
template_config_entry,
options={
"name": "hello",
"template_type": "sensor",
"state": f"{{{{ '{set_state}' }}}}",
},
)
with patch(
"homeassistant.config.load_yaml_config_file",
autospec=True,
return_value={},
):
await ws_client.send_json_auto_id(
{
"type": "labs/update",
"domain": "automation",
"preview_feature": "new_triggers_conditions",
"enabled": enabled,
}
)
msg = await ws_client.receive_json()
assert msg["success"]
await hass.async_block_till_done()
assert hass.states.get("sensor.hello") is not None
assert hass.states.get("sensor.hello").state == set_state