core/tests/components/template/test_lock.py

397 lines
13 KiB
Python
Raw Normal View History

"""The tests for the Template lock platform."""
import logging
from homeassistant import setup
from homeassistant.components import lock
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import callback
from tests.common import assert_setup_component, get_test_home_assistant
_LOGGER = logging.getLogger(__name__)
class TestTemplateLock:
"""Test the Template lock."""
hass = None
calls = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls."""
self.calls.append(service)
2019-07-31 19:25:30 +00:00
self.hass.services.register("test", "automation", record_call)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template_state(self):
"""Test template."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(1, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"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",
},
}
2019-07-31 19:25:30 +00:00
},
)
self.hass.start()
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.hass.states.set("switch.test_state", STATE_ON)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.test_template_lock")
assert state.state == lock.STATE_LOCKED
2019-07-31 19:25:30 +00:00
self.hass.states.set("switch.test_state", STATE_OFF)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.test_template_lock")
assert state.state == lock.STATE_UNLOCKED
def test_template_state_boolean_on(self):
"""Test the setting of the state with boolean on."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(1, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"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
},
)
self.hass.start()
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.template_lock")
assert state.state == lock.STATE_LOCKED
def test_template_state_boolean_off(self):
"""Test the setting of the state with off."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(1, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"lock": {
"platform": "template",
"value_template": "{{ 1 == 2 }}",
"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
},
)
self.hass.start()
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.template_lock")
assert state.state == lock.STATE_UNLOCKED
def test_template_syntax_error(self):
"""Test templating syntax error."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(0, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"lock": {
"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",
},
}
2019-07-31 19:25:30 +00:00
},
)
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_name_does_not_create(self):
"""Test invalid name."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(0, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"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",
},
}
2019-07-31 19:25:30 +00:00
},
)
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_invalid_lock_does_not_create(self):
"""Test invalid lock."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(0, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{"lock": {"platform": "template", "value_template": "Invalid"}},
)
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_missing_template_does_not_create(self):
"""Test missing template."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(0, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"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",
},
}
2019-07-31 19:25:30 +00:00
},
)
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == []
def test_no_template_match_all(self, caplog):
"""Test that we do not allow locks that match on all."""
2019-07-31 19:25:30 +00:00
with assert_setup_component(1, "lock"):
assert setup.setup_component(
self.hass,
"lock",
{
"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
},
)
self.hass.start()
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.template_lock")
assert state.state == lock.STATE_UNLOCKED
2019-07-31 19:25:30 +00:00
assert (
Refactor template components to extract common routines (#27064) * Added availability_template to Template Cover platform * Added availability_template to Template Binary Sensor platform * Added availability_template to Template Fan platform * Added availability_template to Template Light platform * Added availability_template to Template Sensor platform * Added availability_template to Template Switch platform * Added availability_template to Template Vacuum platform * Added availability_template to Template Lock platform * Added to test for invalid values in availability_template * Black and Lint fix * black formatting * Added to test for invalid values in availability_template * black * Added to test for invalid values in availability_template * Added to test for invalid values in availability_template * simplified exception handler * Fixed Entity discovery big and coverage * Added to test for invalid values in availability_template * flake8 * fixed component ID in test * Added to test for invalid values in availability_template * Added to test for invalid values in availability_template * Made availability_template redering erorr more concise * Cleaned template setup * I'll remember to run black every time one of these days... * Refactored Template initialisation * Refactored Template initialisation * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fixed linting issues * Moved availability_template rendering to common loop * Moved availability_template rendering to common loop * Moved availability_template rendering to common loop * Moved availability_template rendering to common loop * Removed 'Magic' string * Removed 'Magic' string and removed duplicate code * Removed 'Magic' string * Removed 'Magic' string * Brought contant into line * Refactored availability_tempalte rendering to common loop * Removed 'Magic' string * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * Fixed tests (magic values and state checks) * Fixed tests (magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Removed duplicate * Clean up and remove debug * Reverted Dev Container Change
2019-11-26 00:30:49 +00:00
"Template lock 'Template Lock' has no entity ids configured to track "
"nor were we able to extract the entities to track from the value "
"template(s). This entity will only be able to be updated manually"
2019-07-31 19:25:30 +00:00
) in caplog.text
2019-07-31 19:25:30 +00:00
self.hass.states.set("lock.template_lock", lock.STATE_LOCKED)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.template_lock")
assert state.state == lock.STATE_LOCKED
def test_lock_action(self):
"""Test lock action."""
2019-07-31 19:25:30 +00:00
assert setup.setup_component(
self.hass,
"lock",
{
"lock": {
"platform": "template",
"value_template": "{{ states.switch.test_state.state }}",
"lock": {"service": "test.automation"},
"unlock": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
2019-07-31 19:25:30 +00:00
},
)
self.hass.start()
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.hass.states.set("switch.test_state", STATE_OFF)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.template_lock")
assert state.state == lock.STATE_UNLOCKED
2019-07-31 19:25:30 +00:00
self.hass.services.call(
lock.DOMAIN, lock.SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.template_lock"}
)
self.hass.block_till_done()
assert len(self.calls) == 1
def test_unlock_action(self):
"""Test unlock action."""
2019-07-31 19:25:30 +00:00
assert setup.setup_component(
self.hass,
"lock",
{
"lock": {
"platform": "template",
"value_template": "{{ states.switch.test_state.state }}",
"lock": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"unlock": {"service": "test.automation"},
}
2019-07-31 19:25:30 +00:00
},
)
self.hass.start()
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.hass.states.set("switch.test_state", STATE_ON)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
state = self.hass.states.get("lock.template_lock")
assert state.state == lock.STATE_LOCKED
2019-07-31 19:25:30 +00:00
self.hass.services.call(
lock.DOMAIN, lock.SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.template_lock"}
)
self.hass.block_till_done()
assert len(self.calls) == 1
async def test_available_template_with_entities(hass):
"""Test availability templates with values from other entities."""
await setup.async_setup_component(
hass,
"lock",
{
"lock": {
"platform": "template",
Refactor template components to extract common routines (#27064) * Added availability_template to Template Cover platform * Added availability_template to Template Binary Sensor platform * Added availability_template to Template Fan platform * Added availability_template to Template Light platform * Added availability_template to Template Sensor platform * Added availability_template to Template Switch platform * Added availability_template to Template Vacuum platform * Added availability_template to Template Lock platform * Added to test for invalid values in availability_template * Black and Lint fix * black formatting * Added to test for invalid values in availability_template * black * Added to test for invalid values in availability_template * Added to test for invalid values in availability_template * simplified exception handler * Fixed Entity discovery big and coverage * Added to test for invalid values in availability_template * flake8 * fixed component ID in test * Added to test for invalid values in availability_template * Added to test for invalid values in availability_template * Made availability_template redering erorr more concise * Cleaned template setup * I'll remember to run black every time one of these days... * Refactored Template initialisation * Refactored Template initialisation * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Updated AVAILABILITY_TEMPLATE Rendering error * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Moved const to package Const.py * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fix import order (pylint) * Fixed linting issues * Moved availability_template rendering to common loop * Moved availability_template rendering to common loop * Moved availability_template rendering to common loop * Moved availability_template rendering to common loop * Removed 'Magic' string * Removed 'Magic' string and removed duplicate code * Removed 'Magic' string * Removed 'Magic' string * Brought contant into line * Refactored availability_tempalte rendering to common loop * Removed 'Magic' string * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * Cleaned up const and compare lowercase result to 'true' * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * reverted _available back to boolean * Fixed tests (magic values and state checks) * Fixed tests (magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Fixed tests (async, magic values and state checks) * Removed duplicate * Clean up and remove debug * Reverted Dev Container Change
2019-11-26 00:30:49 +00:00
"value_template": "{{ states('switch.test_state') }}",
"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') }}",
}
},
)
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,
"lock",
{
"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",
},
}
},
)
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