mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Update template lock to support locking, unlocking, jammed (#52817)
This commit is contained in:
parent
5d85983b09
commit
ee242764a1
@ -1,7 +1,13 @@
|
||||
"""Support for locks which integrates with other components."""
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.lock import PLATFORM_SCHEMA, LockEntity
|
||||
from homeassistant.components.lock import (
|
||||
PLATFORM_SCHEMA,
|
||||
STATE_JAMMED,
|
||||
STATE_LOCKING,
|
||||
STATE_UNLOCKING,
|
||||
LockEntity,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_NAME,
|
||||
CONF_OPTIMISTIC,
|
||||
@ -9,6 +15,8 @@ from homeassistant.const import (
|
||||
CONF_VALUE_TEMPLATE,
|
||||
STATE_LOCKED,
|
||||
STATE_ON,
|
||||
STATE_UNKNOWN,
|
||||
STATE_UNLOCKED,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import TemplateError
|
||||
@ -105,7 +113,22 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
@property
|
||||
def is_locked(self):
|
||||
"""Return true if lock is locked."""
|
||||
return self._state
|
||||
return self._state in ("true", STATE_ON, STATE_LOCKED)
|
||||
|
||||
@property
|
||||
def is_jammed(self):
|
||||
"""Return true if lock is jammed."""
|
||||
return self._state == STATE_JAMMED
|
||||
|
||||
@property
|
||||
def is_unlocking(self):
|
||||
"""Return true if lock is unlocking."""
|
||||
return self._state == STATE_UNLOCKING
|
||||
|
||||
@property
|
||||
def is_locking(self):
|
||||
"""Return true if lock is locking."""
|
||||
return self._state == STATE_LOCKING
|
||||
|
||||
@callback
|
||||
def _update_state(self, result):
|
||||
@ -115,14 +138,14 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
return
|
||||
|
||||
if isinstance(result, bool):
|
||||
self._state = result
|
||||
self._state = STATE_LOCKED if result else STATE_UNLOCKED
|
||||
return
|
||||
|
||||
if isinstance(result, str):
|
||||
self._state = result.lower() in ("true", STATE_ON, STATE_LOCKED)
|
||||
self._state = result.lower()
|
||||
return
|
||||
|
||||
self._state = False
|
||||
self._state = STATE_UNKNOWN
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register callbacks."""
|
||||
|
@ -325,6 +325,84 @@ async def test_unlock_action(hass, calls):
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_unlocking(hass, calls):
|
||||
"""Test unlocking."""
|
||||
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_UNLOCKING)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("lock.template_lock")
|
||||
assert state.state == lock.STATE_UNLOCKING
|
||||
|
||||
|
||||
async def test_locking(hass, calls):
|
||||
"""Test unlocking."""
|
||||
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_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."""
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user