mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Add open to Template lock (#129292)
* Add open to Template lock * Update from review
This commit is contained in:
parent
284fe17b1c
commit
3464ffc53e
@ -2,13 +2,14 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.lock import (
|
||||
PLATFORM_SCHEMA as LOCK_PLATFORM_SCHEMA,
|
||||
LockEntity,
|
||||
LockEntityFeature,
|
||||
LockState,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
@ -36,6 +37,7 @@ from .template_entity import (
|
||||
CONF_CODE_FORMAT_TEMPLATE = "code_format_template"
|
||||
CONF_LOCK = "lock"
|
||||
CONF_UNLOCK = "unlock"
|
||||
CONF_OPEN = "open"
|
||||
|
||||
DEFAULT_NAME = "Template Lock"
|
||||
DEFAULT_OPTIMISTIC = False
|
||||
@ -45,6 +47,7 @@ PLATFORM_SCHEMA = LOCK_PLATFORM_SCHEMA.extend(
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_LOCK): cv.SCRIPT_SCHEMA,
|
||||
vol.Required(CONF_UNLOCK): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_OPEN): cv.SCRIPT_SCHEMA,
|
||||
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_CODE_FORMAT_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
||||
@ -53,7 +56,9 @@ PLATFORM_SCHEMA = LOCK_PLATFORM_SCHEMA.extend(
|
||||
).extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY.schema)
|
||||
|
||||
|
||||
async def _async_create_entities(hass, config):
|
||||
async def _async_create_entities(
|
||||
hass: HomeAssistant, config: dict[str, Any]
|
||||
) -> list[TemplateLock]:
|
||||
"""Create the Template lock."""
|
||||
config = rewrite_common_legacy_to_modern_conf(hass, config)
|
||||
return [TemplateLock(hass, config, config.get(CONF_UNIQUE_ID))]
|
||||
@ -76,22 +81,26 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass,
|
||||
config,
|
||||
unique_id,
|
||||
):
|
||||
hass: HomeAssistant,
|
||||
config: dict[str, Any],
|
||||
unique_id: str | None,
|
||||
) -> None:
|
||||
"""Initialize the lock."""
|
||||
super().__init__(
|
||||
hass, config=config, fallback_name=DEFAULT_NAME, unique_id=unique_id
|
||||
)
|
||||
self._state = None
|
||||
self._state: str | bool | LockState | None = None
|
||||
name = self._attr_name
|
||||
assert name
|
||||
self._state_template = config.get(CONF_VALUE_TEMPLATE)
|
||||
self._command_lock = Script(hass, config[CONF_LOCK], name, DOMAIN)
|
||||
self._command_unlock = Script(hass, config[CONF_UNLOCK], name, DOMAIN)
|
||||
if CONF_OPEN in config:
|
||||
self._command_open = Script(hass, config[CONF_OPEN], name, DOMAIN)
|
||||
self._attr_supported_features |= LockEntityFeature.OPEN
|
||||
self._code_format_template = config.get(CONF_CODE_FORMAT_TEMPLATE)
|
||||
self._code_format = None
|
||||
self._code_format_template_error = None
|
||||
self._code_format: str | None = None
|
||||
self._code_format_template_error: TemplateError | None = None
|
||||
self._optimistic = config.get(CONF_OPTIMISTIC)
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
@ -115,6 +124,11 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
"""Return true if lock is locking."""
|
||||
return self._state == LockState.LOCKING
|
||||
|
||||
@property
|
||||
def is_open(self) -> bool:
|
||||
"""Return true if lock is open."""
|
||||
return self._state == LockState.OPEN
|
||||
|
||||
@callback
|
||||
def _update_state(self, result):
|
||||
"""Update the state from the template."""
|
||||
@ -141,6 +155,8 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
@callback
|
||||
def _async_setup_templates(self) -> None:
|
||||
"""Set up templates."""
|
||||
if TYPE_CHECKING:
|
||||
assert self._state_template is not None
|
||||
self.add_template_attribute(
|
||||
"_state", self._state_template, None, self._update_state
|
||||
)
|
||||
@ -168,6 +184,8 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
|
||||
async def async_lock(self, **kwargs: Any) -> None:
|
||||
"""Lock the device."""
|
||||
# Check if we need to raise for incorrect code format
|
||||
# template before processing the action.
|
||||
self._raise_template_error_if_available()
|
||||
|
||||
if self._optimistic:
|
||||
@ -182,6 +200,8 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
|
||||
async def async_unlock(self, **kwargs: Any) -> None:
|
||||
"""Unlock the device."""
|
||||
# Check if we need to raise for incorrect code format
|
||||
# template before processing the action.
|
||||
self._raise_template_error_if_available()
|
||||
|
||||
if self._optimistic:
|
||||
@ -194,7 +214,24 @@ class TemplateLock(TemplateEntity, LockEntity):
|
||||
self._command_unlock, run_variables=tpl_vars, context=self._context
|
||||
)
|
||||
|
||||
async def async_open(self, **kwargs: Any) -> None:
|
||||
"""Open the device."""
|
||||
# Check if we need to raise for incorrect code format
|
||||
# template before processing the action.
|
||||
self._raise_template_error_if_available()
|
||||
|
||||
if self._optimistic:
|
||||
self._state = LockState.OPEN
|
||||
self.async_write_ha_state()
|
||||
|
||||
tpl_vars = {ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None}
|
||||
|
||||
await self.async_run_script(
|
||||
self._command_open, run_variables=tpl_vars, context=self._context
|
||||
)
|
||||
|
||||
def _raise_template_error_if_available(self):
|
||||
"""Raise an error if the rendered code format is not valid."""
|
||||
if self._code_format_template_error is not None:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
|
@ -10,6 +10,7 @@ from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_OPEN,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
@ -30,6 +31,13 @@ OPTIMISTIC_LOCK_CONFIG = {
|
||||
"caller": "{{ this.entity_id }}",
|
||||
},
|
||||
},
|
||||
"open": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"action": "open",
|
||||
"caller": "{{ this.entity_id }}",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
OPTIMISTIC_CODED_LOCK_CONFIG = {
|
||||
@ -81,6 +89,53 @@ async def test_template_state(hass: HomeAssistant) -> None:
|
||||
state = hass.states.get("lock.test_template_lock")
|
||||
assert state.state == LockState.UNLOCKED
|
||||
|
||||
hass.states.async_set("switch.test_state", STATE_OPEN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("lock.test_template_lock")
|
||||
assert state.state == LockState.OPEN
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"name": "Test lock",
|
||||
"optimistic": True,
|
||||
"value_template": "{{ states.switch.test_state.state }}",
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("start_ha")
|
||||
async def test_open_lock_optimistic(
|
||||
hass: HomeAssistant, calls: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test optimistic open."""
|
||||
await setup.async_setup_component(hass, "switch", {})
|
||||
hass.states.async_set("switch.test_state", STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("lock.test_lock")
|
||||
assert state.state == LockState.LOCKED
|
||||
|
||||
await hass.services.async_call(
|
||||
lock.DOMAIN,
|
||||
lock.SERVICE_OPEN,
|
||||
{ATTR_ENTITY_ID: "lock.test_lock"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["action"] == "open"
|
||||
assert calls[0].data["caller"] == "lock.test_lock"
|
||||
|
||||
state = hass.states.get("lock.test_lock")
|
||||
assert state.state == LockState.OPEN
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
@ -282,6 +337,40 @@ async def test_unlock_action(hass: HomeAssistant, calls: list[ServiceCall]) -> N
|
||||
assert calls[0].data["caller"] == "lock.template_lock"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ states.switch.test_state.state }}",
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("start_ha")
|
||||
async def test_open_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None:
|
||||
"""Test open action."""
|
||||
await setup.async_setup_component(hass, "switch", {})
|
||||
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 == LockState.LOCKED
|
||||
|
||||
await hass.services.async_call(
|
||||
lock.DOMAIN,
|
||||
lock.SERVICE_OPEN,
|
||||
{ATTR_ENTITY_ID: "lock.template_lock"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["action"] == "open"
|
||||
assert calls[0].data["caller"] == "lock.template_lock"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)])
|
||||
@pytest.mark.parametrize(
|
||||
"config",
|
||||
|
Loading…
x
Reference in New Issue
Block a user