mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Allow templatable service target to support scripts (#48600)
This commit is contained in:
parent
c9cd6b0fbb
commit
4e3c12883e
@ -916,7 +916,7 @@ SERVICE_SCHEMA = vol.All(
|
|||||||
vol.Optional("data"): vol.All(dict, template_complex),
|
vol.Optional("data"): vol.All(dict, template_complex),
|
||||||
vol.Optional("data_template"): 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_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),
|
has_at_least_one_key(CONF_SERVICE, CONF_SERVICE_TEMPLATE),
|
||||||
|
@ -204,10 +204,15 @@ def async_prepare_call_from_config(
|
|||||||
|
|
||||||
target = {}
|
target = {}
|
||||||
if CONF_TARGET in config:
|
if CONF_TARGET in config:
|
||||||
conf = config.get(CONF_TARGET)
|
conf = config[CONF_TARGET]
|
||||||
try:
|
try:
|
||||||
template.attach(hass, conf)
|
if isinstance(conf, template.Template):
|
||||||
target.update(template.render_complex(conf, variables))
|
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:
|
if CONF_ENTITY_ID in target:
|
||||||
target[CONF_ENTITY_ID] = cv.comp_entity_ids(target[CONF_ENTITY_ID])
|
target[CONF_ENTITY_ID] = cv.comp_entity_ids(target[CONF_ENTITY_ID])
|
||||||
except TemplateError as ex:
|
except TemplateError as ex:
|
||||||
|
@ -213,6 +213,30 @@ class TestServiceHelpers(unittest.TestCase):
|
|||||||
"entity_id": ["light.static", "light.dynamic"],
|
"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):
|
def test_service_template_service_call(self):
|
||||||
"""Test legacy service_template call with templating."""
|
"""Test legacy service_template call with templating."""
|
||||||
config = {
|
config = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user