1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 22:18:40 +00:00

Update lock entity to support locking, unlocking, jammed (#51455)

This commit is contained in:
J. Nick Koston
2021-07-20 06:12:56 -10:00
committed by GitHub
parent 0cc4231ac2
commit 9b705ad6df
10 changed files with 418 additions and 27 deletions

View File

@@ -1,4 +1,6 @@
"""The tests for the Demo lock platform."""
import asyncio
import pytest
from homeassistant.components.demo import DOMAIN
@@ -7,8 +9,11 @@ from homeassistant.components.lock import (
SERVICE_LOCK,
SERVICE_OPEN,
SERVICE_UNLOCK,
STATE_JAMMED,
STATE_LOCKED,
STATE_LOCKING,
STATE_UNLOCKED,
STATE_UNLOCKING,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.setup import async_setup_component
@@ -17,6 +22,7 @@ from tests.common import async_mock_service
FRONT = "lock.front_door"
KITCHEN = "lock.kitchen_door"
POORLY_INSTALLED = "lock.poorly_installed_door"
OPENABLE_LOCK = "lock.openable_lock"
@@ -35,9 +41,13 @@ async def test_locking(hass):
assert state.state == STATE_UNLOCKED
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: KITCHEN}, blocking=True
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: KITCHEN}, blocking=False
)
await asyncio.sleep(1)
state = hass.states.get(KITCHEN)
assert state.state == STATE_LOCKING
await asyncio.sleep(2)
state = hass.states.get(KITCHEN)
assert state.state == STATE_LOCKED
@@ -48,17 +58,46 @@ async def test_unlocking(hass):
assert state.state == STATE_LOCKED
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: FRONT}, blocking=True
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: FRONT}, blocking=False
)
await asyncio.sleep(1)
state = hass.states.get(FRONT)
assert state.state == STATE_UNLOCKING
await asyncio.sleep(2)
state = hass.states.get(FRONT)
assert state.state == STATE_UNLOCKED
async def test_opening(hass):
async def test_jammed_when_locking(hass):
"""Test the locking of a lock jams."""
state = hass.states.get(POORLY_INSTALLED)
assert state.state == STATE_UNLOCKED
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: POORLY_INSTALLED}, blocking=False
)
await asyncio.sleep(1)
state = hass.states.get(POORLY_INSTALLED)
assert state.state == STATE_LOCKING
await asyncio.sleep(2)
state = hass.states.get(POORLY_INSTALLED)
assert state.state == STATE_JAMMED
async def test_opening_mocked(hass):
"""Test the opening of a lock."""
calls = async_mock_service(hass, LOCK_DOMAIN, SERVICE_OPEN)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: OPENABLE_LOCK}, blocking=True
)
assert len(calls) == 1
async def test_opening(hass):
"""Test the opening of a lock."""
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: OPENABLE_LOCK}, blocking=True
)
state = hass.states.get(OPENABLE_LOCK)
assert state.state == STATE_UNLOCKED