Support this variable in template alarm actions (#71744)

pull/71780/head
Erik Montnemery 2022-05-13 12:17:40 +02:00 committed by GitHub
parent f301de98e4
commit dba2f5ab1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -230,7 +230,9 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
self._state = state
optimistic_set = True
await script.async_run({ATTR_CODE: code}, context=self._context)
await self.async_run_script(
script, run_variables={ATTR_CODE: code}, context=self._context
)
if optimistic_set:
self.async_write_ha_state()

View File

@ -19,7 +19,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
STATE_UNKNOWN,
)
from homeassistant.core import CoreState, Event, State, callback
from homeassistant.core import Context, CoreState, Event, State, callback
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
@ -28,6 +28,7 @@ from homeassistant.helpers.event import (
TrackTemplateResult,
async_track_template_result,
)
from homeassistant.helpers.script import Script, _VarsType
from homeassistant.helpers.template import (
Template,
TemplateStateFromEntityId,
@ -455,3 +456,21 @@ class TemplateEntity(Entity):
async def async_update(self) -> None:
"""Call for forced update."""
self._async_update()
async def async_run_script(
self,
script: Script,
*,
run_variables: _VarsType | None = None,
context: Context | None = None,
) -> None:
"""Run an action script."""
if run_variables is None:
run_variables = {}
return await script.async_run(
run_variables={
"this": TemplateStateFromEntityId(self.hass, self.entity_id),
**run_variables,
},
context=context,
)

View File

@ -44,22 +44,22 @@ OPTIMISTIC_TEMPLATE_ALARM_CONFIG = {
"arm_away": {
"service": "alarm_control_panel.alarm_arm_away",
"entity_id": "alarm_control_panel.test",
"data": {"code": "1234"},
"data": {"code": "{{ this.entity_id }}"},
},
"arm_home": {
"service": "alarm_control_panel.alarm_arm_home",
"entity_id": "alarm_control_panel.test",
"data": {"code": "1234"},
"data": {"code": "{{ this.entity_id }}"},
},
"arm_night": {
"service": "alarm_control_panel.alarm_arm_night",
"entity_id": "alarm_control_panel.test",
"data": {"code": "1234"},
"data": {"code": "{{ this.entity_id }}"},
},
"disarm": {
"service": "alarm_control_panel.alarm_disarm",
"entity_id": "alarm_control_panel.test",
"data": {"code": "1234"},
"data": {"code": "{{ this.entity_id }}"},
},
}
@ -261,6 +261,7 @@ async def test_actions(hass, service, start_ha, service_calls):
await hass.async_block_till_done()
assert len(service_calls) == 1
assert service_calls[0].data["service"] == service
assert service_calls[0].data["service_data"]["code"] == TEMPLATE_NAME
@pytest.mark.parametrize("count,domain", [(1, "alarm_control_panel")])