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

Reload templates when labs flag automation.new_triggers_conditions is set (#157368)

This commit is contained in:
Petro31
2025-11-27 12:05:33 -05:00
committed by GitHub
parent 563678dc47
commit 010aea952c
2 changed files with 126 additions and 3 deletions

View File

@@ -8,6 +8,11 @@ import logging
from typing import Any
from homeassistant import config as conf_util
from homeassistant.components.automation import (
DOMAIN as AUTOMATION_DOMAIN,
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
)
from homeassistant.components.labs import async_listen as async_labs_listen
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DEVICE_ID,
@@ -16,7 +21,7 @@ from homeassistant.const import (
CONF_UNIQUE_ID,
SERVICE_RELOAD,
)
from homeassistant.core import Event, HomeAssistant, ServiceCall
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import ConfigEntryError, HomeAssistantError
from homeassistant.helpers import discovery, issue_registry as ir
from homeassistant.helpers.device import (
@@ -90,6 +95,20 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config)
@callback
def new_triggers_conditions_listener() -> None:
"""Handle new_triggers_conditions flag change."""
hass.async_create_task(
_reload_config(ServiceCall(hass, DOMAIN, SERVICE_RELOAD))
)
async_labs_listen(
hass,
AUTOMATION_DOMAIN,
NEW_TRIGGERS_CONDITIONS_FEATURE_FLAG,
new_triggers_conditions_listener,
)
return True

View File

@@ -6,9 +6,10 @@ from unittest.mock import patch
import pytest
from homeassistant import config
from homeassistant.components import labs
from homeassistant.components.template import DOMAIN
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.core import Context, HomeAssistant
from homeassistant.helpers import (
device_registry as dr,
entity_registry as er,
@@ -17,7 +18,14 @@ from homeassistant.helpers import (
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed, get_fixture_path
from tests.common import (
MockConfigEntry,
MockUser,
async_capture_events,
async_fire_time_changed,
get_fixture_path,
)
from tests.typing import WebSocketGenerator
@pytest.mark.parametrize(("count", "domain"), [(1, "sensor")])
@@ -586,3 +594,99 @@ async def test_fail_non_numerical_number_settings(
"The 'My template' number template needs to be reconfigured, "
"max must be a number, got '{{ 100 }}'" in caplog.text
)
async def test_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)
assert await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
"triggers": {
"trigger": "event",
"event_type": "test_event",
},
"sensor": {
"name": "hello",
"state": "{{ trigger.event.data.stuff }}",
},
}
},
)
assert await async_setup_component(hass, labs.DOMAIN, {})
assert hass.states.get("sensor.hello") is not None
assert hass.states.get("sensor.bye") is None
listeners = hass.bus.async_listeners()
assert listeners.get("test_event") == 1
assert listeners.get("test_event2") is None
context = Context()
hass.bus.async_fire("test_event", {"stuff": "foo"}, context=context)
await hass.async_block_till_done()
assert hass.states.get("sensor.hello").state == "foo"
test_reload_event = async_capture_events(hass, "event_template_reloaded")
# Check we reload whenever the labs flag is set, even if it's already enabled
last_state = "unknown"
for enabled, set_state in (
(True, "foo"),
(True, "bar"),
(False, "beer"),
(False, "good"),
):
test_reload_event.clear()
with patch(
"homeassistant.config.load_yaml_config_file",
autospec=True,
return_value={
DOMAIN: {
"triggers": {
"trigger": "event",
"event_type": "test_event2",
},
"sensor": {
"name": "bye",
"state": "{{ trigger.event.data.stuff }}",
},
}
},
):
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 len(test_reload_event) == 1
assert hass.states.get("sensor.hello") is None
assert hass.states.get("sensor.bye") is not None
listeners = hass.bus.async_listeners()
assert listeners.get("test_event") is None
assert listeners.get("test_event2") == 1
hass.bus.async_fire("test_event", {"stuff": "foo"}, context=context)
await hass.async_block_till_done()
assert hass.states.get("sensor.bye").state == last_state
hass.bus.async_fire("test_event2", {"stuff": set_state}, context=context)
await hass.async_block_till_done()
assert hass.states.get("sensor.bye").state == set_state
last_state = set_state