Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/template/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@
CONF_ATTRIBUTE_TEMPLATES = "attribute_templates"
CONF_PICTURE = "picture"
CONF_OBJECT_ID = "object_id"

EVENT_TEMPLATE_TRIGGERED = "template_triggered"
28 changes: 23 additions & 5 deletions homeassistant/components/template/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
from collections.abc import Callable
import logging

from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.core import CoreState, callback
from homeassistant.const import CONF_UNIQUE_ID, EVENT_HOMEASSISTANT_START
from homeassistant.core import Context, CoreState, callback
from homeassistant.helpers import discovery, trigger as trigger_helper
from homeassistant.helpers.script import Script
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import CONF_ACTION, CONF_TRIGGER, DOMAIN, PLATFORMS
from .const import (
CONF_ACTION,
CONF_TRIGGER,
DOMAIN,
EVENT_TEMPLATE_TRIGGERED,
PLATFORMS,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,10 +91,22 @@ async def _attach_triggers(self, start_event=None) -> None:
)

async def _handle_triggered(self, run_variables, context=None):
# Create a new context referring to the old context.
parent_id = None if context is None else context.id
trigger_context = Context(parent_id=parent_id)

self.hass.bus.async_fire(
EVENT_TEMPLATE_TRIGGERED,
{
CONF_UNIQUE_ID: self.unique_id,
},
context=trigger_context,
)

if self._script:
script_result = await self._script.async_run(run_variables, context)
script_result = await self._script.async_run(run_variables, trigger_context)
if script_result:
run_variables = script_result.variables
self.async_set_updated_data(
{"run_variables": run_variables, "context": context}
{"run_variables": run_variables, "context": trigger_context}
)
4 changes: 2 additions & 2 deletions tests/components/template/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ async def test_trigger_entity(
assert state.attributes.get("icon") == "mdi:pirate"
assert state.attributes.get("entity_picture") == "/local/dogs.png"
assert state.attributes.get("plus_one") == 3
assert state.context is context
assert state.context.parent_id is context.id

assert len(entity_registry.entities) == 2
assert (
Expand All @@ -1142,7 +1142,7 @@ async def test_trigger_entity(
assert state.attributes.get("entity_picture") == "/local/dogs.png"
assert state.attributes.get("plus_one") == 3
assert state.attributes.get("another") == 1
assert state.context is context
assert state.context.parent_id is context.id

# Even if state itself didn't change, attributes might have changed
hass.bus.async_fire("test_event", {"beer": 2, "uno_mas": "si"})
Expand Down
6 changes: 3 additions & 3 deletions tests/components/template/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ async def test_trigger_entity(
assert state.attributes.get("entity_picture") == "/local/dogs.png"
assert state.attributes.get("plus_one") == 3
assert state.attributes.get("unit_of_measurement") == "%"
assert state.context is context
assert state.context.parent_id is context.id

assert len(entity_registry.entities) == 2
assert (
Expand All @@ -1189,7 +1189,7 @@ async def test_trigger_entity(
assert state.attributes.get("plus_one") == 3
assert state.attributes.get("unit_of_measurement") == "%"
assert state.attributes.get("state_class") == "measurement"
assert state.context is context
assert state.context.parent_id is context.id


@pytest.mark.parametrize(("count", "domain"), [(1, "template")])
Expand Down Expand Up @@ -1667,4 +1667,4 @@ async def test_trigger_action(

state = hass.states.get("sensor.hello_name")
assert state.state == "3"
assert state.context is context
assert state.context.parent_id is context.id
4 changes: 2 additions & 2 deletions tests/components/template/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ async def test_trigger_action(
state = hass.states.get("weather.hello_name")
assert state.state == "sunny"
assert state.attributes["temperature"] == 3.0
assert state.context is context
assert state.context.parent_id is context.id


@pytest.mark.parametrize(("count", "domain"), [(1, "template")])
Expand Down Expand Up @@ -752,7 +752,7 @@ async def test_trigger_weather_services(
assert state.attributes["cloud_coverage"] == 3.0
assert state.attributes["dew_point"] == 3.0
assert state.attributes["apparent_temperature"] == 3.0
assert state.context is context
assert state.context.parent_id is context.id

response = await hass.services.async_call(
WEATHER_DOMAIN,
Expand Down