Reload template blueprints when reloading templates (#136794)

This commit is contained in:
Petro31 2025-01-28 22:48:38 -05:00 committed by GitHub
parent f909b54811
commit d06b0fe340
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 0 deletions

View File

@ -53,6 +53,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def _reload_config(call: Event | ServiceCall) -> None:
"""Reload top-level + platforms."""
await async_get_blueprints(hass).async_reset_cache()
try:
unprocessed_conf = await conf_util.async_hass_config_yaml(hass)
except HomeAssistantError as err:

View File

@ -149,6 +149,69 @@ async def test_inverted_binary_sensor(
)
async def test_reload_template_when_blueprint_changes(hass: HomeAssistant) -> None:
"""Test a template is updated at reload if the blueprint has changed."""
hass.states.async_set("binary_sensor.foo", "on", {"friendly_name": "Foo"})
config = {
DOMAIN: [
{
"use_blueprint": {
"path": "inverted_binary_sensor.yaml",
"input": {"reference_entity": "binary_sensor.foo"},
},
"name": "Inverted foo",
},
]
}
with patch_blueprint(
"inverted_binary_sensor.yaml",
BUILTIN_BLUEPRINT_FOLDER / "inverted_binary_sensor.yaml",
):
assert await async_setup_component(hass, DOMAIN, config)
hass.states.async_set("binary_sensor.foo", "off", {"friendly_name": "Foo"})
await hass.async_block_till_done()
assert hass.states.get("binary_sensor.foo").state == "off"
inverted = hass.states.get("binary_sensor.inverted_foo")
assert inverted
assert inverted.state == "on"
# Reload the automations without any change, but with updated blueprint
blueprint_config = yaml_util.load_yaml(
BUILTIN_BLUEPRINT_FOLDER / "inverted_binary_sensor.yaml"
)
blueprint_config["binary_sensor"]["state"] = "{{ states(reference_entity) }}"
with (
patch(
"homeassistant.config.load_yaml_config_file",
autospec=True,
return_value=config,
),
patch(
"homeassistant.components.blueprint.models.yaml_util.load_yaml_dict",
autospec=True,
return_value=blueprint_config,
),
):
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
hass.states.async_set("binary_sensor.foo", "off", {"friendly_name": "Foo"})
await hass.async_block_till_done()
not_inverted = hass.states.get("binary_sensor.inverted_foo")
assert not_inverted
assert not_inverted.state == "off"
hass.states.async_set("binary_sensor.foo", "on", {"friendly_name": "Foo"})
await hass.async_block_till_done()
not_inverted = hass.states.get("binary_sensor.inverted_foo")
assert not_inverted
assert not_inverted.state == "on"
async def test_domain_blueprint(hass: HomeAssistant) -> None:
"""Test DomainBlueprint services."""
reload_handler_calls = async_mock_service(hass, DOMAIN, SERVICE_RELOAD)