2018-10-11 10:53:54 +00:00
|
|
|
"""The tests for the Template lock platform."""
|
2020-10-16 11:25:57 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-10-11 10:53:54 +00:00
|
|
|
from homeassistant import setup
|
|
|
|
from homeassistant.components import lock
|
2019-12-08 20:05:08 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
from tests.common import assert_setup_component, async_mock_service
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
|
|
|
"""Track calls to a mock service."""
|
|
|
|
return async_mock_service(hass, "test", "automation")
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
async def test_template_state(hass):
|
|
|
|
"""Test template."""
|
|
|
|
with assert_setup_component(1, lock.DOMAIN):
|
|
|
|
assert await setup.async_setup_component(
|
|
|
|
hass,
|
|
|
|
lock.DOMAIN,
|
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"name": "Test template lock",
|
|
|
|
"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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
hass.states.async_set("switch.test_state", STATE_ON)
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
state = hass.states.get("lock.test_template_lock")
|
|
|
|
assert state.state == lock.STATE_LOCKED
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
hass.states.async_set("switch.test_state", STATE_OFF)
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
state = hass.states.get("lock.test_template_lock")
|
|
|
|
assert state.state == lock.STATE_UNLOCKED
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
async def test_template_state_boolean_on(hass):
|
|
|
|
"""Test the setting of the state with boolean on."""
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
state = hass.states.get("lock.template_lock")
|
|
|
|
assert state.state == lock.STATE_LOCKED
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
|
|
|
|
async def test_template_state_boolean_off(hass):
|
|
|
|
"""Test the setting of the state with off."""
|
|
|
|
with assert_setup_component(1, lock.DOMAIN):
|
|
|
|
assert await setup.async_setup_component(
|
|
|
|
hass,
|
|
|
|
lock.DOMAIN,
|
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"value_template": "{{ 1 == 2 }}",
|
2019-07-31 19:25:30 +00:00
|
|
|
"lock": {
|
2020-10-16 11:25:57 +00:00
|
|
|
"service": "switch.turn_on",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
state = hass.states.get("lock.template_lock")
|
|
|
|
assert state.state == lock.STATE_UNLOCKED
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
|
|
|
|
async def test_template_syntax_error(hass):
|
|
|
|
"""Test templating syntax error."""
|
|
|
|
with assert_setup_component(0, lock.DOMAIN):
|
|
|
|
assert await setup.async_setup_component(
|
|
|
|
hass,
|
|
|
|
lock.DOMAIN,
|
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"value_template": "{% if rubbish %}",
|
2019-07-31 19:25:30 +00:00
|
|
|
"lock": {
|
2020-10-16 11:25:57 +00:00
|
|
|
"service": "switch.turn_on",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
assert hass.states.async_all() == []
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
2020-10-16 11:25:57 +00:00
|
|
|
"switch": {
|
|
|
|
"platform": "lock",
|
|
|
|
"name": "{{%}",
|
|
|
|
"value_template": "{{ rubbish }",
|
|
|
|
"lock": {
|
|
|
|
"service": "switch.turn_on",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
2018-10-11 10:53:54 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
assert hass.states.async_all() == []
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
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"}},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.async_all() == []
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
|
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
2020-10-16 11:25:57 +00:00
|
|
|
"not_value_template": "{{ states.switch.test_state.state }}",
|
2019-07-31 19:25:30 +00:00
|
|
|
"lock": {
|
|
|
|
"service": "switch.turn_on",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
2020-10-16 11:25:57 +00:00
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
2018-10-11 10:53:54 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
assert hass.states.async_all() == []
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
async def test_template_static(hass, caplog):
|
|
|
|
"""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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2020-10-16 11:25:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("lock.template_lock")
|
|
|
|
assert state.state == lock.STATE_UNLOCKED
|
|
|
|
|
|
|
|
hass.states.async_set("lock.template_lock", lock.STATE_LOCKED)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("lock.template_lock")
|
|
|
|
assert state.state == lock.STATE_LOCKED
|
|
|
|
|
|
|
|
|
|
|
|
async def test_lock_action(hass, calls):
|
|
|
|
"""Test lock action."""
|
|
|
|
assert await setup.async_setup_component(
|
|
|
|
hass,
|
|
|
|
lock.DOMAIN,
|
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"value_template": "{{ states.switch.test_state.state }}",
|
|
|
|
"lock": {"service": "test.automation"},
|
|
|
|
"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()
|
|
|
|
|
|
|
|
hass.states.async_set("switch.test_state", STATE_OFF)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("lock.template_lock")
|
|
|
|
assert state.state == lock.STATE_UNLOCKED
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
lock.DOMAIN, lock.SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.template_lock"}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unlock_action(hass, calls):
|
|
|
|
"""Test unlock action."""
|
|
|
|
assert await setup.async_setup_component(
|
|
|
|
hass,
|
|
|
|
lock.DOMAIN,
|
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"value_template": "{{ states.switch.test_state.state }}",
|
|
|
|
"lock": {
|
|
|
|
"service": "switch.turn_on",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
"unlock": {"service": "test.automation"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("lock.template_lock")
|
|
|
|
assert state.state == lock.STATE_LOCKED
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
lock.DOMAIN, lock.SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.template_lock"}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
2019-10-01 12:15:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_available_template_with_entities(hass):
|
|
|
|
"""Test availability templates with values from other entities."""
|
|
|
|
|
|
|
|
await setup.async_setup_component(
|
|
|
|
hass,
|
2020-10-16 11:25:57 +00:00
|
|
|
lock.DOMAIN,
|
2019-10-01 12:15:15 +00:00
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
2019-11-26 00:30:49 +00:00
|
|
|
"value_template": "{{ states('switch.test_state') }}",
|
2019-10-01 12:15:15 +00:00
|
|
|
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
"availability_template": "{{ is_state('availability_state.state', 'on') }}",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-10-01 12:15:15 +00:00
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# When template returns true..
|
|
|
|
hass.states.async_set("availability_state.state", STATE_ON)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Device State should not be unavailable
|
|
|
|
assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
# When Availability template returns false
|
|
|
|
hass.states.async_set("availability_state.state", STATE_OFF)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# device state should be unavailable
|
|
|
|
assert hass.states.get("lock.template_lock").state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
|
|
|
|
async def test_invalid_availability_template_keeps_component_available(hass, caplog):
|
|
|
|
"""Test that an invalid availability keeps the device available."""
|
|
|
|
await setup.async_setup_component(
|
|
|
|
hass,
|
2020-10-16 11:25:57 +00:00
|
|
|
lock.DOMAIN,
|
2019-10-01 12:15:15 +00:00
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"value_template": "{{ 1 + 1 }}",
|
|
|
|
"availability_template": "{{ x - 12 }}",
|
|
|
|
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-10-01 12:15:15 +00:00
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE
|
|
|
|
assert ("UndefinedError: 'x' is undefined") in caplog.text
|
2020-08-01 22:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unique_id(hass):
|
|
|
|
"""Test unique_id option only creates one lock per id."""
|
|
|
|
await setup.async_setup_component(
|
|
|
|
hass,
|
2020-10-16 11:25:57 +00:00
|
|
|
lock.DOMAIN,
|
2020-08-01 22:45:55 +00:00
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"name": "test_template_lock_01",
|
|
|
|
"unique_id": "not-so-unique-anymore",
|
|
|
|
"value_template": "{{ true }}",
|
|
|
|
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
|
|
|
"unlock": {
|
|
|
|
"service": "switch.turn_off",
|
|
|
|
"entity_id": "switch.test_state",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
await setup.async_setup_component(
|
|
|
|
hass,
|
2020-10-16 11:25:57 +00:00
|
|
|
lock.DOMAIN,
|
2020-08-01 22:45:55 +00:00
|
|
|
{
|
|
|
|
"lock": {
|
|
|
|
"platform": "template",
|
|
|
|
"name": "test_template_lock_02",
|
|
|
|
"unique_id": "not-so-unique-anymore",
|
|
|
|
"value_template": "{{ false }}",
|
|
|
|
"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 len(hass.states.async_all()) == 1
|