mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Update template/test_trigger.py to use pytest (#55950)
This commit is contained in:
parent
dec7877671
commit
ac1251c52b
@ -12,33 +12,20 @@ from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component,
|
||||
async_fire_time_changed,
|
||||
async_mock_service,
|
||||
mock_component,
|
||||
)
|
||||
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
from tests.common import async_fire_time_changed, mock_component
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
def setup_comp(hass, calls):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
|
||||
|
||||
async def test_if_fires_on_change_bool(hass, calls):
|
||||
"""Test for firing on boolean change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -51,8 +38,10 @@ async def test_if_fires_on_change_bool(hass, calls):
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_bool(hass, start_ha, calls):
|
||||
"""Test for firing on boolean change."""
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
@ -65,18 +54,17 @@ async def test_if_fires_on_change_bool(hass, calls):
|
||||
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "planet")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["id"] == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_change_str(hass, calls):
|
||||
"""Test for firing on change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config, call_setup",
|
||||
[
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -86,20 +74,9 @@ async def test_if_fires_on_change_str(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_str_crazy(hass, calls):
|
||||
"""Test for firing on change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
[(1, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -109,112 +86,9 @@ async def test_if_fires_on_change_str_crazy(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_when_true_at_setup(hass, calls):
|
||||
"""Test for not firing during startup."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.entity", "hello", force_update=True)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_when_true_at_setup_variables(hass, calls):
|
||||
"""Test for not firing during startup + trigger_variables."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger_variables": {"entity": "test.entity"},
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state(entity|default("test.entity2"), "hello") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
# Assert that the trigger doesn't fire immediately when it's setup
|
||||
# If trigger_variable 'entity' is not passed to initial check at setup, the
|
||||
# trigger will immediately fire
|
||||
hass.states.async_set("test.entity", "hello", force_update=True)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.entity", "goodbye", force_update=True)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# Assert that the trigger fires after state change
|
||||
# If trigger_variable 'entity' is not passed to the template trigger, the
|
||||
# trigger will never fire because it falls back to 'test.entity2'
|
||||
hass.states.async_set("test.entity", "hello", force_update=True)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_because_fail(hass, calls):
|
||||
"""Test for not firing after TemplateError."""
|
||||
hass.states.async_set("test.number", "1")
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ 84 / states.test.number.state|int == 42 }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.number", "2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.number", "0")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.number", "2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_bool(hass, calls):
|
||||
"""Test for not firing on boolean change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
[(1, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -224,36 +98,18 @@ async def test_if_not_fires_on_change_bool(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_str(hass, calls):
|
||||
"""Test for not firing on string change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
[(0, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "true"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_str_crazy(hass, calls):
|
||||
"""Test for not firing on string change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
[(0, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -263,66 +119,9 @@ async def test_if_not_fires_on_change_str_crazy(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_no_change(hass, calls):
|
||||
"""Test for firing on no change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ true }}"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
cur_len = len(calls)
|
||||
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
await hass.async_block_till_done()
|
||||
assert cur_len == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_on_two_change(hass, calls):
|
||||
"""Test for firing on two changes."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ states.test.entity.state == 'world' }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Trigger once
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# Trigger again
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_template(hass, calls):
|
||||
"""Test for firing on change with template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
[(0, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -332,18 +131,9 @@ async def test_if_fires_on_change_with_template(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_with_template(hass, calls):
|
||||
"""Test for not firing on change with template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
[(1, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -353,21 +143,163 @@ async def test_if_not_fires_on_change_with_template(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[(0, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ states.test.entity.state == 'world' }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[(1, "world", False), (1, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
},
|
||||
},
|
||||
[(0, "world", True)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger_variables": {"entity": "test.entity"},
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state(entity|default("test.entity2"), "hello") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
},
|
||||
},
|
||||
[(0, "hello", True), (0, "goodbye", True), (1, "hello", True)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": """{%- if is_state("test.entity", "world") -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}""",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[(0, "worldz", False), (0, "hello", True)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ not is_state("test.entity", "world") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[
|
||||
(0, "world", False),
|
||||
(1, "home", False),
|
||||
(1, "work", False),
|
||||
(1, "not_home", False),
|
||||
(1, "world", False),
|
||||
(2, "home", False),
|
||||
],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ xyz | round(0) }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[(0, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": 0},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[(1, "world", False)],
|
||||
),
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ true }}"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[(0, "hello", False)],
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
async def test_general(hass, call_setup, start_ha, calls):
|
||||
"""Test for firing on change."""
|
||||
assert len(calls) == 0
|
||||
|
||||
for call_len, call_name, call_force in call_setup:
|
||||
hass.states.async_set("test.entity", call_name, force_update=call_force)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == call_len
|
||||
|
||||
async def test_if_fires_on_change_with_template_advanced(hass, calls):
|
||||
"""Test for firing on change with template advanced."""
|
||||
context = Context()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config, call_setup",
|
||||
[
|
||||
(
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ 84 / states.test.number.state|int == 42 }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
[
|
||||
(0, "1"),
|
||||
(1, "2"),
|
||||
(1, "0"),
|
||||
(1, "2"),
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_if_not_fires_because_fail(hass, call_setup, start_ha, calls):
|
||||
"""Test for not firing after TemplateError."""
|
||||
assert len(calls) == 0
|
||||
|
||||
for call_len, call_number in call_setup:
|
||||
hass.states.async_set("test.number", call_number)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == call_len
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -391,8 +323,11 @@ async def test_if_fires_on_change_with_template_advanced(hass, calls):
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_template_advanced(hass, start_ha, calls):
|
||||
"""Test for firing on change with template advanced."""
|
||||
context = Context()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
@ -402,85 +337,10 @@ async def test_if_fires_on_change_with_template_advanced(hass, calls):
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - None"
|
||||
|
||||
|
||||
async def test_if_fires_on_no_change_with_template_advanced(hass, calls):
|
||||
"""Test for firing on no change with template advanced."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": """{%- if is_state("test.entity", "world") -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}""",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Different state
|
||||
hass.states.async_set("test.entity", "worldz")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# Different state
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_template_2(hass, calls):
|
||||
"""Test for firing on change with template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ not is_state("test.entity", "world") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.entity", "home")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "work")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "not_home")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "home")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action(hass, calls):
|
||||
"""Test for firing if action."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
@ -493,8 +353,10 @@ async def test_if_action(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_action(hass, start_ha, calls):
|
||||
"""Test for firing if action."""
|
||||
# Condition is not true yet
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
@ -511,47 +373,26 @@ async def test_if_action(hass, calls):
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_bad_template(hass, calls):
|
||||
"""Test for firing on change with bad template."""
|
||||
with assert_setup_component(0, automation.DOMAIN):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(0, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ "},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_bad_template_2(hass, calls):
|
||||
async def test_if_fires_on_change_with_bad_template(hass, start_ha, calls):
|
||||
"""Test for firing on change with bad template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ xyz | round(0) }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_wait_template_with_trigger(hass, calls):
|
||||
"""Test using wait template with 'trigger.entity_id'."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -579,8 +420,10 @@ async def test_wait_template_with_trigger(hass, calls):
|
||||
],
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_wait_template_with_trigger(hass, start_ha, calls):
|
||||
"""Test using wait template with 'trigger.entity_id'."""
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@callback
|
||||
@ -620,12 +463,10 @@ async def test_if_fires_on_change_with_for(hass, calls):
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
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,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -650,8 +491,11 @@ async def test_if_fires_on_change_with_for_advanced(hass, calls):
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_for_advanced(hass, start_ha, calls):
|
||||
"""Test for firing on change with for advanced."""
|
||||
context = Context()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
@ -664,34 +508,10 @@ async def test_if_fires_on_change_with_for_advanced(hass, calls):
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - 0:00:05"
|
||||
|
||||
|
||||
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 len(calls) == 1
|
||||
|
||||
|
||||
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,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -716,8 +536,11 @@ async def test_if_fires_on_change_with_for_0_advanced(hass, calls):
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_for_0_advanced(hass, start_ha, calls):
|
||||
"""Test for firing on change with for: 0 advanced."""
|
||||
context = Context()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
@ -727,12 +550,10 @@ async def test_if_fires_on_change_with_for_0_advanced(hass, calls):
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - 0:00:00"
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
context = Context()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -757,8 +578,11 @@ async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_for_2(hass, start_ha, calls):
|
||||
"""Test for firing on change with for."""
|
||||
context = Context()
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@ -769,11 +593,10 @@ async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - 0:00:05"
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_with_for(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -784,8 +607,10 @@ async def test_if_not_fires_on_change_with_for(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_not_fires_on_change_with_for(hass, start_ha, calls):
|
||||
"""Test for firing on change with for."""
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@ -800,11 +625,10 @@ async def test_if_not_fires_on_change_with_for(hass, calls):
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_when_turned_off_with_for(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -815,8 +639,10 @@ async def test_if_not_fires_when_turned_off_with_for(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_not_fires_when_turned_off_with_for(hass, start_ha, calls):
|
||||
"""Test for firing on change with for."""
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@ -835,11 +661,10 @@ async def test_if_not_fires_when_turned_off_with_for(hass, calls):
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_1(hass, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -850,8 +675,10 @@ async def test_if_fires_on_change_with_for_template_1(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_1(hass, start_ha, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@ -860,11 +687,10 @@ async def test_if_fires_on_change_with_for_template_1(hass, calls):
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_2(hass, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -875,8 +701,10 @@ async def test_if_fires_on_change_with_for_template_2(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_2(hass, start_ha, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@ -885,11 +713,10 @@ async def test_if_fires_on_change_with_for_template_2(hass, calls):
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_3(hass, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -900,8 +727,10 @@ async def test_if_fires_on_change_with_for_template_3(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_3(hass, start_ha, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@ -910,11 +739,10 @@ async def test_if_fires_on_change_with_for_template_3(hass, calls):
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_invalid_for_template_1(hass, calls):
|
||||
"""Test for invalid for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@pytest.mark.parametrize("count,domain", [(1, automation.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
@ -925,8 +753,10 @@ async def test_invalid_for_template_1(hass, calls):
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
async def test_invalid_for_template_1(hass, start_ha, calls):
|
||||
"""Test for invalid for template."""
|
||||
with mock.patch.object(template_trigger, "_LOGGER") as mock_logger:
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
|
Loading…
x
Reference in New Issue
Block a user