2019-04-03 15:40:03 +00:00
|
|
|
"""Support for locks which integrates with other components."""
|
2018-10-11 10:53:54 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-08 20:05:08 +00:00
|
|
|
from homeassistant.components.lock import PLATFORM_SCHEMA, LockDevice
|
2018-10-11 10:53:54 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_OPTIMISTIC,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
MATCH_ALL,
|
2019-12-08 20:05:08 +00:00
|
|
|
STATE_LOCKED,
|
|
|
|
STATE_ON,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-08 20:05:08 +00:00
|
|
|
from homeassistant.core import callback
|
2018-10-11 10:53:54 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2019-12-08 20:05:08 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-10-11 10:53:54 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
|
|
|
from homeassistant.helpers.script import Script
|
2019-12-08 20:05:08 +00:00
|
|
|
|
2019-11-26 00:30:49 +00:00
|
|
|
from . import extract_entities, initialise_templates
|
2019-10-01 12:15:15 +00:00
|
|
|
from .const import CONF_AVAILABILITY_TEMPLATE
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LOCK = "lock"
|
|
|
|
CONF_UNLOCK = "unlock"
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Template Lock"
|
2018-10-11 10:53:54 +00:00
|
|
|
DEFAULT_OPTIMISTIC = False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_LOCK): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Required(CONF_UNLOCK): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
2019-10-01 12:15:15 +00:00
|
|
|
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
2018-10-11 10:53:54 +00:00
|
|
|
"""Set up the Template lock."""
|
2019-11-26 00:30:49 +00:00
|
|
|
device = config.get(CONF_NAME)
|
2018-10-11 10:53:54 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
2019-11-26 00:30:49 +00:00
|
|
|
availability_template = config.get(CONF_AVAILABILITY_TEMPLATE)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-11-26 00:30:49 +00:00
|
|
|
templates = {
|
|
|
|
CONF_VALUE_TEMPLATE: value_template,
|
|
|
|
CONF_AVAILABILITY_TEMPLATE: availability_template,
|
|
|
|
}
|
2019-10-01 12:15:15 +00:00
|
|
|
|
2019-11-26 00:30:49 +00:00
|
|
|
initialise_templates(hass, templates)
|
|
|
|
entity_ids = extract_entities(device, "lock", None, templates)
|
2019-10-01 12:15:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_devices(
|
|
|
|
[
|
|
|
|
TemplateLock(
|
|
|
|
hass,
|
2019-11-26 00:30:49 +00:00
|
|
|
device,
|
2019-07-31 19:25:30 +00:00
|
|
|
value_template,
|
2019-10-01 12:15:15 +00:00
|
|
|
availability_template,
|
2019-11-26 00:30:49 +00:00
|
|
|
entity_ids,
|
2019-07-31 19:25:30 +00:00
|
|
|
config.get(CONF_LOCK),
|
|
|
|
config.get(CONF_UNLOCK),
|
|
|
|
config.get(CONF_OPTIMISTIC),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateLock(LockDevice):
|
|
|
|
"""Representation of a template lock."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass,
|
|
|
|
name,
|
|
|
|
value_template,
|
2019-10-01 12:15:15 +00:00
|
|
|
availability_template,
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_ids,
|
|
|
|
command_lock,
|
|
|
|
command_unlock,
|
|
|
|
optimistic,
|
|
|
|
):
|
2018-10-11 10:53:54 +00:00
|
|
|
"""Initialize the lock."""
|
|
|
|
self._state = None
|
|
|
|
self._hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._state_template = value_template
|
2019-10-01 12:15:15 +00:00
|
|
|
self._availability_template = availability_template
|
2018-10-11 10:53:54 +00:00
|
|
|
self._state_entities = entity_ids
|
|
|
|
self._command_lock = Script(hass, command_lock)
|
|
|
|
self._command_unlock = Script(hass, command_unlock)
|
|
|
|
self._optimistic = optimistic
|
2019-10-01 12:15:15 +00:00
|
|
|
self._available = True
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-10-11 10:53:54 +00:00
|
|
|
@callback
|
|
|
|
def template_lock_state_listener(entity, old_state, new_state):
|
|
|
|
"""Handle target device state changes."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def template_lock_startup(event):
|
|
|
|
"""Update template on startup."""
|
|
|
|
if self._state_entities != MATCH_ALL:
|
|
|
|
# Track state change only for valid templates
|
|
|
|
async_track_state_change(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hass, self._state_entities, template_lock_state_listener
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
self._hass.bus.async_listen_once(
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, template_lock_startup
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return true if we do optimistic updates."""
|
|
|
|
return self._optimistic
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the lock."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self):
|
|
|
|
"""Return true if lock is locked."""
|
|
|
|
return self._state
|
|
|
|
|
2019-10-01 12:15:15 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the device is available."""
|
|
|
|
return self._available
|
|
|
|
|
2018-10-11 10:53:54 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Update the state from the template."""
|
|
|
|
try:
|
|
|
|
self._state = self._state_template.async_render().lower() in (
|
2019-07-31 19:25:30 +00:00
|
|
|
"true",
|
|
|
|
STATE_ON,
|
|
|
|
STATE_LOCKED,
|
|
|
|
)
|
2018-10-11 10:53:54 +00:00
|
|
|
except TemplateError as ex:
|
|
|
|
self._state = None
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Could not render template %s: %s", self._name, ex)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
2019-10-01 12:15:15 +00:00
|
|
|
if self._availability_template is not None:
|
|
|
|
try:
|
|
|
|
self._available = (
|
|
|
|
self._availability_template.async_render().lower() == "true"
|
|
|
|
)
|
|
|
|
except (TemplateError, ValueError) as ex:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Could not render %s template %s: %s",
|
|
|
|
CONF_AVAILABILITY_TEMPLATE,
|
|
|
|
self._name,
|
|
|
|
ex,
|
|
|
|
)
|
|
|
|
|
2018-10-11 10:53:54 +00:00
|
|
|
async def async_lock(self, **kwargs):
|
|
|
|
"""Lock the device."""
|
|
|
|
if self._optimistic:
|
|
|
|
self._state = True
|
|
|
|
self.async_schedule_update_ha_state()
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._command_lock.async_run(context=self._context)
|
2018-10-11 10:53:54 +00:00
|
|
|
|
|
|
|
async def async_unlock(self, **kwargs):
|
|
|
|
"""Unlock the device."""
|
|
|
|
if self._optimistic:
|
|
|
|
self._state = False
|
|
|
|
self.async_schedule_update_ha_state()
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._command_unlock.async_run(context=self._context)
|