mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Improve template test lock (#41195)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
30c20f362e
commit
91042e3e46
@ -1,45 +1,29 @@
|
||||
"""The tests for the Template lock platform."""
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
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
|
||||
from tests.common import assert_setup_component, async_mock_service
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestTemplateLock:
|
||||
"""Test the Template lock."""
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
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)
|
||||
|
||||
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):
|
||||
async def test_template_state(hass):
|
||||
"""Test template."""
|
||||
with assert_setup_component(1, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(1, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -57,28 +41,29 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("switch.test_state", STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("switch.test_state", STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.test_template_lock")
|
||||
state = hass.states.get("lock.test_template_lock")
|
||||
assert state.state == lock.STATE_LOCKED
|
||||
|
||||
self.hass.states.set("switch.test_state", STATE_OFF)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("switch.test_state", STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.test_template_lock")
|
||||
state = hass.states.get("lock.test_template_lock")
|
||||
assert state.state == lock.STATE_UNLOCKED
|
||||
|
||||
def test_template_state_boolean_on(self):
|
||||
|
||||
async def test_template_state_boolean_on(hass):
|
||||
"""Test the setting of the state with boolean on."""
|
||||
with assert_setup_component(1, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(1, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -95,19 +80,20 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.template_lock")
|
||||
state = hass.states.get("lock.template_lock")
|
||||
assert state.state == lock.STATE_LOCKED
|
||||
|
||||
def test_template_state_boolean_off(self):
|
||||
|
||||
async def test_template_state_boolean_off(hass):
|
||||
"""Test the setting of the state with off."""
|
||||
with assert_setup_component(1, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(1, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -124,19 +110,20 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.template_lock")
|
||||
state = hass.states.get("lock.template_lock")
|
||||
assert state.state == lock.STATE_UNLOCKED
|
||||
|
||||
def test_template_syntax_error(self):
|
||||
|
||||
async def test_template_syntax_error(hass):
|
||||
"""Test templating syntax error."""
|
||||
with assert_setup_component(0, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(0, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -153,18 +140,19 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
assert hass.states.async_all() == []
|
||||
|
||||
def test_invalid_name_does_not_create(self):
|
||||
|
||||
async def test_invalid_name_does_not_create(hass):
|
||||
"""Test invalid name."""
|
||||
with assert_setup_component(0, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(0, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"switch": {
|
||||
"platform": "lock",
|
||||
@ -182,33 +170,35 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
assert hass.states.async_all() == []
|
||||
|
||||
def test_invalid_lock_does_not_create(self):
|
||||
|
||||
async def test_invalid_lock_does_not_create(hass):
|
||||
"""Test invalid lock."""
|
||||
with assert_setup_component(0, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(0, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{"lock": {"platform": "template", "value_template": "Invalid"}},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
assert hass.states.async_all() == []
|
||||
|
||||
def test_missing_template_does_not_create(self):
|
||||
|
||||
async def test_missing_template_does_not_create(hass):
|
||||
"""Test missing template."""
|
||||
with assert_setup_component(0, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(0, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -225,18 +215,19 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
assert hass.states.async_all() == []
|
||||
|
||||
def test_template_static(self, caplog):
|
||||
|
||||
async def test_template_static(hass, caplog):
|
||||
"""Test that we allow static templates."""
|
||||
with assert_setup_component(1, "lock"):
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
with assert_setup_component(1, lock.DOMAIN):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -253,23 +244,24 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.template_lock")
|
||||
state = hass.states.get("lock.template_lock")
|
||||
assert state.state == lock.STATE_UNLOCKED
|
||||
|
||||
self.hass.states.set("lock.template_lock", lock.STATE_LOCKED)
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get("lock.template_lock")
|
||||
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
|
||||
|
||||
def test_lock_action(self):
|
||||
|
||||
async def test_lock_action(hass, calls):
|
||||
"""Test lock action."""
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -283,28 +275,29 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("switch.test_state", STATE_OFF)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("switch.test_state", STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.template_lock")
|
||||
state = hass.states.get("lock.template_lock")
|
||||
assert state.state == lock.STATE_UNLOCKED
|
||||
|
||||
self.hass.services.call(
|
||||
await hass.services.async_call(
|
||||
lock.DOMAIN, lock.SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.template_lock"}
|
||||
)
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
assert len(calls) == 1
|
||||
|
||||
def test_unlock_action(self):
|
||||
|
||||
async def test_unlock_action(hass, calls):
|
||||
"""Test unlock action."""
|
||||
assert setup.setup_component(
|
||||
self.hass,
|
||||
"lock",
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -318,22 +311,22 @@ class TestTemplateLock:
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("switch.test_state", STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("switch.test_state", STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get("lock.template_lock")
|
||||
state = hass.states.get("lock.template_lock")
|
||||
assert state.state == lock.STATE_LOCKED
|
||||
|
||||
self.hass.services.call(
|
||||
await hass.services.async_call(
|
||||
lock.DOMAIN, lock.SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.template_lock"}
|
||||
)
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_available_template_with_entities(hass):
|
||||
@ -341,7 +334,7 @@ async def test_available_template_with_entities(hass):
|
||||
|
||||
await setup.async_setup_component(
|
||||
hass,
|
||||
"lock",
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -379,7 +372,7 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
|
||||
"""Test that an invalid availability keeps the device available."""
|
||||
await setup.async_setup_component(
|
||||
hass,
|
||||
"lock",
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -406,7 +399,7 @@ async def test_unique_id(hass):
|
||||
"""Test unique_id option only creates one lock per id."""
|
||||
await setup.async_setup_component(
|
||||
hass,
|
||||
"lock",
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
@ -424,7 +417,7 @@ async def test_unique_id(hass):
|
||||
|
||||
await setup.async_setup_component(
|
||||
hass,
|
||||
"lock",
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
|
Loading…
x
Reference in New Issue
Block a user