mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Support this variable in template alarm actions (#71744)
This commit is contained in:
parent
f301de98e4
commit
dba2f5ab1c
@ -230,7 +230,9 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
|
|||||||
self._state = state
|
self._state = state
|
||||||
optimistic_set = True
|
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:
|
if optimistic_set:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@ -19,7 +19,7 @@ from homeassistant.const import (
|
|||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CoreState, Event, State, callback
|
from homeassistant.core import Context, CoreState, Event, State, callback
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
@ -28,6 +28,7 @@ from homeassistant.helpers.event import (
|
|||||||
TrackTemplateResult,
|
TrackTemplateResult,
|
||||||
async_track_template_result,
|
async_track_template_result,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.script import Script, _VarsType
|
||||||
from homeassistant.helpers.template import (
|
from homeassistant.helpers.template import (
|
||||||
Template,
|
Template,
|
||||||
TemplateStateFromEntityId,
|
TemplateStateFromEntityId,
|
||||||
@ -455,3 +456,21 @@ class TemplateEntity(Entity):
|
|||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Call for forced update."""
|
"""Call for forced update."""
|
||||||
self._async_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,
|
||||||
|
)
|
||||||
|
@ -44,22 +44,22 @@ OPTIMISTIC_TEMPLATE_ALARM_CONFIG = {
|
|||||||
"arm_away": {
|
"arm_away": {
|
||||||
"service": "alarm_control_panel.alarm_arm_away",
|
"service": "alarm_control_panel.alarm_arm_away",
|
||||||
"entity_id": "alarm_control_panel.test",
|
"entity_id": "alarm_control_panel.test",
|
||||||
"data": {"code": "1234"},
|
"data": {"code": "{{ this.entity_id }}"},
|
||||||
},
|
},
|
||||||
"arm_home": {
|
"arm_home": {
|
||||||
"service": "alarm_control_panel.alarm_arm_home",
|
"service": "alarm_control_panel.alarm_arm_home",
|
||||||
"entity_id": "alarm_control_panel.test",
|
"entity_id": "alarm_control_panel.test",
|
||||||
"data": {"code": "1234"},
|
"data": {"code": "{{ this.entity_id }}"},
|
||||||
},
|
},
|
||||||
"arm_night": {
|
"arm_night": {
|
||||||
"service": "alarm_control_panel.alarm_arm_night",
|
"service": "alarm_control_panel.alarm_arm_night",
|
||||||
"entity_id": "alarm_control_panel.test",
|
"entity_id": "alarm_control_panel.test",
|
||||||
"data": {"code": "1234"},
|
"data": {"code": "{{ this.entity_id }}"},
|
||||||
},
|
},
|
||||||
"disarm": {
|
"disarm": {
|
||||||
"service": "alarm_control_panel.alarm_disarm",
|
"service": "alarm_control_panel.alarm_disarm",
|
||||||
"entity_id": "alarm_control_panel.test",
|
"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()
|
await hass.async_block_till_done()
|
||||||
assert len(service_calls) == 1
|
assert len(service_calls) == 1
|
||||||
assert service_calls[0].data["service"] == service
|
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")])
|
@pytest.mark.parametrize("count,domain", [(1, "alarm_control_panel")])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user