Add missing trigger.for variable to template trigger (#24893)
This commit is contained in:
parent
3f4ce70414
commit
8dca73d08e
2 changed files with 137 additions and 14 deletions
|
@ -35,6 +35,23 @@ async def async_trigger(hass, config, action, automation_info):
|
||||||
"""Listen for state changes and calls action."""
|
"""Listen for state changes and calls action."""
|
||||||
nonlocal unsub_track_same
|
nonlocal unsub_track_same
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def call_action():
|
||||||
|
"""Call action with right context."""
|
||||||
|
hass.async_run_job(action({
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'template',
|
||||||
|
'entity_id': entity_id,
|
||||||
|
'from_state': from_s,
|
||||||
|
'to_state': to_s,
|
||||||
|
'for': time_delta if not time_delta else period
|
||||||
|
},
|
||||||
|
}, context=(to_s.context if to_s else None)))
|
||||||
|
|
||||||
|
if not time_delta:
|
||||||
|
call_action()
|
||||||
|
return
|
||||||
|
|
||||||
variables = {
|
variables = {
|
||||||
'trigger': {
|
'trigger': {
|
||||||
'platform': 'template',
|
'platform': 'template',
|
||||||
|
@ -44,16 +61,6 @@ async def async_trigger(hass, config, action, automation_info):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@callback
|
|
||||||
def call_action():
|
|
||||||
"""Call action with right context."""
|
|
||||||
hass.async_run_job(action(
|
|
||||||
variables, context=(to_s.context if to_s else None)))
|
|
||||||
|
|
||||||
if not time_delta:
|
|
||||||
call_action()
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if isinstance(time_delta, template.Template):
|
if isinstance(time_delta, template.Template):
|
||||||
period = vol.All(
|
period = vol.All(
|
||||||
|
|
|
@ -251,7 +251,7 @@ async def test_if_fires_on_change_with_template_advanced(hass, calls):
|
||||||
'some':
|
'some':
|
||||||
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
||||||
'platform', 'entity_id', 'from_state.state',
|
'platform', 'entity_id', 'from_state.state',
|
||||||
'to_state.state'))
|
'to_state.state', 'for'))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -263,7 +263,7 @@ async def test_if_fires_on_change_with_template_advanced(hass, calls):
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert 1 == len(calls)
|
assert 1 == len(calls)
|
||||||
assert calls[0].context.parent_id == context.id
|
assert calls[0].context.parent_id == context.id
|
||||||
assert 'template - test.entity - hello - world' == \
|
assert 'template - test.entity - hello - world - None' == \
|
||||||
calls[0].data['some']
|
calls[0].data['some']
|
||||||
|
|
||||||
|
|
||||||
|
@ -424,7 +424,7 @@ async def test_wait_template_with_trigger(hass, calls):
|
||||||
'some':
|
'some':
|
||||||
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
||||||
'platform', 'entity_id', 'from_state.state',
|
'platform', 'entity_id', 'from_state.state',
|
||||||
'to_state.state'))
|
'to_state.state', 'for'))
|
||||||
}}
|
}}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
@ -437,7 +437,7 @@ async def test_wait_template_with_trigger(hass, calls):
|
||||||
hass.states.async_set('test.entity', 'hello')
|
hass.states.async_set('test.entity', 'hello')
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert 1 == len(calls)
|
assert 1 == len(calls)
|
||||||
assert 'template - test.entity - hello - world' == \
|
assert 'template - test.entity - hello - world - None' == \
|
||||||
calls[0].data['some']
|
calls[0].data['some']
|
||||||
|
|
||||||
|
|
||||||
|
@ -466,6 +466,122 @@ async def test_if_fires_on_change_with_for(hass, calls):
|
||||||
assert 1 == len(calls)
|
assert 1 == len(calls)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_change_with_for_advanced(hass, calls):
|
||||||
|
"""Test for firing on change with for advanced."""
|
||||||
|
context = Context()
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'template',
|
||||||
|
'value_template': '{{ is_state("test.entity", "world") }}',
|
||||||
|
'for': {
|
||||||
|
'seconds': 5
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation',
|
||||||
|
'data_template': {
|
||||||
|
'some':
|
||||||
|
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
||||||
|
'platform', 'entity_id', 'from_state.state',
|
||||||
|
'to_state.state', 'for'))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 'world', context=context)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 0 == len(calls)
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 1 == len(calls)
|
||||||
|
assert calls[0].context.parent_id == context.id
|
||||||
|
assert 'template - test.entity - hello - world - 0:00:05' == \
|
||||||
|
calls[0].data['some']
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_change_with_for_0(hass, calls):
|
||||||
|
"""Test for firing on change with for: 0."""
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'template',
|
||||||
|
'value_template': "{{ is_state('test.entity', 'world') }}",
|
||||||
|
'for': {
|
||||||
|
'seconds': 0
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 'world')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 1 == len(calls)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_change_with_for_0_advanced(hass, calls):
|
||||||
|
"""Test for firing on change with for: 0 advanced."""
|
||||||
|
context = Context()
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'template',
|
||||||
|
'value_template': '{{ is_state("test.entity", "world") }}',
|
||||||
|
'for': {
|
||||||
|
'seconds': 0
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation',
|
||||||
|
'data_template': {
|
||||||
|
'some':
|
||||||
|
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
||||||
|
'platform', 'entity_id', 'from_state.state',
|
||||||
|
'to_state.state', 'for'))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 'world', context=context)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 1 == len(calls)
|
||||||
|
assert calls[0].context.parent_id == context.id
|
||||||
|
assert 'template - test.entity - hello - world - 0:00:00' == \
|
||||||
|
calls[0].data['some']
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||||
|
"""Test for firing on change with for."""
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'template',
|
||||||
|
'value_template': "{{ is_state('test.entity', 'world') }}",
|
||||||
|
'for': 5,
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 'world')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 0 == len(calls)
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 1 == len(calls)
|
||||||
|
|
||||||
|
|
||||||
async def test_if_not_fires_on_change_with_for(hass, calls):
|
async def test_if_not_fires_on_change_with_for(hass, calls):
|
||||||
"""Test for firing on change with for."""
|
"""Test for firing on change with for."""
|
||||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue