1 """Support for locks which integrates with other components."""
3 from __future__
import annotations
5 from typing
import TYPE_CHECKING, Any
7 import voluptuous
as vol
10 PLATFORM_SCHEMA
as LOCK_PLATFORM_SCHEMA,
30 from .const
import DOMAIN
31 from .template_entity
import (
32 TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY,
34 rewrite_common_legacy_to_modern_conf,
37 CONF_CODE_FORMAT_TEMPLATE =
"code_format_template"
39 CONF_UNLOCK =
"unlock"
42 DEFAULT_NAME =
"Template Lock"
43 DEFAULT_OPTIMISTIC =
False
45 PLATFORM_SCHEMA = LOCK_PLATFORM_SCHEMA.extend(
47 vol.Optional(CONF_NAME): cv.string,
48 vol.Required(CONF_LOCK): cv.SCRIPT_SCHEMA,
49 vol.Required(CONF_UNLOCK): cv.SCRIPT_SCHEMA,
50 vol.Optional(CONF_OPEN): cv.SCRIPT_SCHEMA,
51 vol.Required(CONF_VALUE_TEMPLATE): cv.template,
52 vol.Optional(CONF_CODE_FORMAT_TEMPLATE): cv.template,
53 vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
54 vol.Optional(CONF_UNIQUE_ID): cv.string,
56 ).extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY.schema)
60 hass: HomeAssistant, config: dict[str, Any]
61 ) -> list[TemplateLock]:
62 """Create the Template lock."""
64 return [
TemplateLock(hass, config, config.get(CONF_UNIQUE_ID))]
70 async_add_entities: AddEntitiesCallback,
71 discovery_info: DiscoveryInfoType |
None =
None,
73 """Set up the template lock."""
78 """Representation of a template lock."""
80 _attr_should_poll =
False
85 config: dict[str, Any],
86 unique_id: str |
None,
88 """Initialize the lock."""
90 hass, config=config, fallback_name=DEFAULT_NAME, unique_id=unique_id
92 self.
_state_state: str | bool | LockState |
None =
None
98 if CONF_OPEN
in config:
100 self._attr_supported_features |= LockEntityFeature.OPEN
109 """Return true if lock is locked."""
110 return self.
_state_state
in (
"true", STATE_ON, LockState.LOCKED)
114 """Return true if lock is jammed."""
115 return self.
_state_state == LockState.JAMMED
119 """Return true if lock is unlocking."""
120 return self.
_state_state == LockState.UNLOCKING
124 """Return true if lock is locking."""
125 return self.
_state_state == LockState.LOCKING
129 """Return true if lock is open."""
130 return self.
_state_state == LockState.OPEN
134 """Update the state from the template."""
136 if isinstance(result, TemplateError):
140 if isinstance(result, bool):
141 self.
_state_state = LockState.LOCKED
if result
else LockState.UNLOCKED
144 if isinstance(result, str):
145 self.
_state_state = result.lower()
152 """Regex for code format or None if no code is required."""
157 """Set up templates."""
165 "_code_format_template",
174 """Update code format from the template."""
175 if isinstance(render, TemplateError):
178 elif render
in (
None,
"None",
""):
186 """Lock the device."""
195 tpl_vars = {ATTR_CODE: kwargs.get(ATTR_CODE)
if kwargs
else None}
202 """Unlock the device."""
211 tpl_vars = {ATTR_CODE: kwargs.get(ATTR_CODE)
if kwargs
else None}
218 """Open the device."""
224 self.
_state_state = LockState.OPEN
227 tpl_vars = {ATTR_CODE: kwargs.get(ATTR_CODE)
if kwargs
else None}
234 """Raise an error if the rendered code format is not valid."""
237 translation_domain=DOMAIN,
238 translation_key=
"code_format_template_error",
239 translation_placeholders={
None __init__(self, HomeAssistant hass, dict[str, Any] config, str|None unique_id)
def _raise_template_error_if_available(self)
def _update_code_format(self, str|TemplateError|None render)
_code_format_template_error
None async_unlock(self, **Any kwargs)
None _async_setup_templates(self)
None async_open(self, **Any kwargs)
def _update_state(self, result)
str|None code_format(self)
None async_lock(self, **Any kwargs)
None _update_state(self, str|TemplateError result)
None async_run_script(self, Script script, *_VarsType|None run_variables=None, Context|None context=None)
None add_template_attribute(self, str attribute, Template template, Callable[[Any], Any]|None validator=None, Callable[[Any], None]|None on_update=None, bool none_on_template_error=False)
None async_write_ha_state(self)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
list[TemplateLock] _async_create_entities(HomeAssistant hass, dict[str, Any] config)
dict[str, Any] rewrite_common_legacy_to_modern_conf(HomeAssistant hass, dict[str, Any] entity_cfg, dict[str, str]|None extra_legacy_fields=None)