Update template/test_lock.py to use pytest (#56102)

This commit is contained in:
jan iversen 2021-09-11 20:48:25 +02:00 committed by GitHub
parent a9ed4fa405
commit cb8c0cb123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,42 +5,30 @@ from homeassistant import setup
from homeassistant.components import lock from homeassistant.components import lock
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from tests.common import assert_setup_component, async_mock_service
@pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
@pytest.fixture @pytest.mark.parametrize(
def calls(hass): "config",
"""Track calls to a mock service.""" [
return async_mock_service(hass, "test", "automation") {
lock.DOMAIN: {
"platform": "template",
async def test_template_state(hass): "name": "Test template lock",
"""Test template.""" "value_template": "{{ states.switch.test_state.state }}",
with assert_setup_component(1, lock.DOMAIN):
assert await setup.async_setup_component(
hass,
lock.DOMAIN,
{
"lock": { "lock": {
"platform": "template", "service": "switch.turn_on",
"name": "Test template lock", "entity_id": "switch.test_state",
"value_template": "{{ states.switch.test_state.state }}", },
"lock": { "unlock": {
"service": "switch.turn_on", "service": "switch.turn_off",
"entity_id": "switch.test_state", "entity_id": "switch.test_state",
}, },
"unlock": { }
"service": "switch.turn_off", },
"entity_id": "switch.test_state", ],
}, )
} async def test_template_state(hass, start_ha):
}, """Test template."""
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
hass.states.async_set("switch.test_state", STATE_ON) hass.states.async_set("switch.test_state", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -54,196 +42,135 @@ async def test_template_state(hass):
assert state.state == lock.STATE_UNLOCKED assert state.state == lock.STATE_UNLOCKED
async def test_template_state_boolean_on(hass): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test the setting of the state with boolean on.""" @pytest.mark.parametrize(
with assert_setup_component(1, lock.DOMAIN): "config",
assert await setup.async_setup_component( [
hass, {
lock.DOMAIN, lock.DOMAIN: {
{ "platform": "template",
"value_template": "{{ 1 == 1 }}",
"lock": { "lock": {
"platform": "template", "service": "switch.turn_on",
"value_template": "{{ 1 == 1 }}", "entity_id": "switch.test_state",
"lock": { },
"service": "switch.turn_on", "unlock": {
"entity_id": "switch.test_state", "service": "switch.turn_off",
}, "entity_id": "switch.test_state",
"unlock": { },
"service": "switch.turn_off", }
"entity_id": "switch.test_state", },
}, ],
} )
}, async def test_template_state_boolean_on(hass, start_ha):
) """Test the setting of the state with boolean on."""
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get("lock.template_lock") state = hass.states.get("lock.template_lock")
assert state.state == lock.STATE_LOCKED assert state.state == lock.STATE_LOCKED
async def test_template_state_boolean_off(hass): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test the setting of the state with off.""" @pytest.mark.parametrize(
with assert_setup_component(1, lock.DOMAIN): "config",
assert await setup.async_setup_component( [
hass, {
lock.DOMAIN, lock.DOMAIN: {
{ "platform": "template",
"value_template": "{{ 1 == 2 }}",
"lock": { "lock": {
"platform": "template", "service": "switch.turn_on",
"value_template": "{{ 1 == 2 }}", "entity_id": "switch.test_state",
"lock": { },
"service": "switch.turn_on", "unlock": {
"entity_id": "switch.test_state", "service": "switch.turn_off",
}, "entity_id": "switch.test_state",
"unlock": { },
"service": "switch.turn_off", }
"entity_id": "switch.test_state", },
}, ],
} )
}, async def test_template_state_boolean_off(hass, start_ha):
) """Test the setting of the state with off."""
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get("lock.template_lock") state = hass.states.get("lock.template_lock")
assert state.state == lock.STATE_UNLOCKED assert state.state == lock.STATE_UNLOCKED
async def test_template_syntax_error(hass): @pytest.mark.parametrize("count,domain", [(0, lock.DOMAIN)])
@pytest.mark.parametrize(
"config",
[
{
lock.DOMAIN: {
"platform": "template",
"value_template": "{% if rubbish %}",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
},
{
"switch": {
"platform": "lock",
"name": "{{%}",
"value_template": "{{ rubbish }",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
},
},
{lock.DOMAIN: {"platform": "template", "value_template": "Invalid"}},
{
lock.DOMAIN: {
"platform": "template",
"not_value_template": "{{ states.switch.test_state.state }}",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
},
],
)
async def test_template_syntax_error(hass, start_ha):
"""Test templating syntax error.""" """Test templating syntax error."""
with assert_setup_component(0, lock.DOMAIN): assert hass.states.async_all() == []
assert await setup.async_setup_component(
hass,
lock.DOMAIN, @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
{ @pytest.mark.parametrize(
"config",
[
{
lock.DOMAIN: {
"platform": "template",
"value_template": "{{ 1 + 1 }}",
"lock": { "lock": {
"platform": "template", "service": "switch.turn_on",
"value_template": "{% if rubbish %}", "entity_id": "switch.test_state",
"lock": { },
"service": "switch.turn_on", "unlock": {
"entity_id": "switch.test_state", "service": "switch.turn_off",
}, "entity_id": "switch.test_state",
"unlock": { },
"service": "switch.turn_off", }
"entity_id": "switch.test_state", },
}, ],
} )
}, async def test_template_static(hass, start_ha):
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_invalid_name_does_not_create(hass):
"""Test invalid name."""
with assert_setup_component(0, lock.DOMAIN):
assert await setup.async_setup_component(
hass,
lock.DOMAIN,
{
"switch": {
"platform": "lock",
"name": "{{%}",
"value_template": "{{ rubbish }",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_invalid_lock_does_not_create(hass):
"""Test invalid lock."""
with assert_setup_component(0, lock.DOMAIN):
assert await setup.async_setup_component(
hass,
lock.DOMAIN,
{"lock": {"platform": "template", "value_template": "Invalid"}},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_missing_template_does_not_create(hass):
"""Test missing template."""
with assert_setup_component(0, lock.DOMAIN):
assert await setup.async_setup_component(
hass,
lock.DOMAIN,
{
"lock": {
"platform": "template",
"not_value_template": "{{ states.switch.test_state.state }}",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_template_static(hass, caplog):
"""Test that we allow static templates.""" """Test that we allow static templates."""
with assert_setup_component(1, lock.DOMAIN):
assert await setup.async_setup_component(
hass,
lock.DOMAIN,
{
"lock": {
"platform": "template",
"value_template": "{{ 1 + 1 }}",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get("lock.template_lock") state = hass.states.get("lock.template_lock")
assert state.state == lock.STATE_UNLOCKED assert state.state == lock.STATE_UNLOCKED
@ -253,13 +180,12 @@ async def test_template_static(hass, caplog):
assert state.state == lock.STATE_LOCKED assert state.state == lock.STATE_LOCKED
async def test_lock_action(hass, calls): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test lock action.""" @pytest.mark.parametrize(
assert await setup.async_setup_component( "config",
hass, [
lock.DOMAIN,
{ {
"lock": { lock.DOMAIN: {
"platform": "template", "platform": "template",
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"lock": {"service": "test.automation"}, "lock": {"service": "test.automation"},
@ -269,12 +195,10 @@ async def test_lock_action(hass, calls):
}, },
} }
}, },
) ],
)
await hass.async_block_till_done() async def test_lock_action(hass, start_ha, calls):
await hass.async_start() """Test lock action."""
await hass.async_block_till_done()
hass.states.async_set("switch.test_state", STATE_OFF) hass.states.async_set("switch.test_state", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -289,13 +213,12 @@ async def test_lock_action(hass, calls):
assert len(calls) == 1 assert len(calls) == 1
async def test_unlock_action(hass, calls): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test unlock action.""" @pytest.mark.parametrize(
assert await setup.async_setup_component( "config",
hass, [
lock.DOMAIN,
{ {
"lock": { lock.DOMAIN: {
"platform": "template", "platform": "template",
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"lock": { "lock": {
@ -305,12 +228,10 @@ async def test_unlock_action(hass, calls):
"unlock": {"service": "test.automation"}, "unlock": {"service": "test.automation"},
} }
}, },
) ],
)
await hass.async_block_till_done() async def test_unlock_action(hass, start_ha, calls):
await hass.async_start() """Test unlock action."""
await hass.async_block_till_done()
hass.states.async_set("switch.test_state", STATE_ON) hass.states.async_set("switch.test_state", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -325,92 +246,38 @@ async def test_unlock_action(hass, calls):
assert len(calls) == 1 assert len(calls) == 1
async def test_unlocking(hass, calls): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
@pytest.mark.parametrize(
"config",
[
{
lock.DOMAIN: {
"platform": "template",
"value_template": "{{ states.input_select.test_state.state }}",
"lock": {"service": "test.automation"},
"unlock": {"service": "test.automation"},
}
},
],
)
@pytest.mark.parametrize(
"test_state", [lock.STATE_UNLOCKING, lock.STATE_LOCKING, lock.STATE_JAMMED]
)
async def test_lock_state(hass, test_state, start_ha):
"""Test unlocking.""" """Test unlocking."""
assert await setup.async_setup_component( hass.states.async_set("input_select.test_state", test_state)
hass,
lock.DOMAIN,
{
"lock": {
"platform": "template",
"value_template": "{{ states.input_select.test_state.state }}",
"lock": {"service": "test.automation"},
"unlock": {"service": "test.automation"},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
hass.states.async_set("input_select.test_state", lock.STATE_UNLOCKING)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("lock.template_lock") state = hass.states.get("lock.template_lock")
assert state.state == lock.STATE_UNLOCKING assert state.state == test_state
async def test_locking(hass, calls): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test unlocking.""" @pytest.mark.parametrize(
assert await setup.async_setup_component( "config",
hass, [
lock.DOMAIN,
{ {
"lock": { lock.DOMAIN: {
"platform": "template",
"value_template": "{{ states.input_select.test_state.state }}",
"lock": {"service": "test.automation"},
"unlock": {"service": "test.automation"},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
hass.states.async_set("input_select.test_state", lock.STATE_LOCKING)
await hass.async_block_till_done()
state = hass.states.get("lock.template_lock")
assert state.state == lock.STATE_LOCKING
async def test_jammed(hass, calls):
"""Test jammed."""
assert await setup.async_setup_component(
hass,
lock.DOMAIN,
{
"lock": {
"platform": "template",
"value_template": "{{ states.input_select.test_state.state }}",
"lock": {"service": "test.automation"},
"unlock": {"service": "test.automation"},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
hass.states.async_set("input_select.test_state", lock.STATE_JAMMED)
await hass.async_block_till_done()
state = hass.states.get("lock.template_lock")
assert state.state == lock.STATE_JAMMED
async def test_available_template_with_entities(hass):
"""Test availability templates with values from other entities."""
await setup.async_setup_component(
hass,
lock.DOMAIN,
{
"lock": {
"platform": "template", "platform": "template",
"value_template": "{{ states('switch.test_state') }}", "value_template": "{{ states('switch.test_state') }}",
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"}, "lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
@ -421,12 +288,10 @@ async def test_available_template_with_entities(hass):
"availability_template": "{{ is_state('availability_state.state', 'on') }}", "availability_template": "{{ is_state('availability_state.state', 'on') }}",
} }
}, },
) ],
)
await hass.async_block_till_done() async def test_available_template_with_entities(hass, start_ha):
await hass.async_start() """Test availability templates with values from other entities."""
await hass.async_block_till_done()
# When template returns true.. # When template returns true..
hass.states.async_set("availability_state.state", STATE_ON) hass.states.async_set("availability_state.state", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -442,13 +307,12 @@ async def test_available_template_with_entities(hass):
assert hass.states.get("lock.template_lock").state == STATE_UNAVAILABLE assert hass.states.get("lock.template_lock").state == STATE_UNAVAILABLE
async def test_invalid_availability_template_keeps_component_available(hass, caplog): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test that an invalid availability keeps the device available.""" @pytest.mark.parametrize(
await setup.async_setup_component( "config",
hass, [
lock.DOMAIN,
{ {
"lock": { lock.DOMAIN: {
"platform": "template", "platform": "template",
"value_template": "{{ 1 + 1 }}", "value_template": "{{ 1 + 1 }}",
"availability_template": "{{ x - 12 }}", "availability_template": "{{ x - 12 }}",
@ -459,23 +323,22 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
}, },
} }
}, },
) ],
)
await hass.async_block_till_done() async def test_invalid_availability_template_keeps_component_available(
await hass.async_start() hass, start_ha, caplog_setup_text
await hass.async_block_till_done() ):
"""Test that an invalid availability keeps the device available."""
assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE
assert ("UndefinedError: 'x' is undefined") in caplog.text assert ("UndefinedError: 'x' is undefined") in caplog_setup_text
async def test_unique_id(hass): @pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
"""Test unique_id option only creates one lock per id.""" @pytest.mark.parametrize(
await setup.async_setup_component( "config",
hass, [
lock.DOMAIN,
{ {
"lock": { lock.DOMAIN: {
"platform": "template", "platform": "template",
"name": "test_template_lock_01", "name": "test_template_lock_01",
"unique_id": "not-so-unique-anymore", "unique_id": "not-so-unique-anymore",
@ -485,10 +348,12 @@ async def test_unique_id(hass):
"service": "switch.turn_off", "service": "switch.turn_off",
"entity_id": "switch.test_state", "entity_id": "switch.test_state",
}, },
}, }
}, },
) ],
)
async def test_unique_id(hass, start_ha):
"""Test unique_id option only creates one lock per id."""
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
lock.DOMAIN, lock.DOMAIN,