Skip to content
Merged
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: 1 addition & 1 deletion homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def script_action(value: Any) -> dict:
vol.Optional("data"): vol.All(dict, template_complex),
vol.Optional("data_template"): vol.All(dict, template_complex),
vol.Optional(CONF_ENTITY_ID): comp_entity_ids,
vol.Optional(CONF_TARGET): ENTITY_SERVICE_FIELDS,
vol.Optional(CONF_TARGET): vol.Any(ENTITY_SERVICE_FIELDS, dynamic_template),
}
),
has_at_least_one_key(CONF_SERVICE, CONF_SERVICE_TEMPLATE),
Expand Down
11 changes: 8 additions & 3 deletions homeassistant/helpers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,15 @@ def async_prepare_call_from_config(

target = {}
if CONF_TARGET in config:
conf = config.get(CONF_TARGET)
conf = config[CONF_TARGET]
try:
template.attach(hass, conf)
target.update(template.render_complex(conf, variables))
if isinstance(conf, template.Template):
conf.hass = hass
target.update(conf.async_render(variables))
else:
template.attach(hass, conf)
target.update(template.render_complex(conf, variables))

if CONF_ENTITY_ID in target:
target[CONF_ENTITY_ID] = cv.comp_entity_ids(target[CONF_ENTITY_ID])
except TemplateError as ex:
Expand Down
24 changes: 24 additions & 0 deletions tests/helpers/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ def test_service_call(self):
"entity_id": ["light.static", "light.dynamic"],
}

config = {
"service": "{{ 'test_domain.test_service' }}",
"target": "{{ var_target }}",
}

service.call_from_config(
self.hass,
config,
variables={
"var_target": {
"entity_id": "light.static",
"area_id": ["area-42", "area-51"],
},
},
)

service.call_from_config(self.hass, config)
self.hass.block_till_done()

assert dict(self.calls[2].data) == {
"area_id": ["area-42", "area-51"],
"entity_id": ["light.static"],
}

def test_service_template_service_call(self):
"""Test legacy service_template call with templating."""
config = {
Expand Down