mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Support state trigger with from/for but no to (#39480)
This commit is contained in:
parent
8a3279a5c9
commit
4b84b74b89
@ -37,7 +37,10 @@ TRIGGER_SCHEMA = vol.All(
|
|||||||
vol.Optional(CONF_ATTRIBUTE): cv.match_all,
|
vol.Optional(CONF_ATTRIBUTE): cv.match_all,
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
cv.key_dependency(CONF_FOR, CONF_TO),
|
vol.Any(
|
||||||
|
cv.key_dependency(CONF_FOR, CONF_TO),
|
||||||
|
cv.key_dependency(CONF_FOR, CONF_FROM),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -141,6 +144,9 @@ async def async_attach_trigger(
|
|||||||
else:
|
else:
|
||||||
cur_value = new_st.attributes.get(attribute)
|
cur_value = new_st.attributes.get(attribute)
|
||||||
|
|
||||||
|
if CONF_TO not in config:
|
||||||
|
return cur_value != old_value
|
||||||
|
|
||||||
return cur_value == new_value
|
return cur_value == new_value
|
||||||
|
|
||||||
unsub_track_same[entity] = async_track_same_state(
|
unsub_track_same[entity] = async_track_same_state(
|
||||||
|
@ -925,6 +925,64 @@ async def test_if_fires_on_change_with_for_template_3(hass, calls):
|
|||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_change_from_with_for(hass, calls):
|
||||||
|
"""Test for firing on change with from/for."""
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: {
|
||||||
|
"trigger": {
|
||||||
|
"platform": "state",
|
||||||
|
"entity_id": "media_player.foo",
|
||||||
|
"from": "playing",
|
||||||
|
"for": "00:00:30",
|
||||||
|
},
|
||||||
|
"action": {"service": "test.automation"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.states.async_set("media_player.foo", "playing")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
hass.states.async_set("media_player.foo", "paused")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
hass.states.async_set("media_player.foo", "stopped")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=1))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_not_fires_on_change_from_with_for(hass, calls):
|
||||||
|
"""Test for firing on change with from/for."""
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: {
|
||||||
|
"trigger": {
|
||||||
|
"platform": "state",
|
||||||
|
"entity_id": "media_player.foo",
|
||||||
|
"from": "playing",
|
||||||
|
"for": "00:00:30",
|
||||||
|
},
|
||||||
|
"action": {"service": "test.automation"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.states.async_set("media_player.foo", "playing")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
hass.states.async_set("media_player.foo", "paused")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
hass.states.async_set("media_player.foo", "playing")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=1))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_invalid_for_template_1(hass, calls):
|
async def test_invalid_for_template_1(hass, calls):
|
||||||
"""Test for invalid for template."""
|
"""Test for invalid for template."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user