2019-04-03 15:40:03 +00:00
|
|
|
"""Support for MQTT locks."""
|
2022-01-03 07:53:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-11-07 07:24:49 +00:00
|
|
|
from collections.abc import Callable
|
2021-01-09 13:37:33 +00:00
|
|
|
import functools
|
2023-01-23 13:48:07 +00:00
|
|
|
import re
|
2022-06-15 13:23:36 +00:00
|
|
|
from typing import Any
|
2016-02-28 17:25:28 +00:00
|
|
|
|
2016-04-06 03:34:04 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-12-10 20:30:07 +00:00
|
|
|
from homeassistant.components import lock
|
2022-04-07 07:44:26 +00:00
|
|
|
from homeassistant.components.lock import LockEntity, LockEntityFeature
|
2022-01-03 07:53:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-01-23 15:08:25 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_CODE,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_OPTIMISTIC,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
)
|
2021-04-22 21:53:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2016-04-06 03:34:04 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 07:53:52 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-01-23 13:48:07 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, TemplateVarsType
|
2016-02-28 17:25:28 +00:00
|
|
|
|
2022-05-31 07:32:44 +00:00
|
|
|
from . import subscription
|
|
|
|
from .config import MQTT_RW_SCHEMA
|
2022-01-03 08:03:47 +00:00
|
|
|
from .const import (
|
2023-01-23 13:48:07 +00:00
|
|
|
CONF_COMMAND_TEMPLATE,
|
2022-01-03 08:03:47 +00:00
|
|
|
CONF_COMMAND_TOPIC,
|
|
|
|
CONF_ENCODING,
|
|
|
|
CONF_QOS,
|
|
|
|
CONF_RETAIN,
|
|
|
|
CONF_STATE_TOPIC,
|
|
|
|
)
|
2020-04-01 18:48:32 +00:00
|
|
|
from .debug_info import log_messages
|
2022-02-10 20:09:57 +00:00
|
|
|
from .mixins import (
|
|
|
|
MQTT_ENTITY_COMMON_SCHEMA,
|
|
|
|
MqttEntity,
|
|
|
|
async_setup_entry_helper,
|
2022-05-21 17:07:26 +00:00
|
|
|
warn_for_legacy_schema,
|
2022-02-10 20:09:57 +00:00
|
|
|
)
|
2023-01-23 13:48:07 +00:00
|
|
|
from .models import (
|
|
|
|
MqttCommandTemplate,
|
|
|
|
MqttValueTemplate,
|
|
|
|
PublishPayloadType,
|
|
|
|
ReceiveMessage,
|
|
|
|
ReceivePayloadType,
|
|
|
|
)
|
2022-10-11 08:49:54 +00:00
|
|
|
from .util import get_mqtt_data
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2023-01-23 13:48:07 +00:00
|
|
|
CONF_CODE_FORMAT = "code_format"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PAYLOAD_LOCK = "payload_lock"
|
|
|
|
CONF_PAYLOAD_UNLOCK = "payload_unlock"
|
2021-11-25 14:54:08 +00:00
|
|
|
CONF_PAYLOAD_OPEN = "payload_open"
|
2016-04-06 03:34:04 +00:00
|
|
|
|
2019-12-31 14:24:09 +00:00
|
|
|
CONF_STATE_LOCKED = "state_locked"
|
2023-01-16 07:42:43 +00:00
|
|
|
CONF_STATE_LOCKING = "state_locking"
|
2019-12-31 14:24:09 +00:00
|
|
|
CONF_STATE_UNLOCKED = "state_unlocked"
|
2023-01-16 07:42:43 +00:00
|
|
|
CONF_STATE_UNLOCKING = "state_unlocking"
|
2023-01-20 11:19:26 +00:00
|
|
|
CONF_STATE_JAMMED = "state_jammed"
|
2019-12-31 14:24:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "MQTT Lock"
|
|
|
|
DEFAULT_PAYLOAD_LOCK = "LOCK"
|
|
|
|
DEFAULT_PAYLOAD_UNLOCK = "UNLOCK"
|
2021-11-25 14:54:08 +00:00
|
|
|
DEFAULT_PAYLOAD_OPEN = "OPEN"
|
2019-12-31 14:24:09 +00:00
|
|
|
DEFAULT_STATE_LOCKED = "LOCKED"
|
2023-01-16 07:42:43 +00:00
|
|
|
DEFAULT_STATE_LOCKING = "LOCKING"
|
2019-12-31 14:24:09 +00:00
|
|
|
DEFAULT_STATE_UNLOCKED = "UNLOCKED"
|
2023-01-16 07:42:43 +00:00
|
|
|
DEFAULT_STATE_UNLOCKING = "UNLOCKING"
|
2023-01-20 11:19:26 +00:00
|
|
|
DEFAULT_STATE_JAMMED = "JAMMED"
|
2019-12-31 14:24:09 +00:00
|
|
|
|
2021-06-29 09:16:07 +00:00
|
|
|
MQTT_LOCK_ATTRIBUTES_BLOCKED = frozenset(
|
|
|
|
{
|
|
|
|
lock.ATTR_CHANGED_BY,
|
|
|
|
lock.ATTR_CODE_FORMAT,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-05-31 07:32:44 +00:00
|
|
|
PLATFORM_SCHEMA_MODERN = MQTT_RW_SCHEMA.extend(
|
2021-03-11 12:42:13 +00:00
|
|
|
{
|
2023-01-23 13:48:07 +00:00
|
|
|
vol.Optional(CONF_CODE_FORMAT): cv.is_regex,
|
|
|
|
vol.Optional(CONF_COMMAND_TEMPLATE): cv.template,
|
2021-03-11 12:42:13 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_LOCK, default=DEFAULT_PAYLOAD_LOCK): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_UNLOCK, default=DEFAULT_PAYLOAD_UNLOCK): cv.string,
|
2021-11-25 14:54:08 +00:00
|
|
|
vol.Optional(CONF_PAYLOAD_OPEN): cv.string,
|
2023-01-20 11:19:26 +00:00
|
|
|
vol.Optional(CONF_STATE_JAMMED, default=DEFAULT_STATE_JAMMED): cv.string,
|
2021-03-11 12:42:13 +00:00
|
|
|
vol.Optional(CONF_STATE_LOCKED, default=DEFAULT_STATE_LOCKED): cv.string,
|
2023-01-16 07:42:43 +00:00
|
|
|
vol.Optional(CONF_STATE_LOCKING, default=DEFAULT_STATE_LOCKING): cv.string,
|
2021-03-11 12:42:13 +00:00
|
|
|
vol.Optional(CONF_STATE_UNLOCKED, default=DEFAULT_STATE_UNLOCKED): cv.string,
|
2023-01-16 07:42:43 +00:00
|
|
|
vol.Optional(CONF_STATE_UNLOCKING, default=DEFAULT_STATE_UNLOCKING): cv.string,
|
2021-06-08 06:23:35 +00:00
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
2021-03-11 12:42:13 +00:00
|
|
|
}
|
|
|
|
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2022-11-25 15:11:22 +00:00
|
|
|
# Configuring MQTT Locks under the lock platform key was deprecated in HA Core 2022.6
|
2022-11-17 21:55:31 +00:00
|
|
|
# Setup for the legacy YAML format was removed in HA Core 2022.12
|
2022-05-21 17:07:26 +00:00
|
|
|
PLATFORM_SCHEMA = vol.All(
|
|
|
|
warn_for_legacy_schema(lock.DOMAIN),
|
|
|
|
)
|
|
|
|
|
|
|
|
DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)
|
2021-10-25 11:47:06 +00:00
|
|
|
|
2023-01-16 07:42:43 +00:00
|
|
|
STATE_CONFIG_KEYS = [
|
2023-01-20 11:19:26 +00:00
|
|
|
CONF_STATE_JAMMED,
|
2023-01-16 07:42:43 +00:00
|
|
|
CONF_STATE_LOCKED,
|
|
|
|
CONF_STATE_LOCKING,
|
|
|
|
CONF_STATE_UNLOCKED,
|
|
|
|
CONF_STATE_UNLOCKING,
|
|
|
|
]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2022-01-03 07:53:52 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2023-01-08 21:07:10 +00:00
|
|
|
"""Set up MQTT lock through YAML and through MQTT discovery."""
|
2021-01-09 13:37:33 +00:00
|
|
|
setup = functools.partial(
|
|
|
|
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-10-25 11:47:06 +00:00
|
|
|
await async_setup_entry_helper(hass, lock.DOMAIN, setup, DISCOVERY_SCHEMA)
|
2018-11-06 15:09:46 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def _async_setup_entity(
|
2022-07-12 09:07:18 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
config: ConfigType,
|
2022-11-07 07:24:49 +00:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
discovery_data: DiscoveryInfoType | None = None,
|
2022-07-12 09:07:18 +00:00
|
|
|
) -> None:
|
2018-11-06 15:09:46 +00:00
|
|
|
"""Set up the MQTT Lock platform."""
|
2020-09-10 18:52:23 +00:00
|
|
|
async_add_entities([MqttLock(hass, config, config_entry, discovery_data)])
|
2016-02-28 17:25:28 +00:00
|
|
|
|
|
|
|
|
2021-01-09 16:46:53 +00:00
|
|
|
class MqttLock(MqttEntity, LockEntity):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Representation of a lock that can be toggled using MQTT."""
|
2016-03-07 21:13:18 +00:00
|
|
|
|
2021-11-08 13:02:18 +00:00
|
|
|
_entity_id_format = lock.ENTITY_ID_FORMAT
|
2021-06-29 09:16:07 +00:00
|
|
|
_attributes_extra_blocked = MQTT_LOCK_ATTRIBUTES_BLOCKED
|
|
|
|
|
2023-01-23 13:48:07 +00:00
|
|
|
_compiled_pattern: re.Pattern[Any] | None
|
2022-11-07 07:24:49 +00:00
|
|
|
_optimistic: bool
|
2023-01-16 07:42:43 +00:00
|
|
|
_valid_states: list[str]
|
2023-01-23 13:48:07 +00:00
|
|
|
_command_template: Callable[
|
|
|
|
[PublishPayloadType, TemplateVarsType], PublishPayloadType
|
|
|
|
]
|
2022-11-07 07:24:49 +00:00
|
|
|
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
|
2019-01-14 20:01:42 +00:00
|
|
|
|
2022-11-07 07:24:49 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
discovery_data: DiscoveryInfoType | None,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the lock."""
|
|
|
|
self._attr_is_locked = False
|
2021-01-09 16:46:53 +00:00
|
|
|
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2021-01-09 16:46:53 +00:00
|
|
|
@staticmethod
|
2022-11-07 07:24:49 +00:00
|
|
|
def config_schema() -> vol.Schema:
|
2021-01-09 16:46:53 +00:00
|
|
|
"""Return the config schema."""
|
2021-10-25 11:47:06 +00:00
|
|
|
return DISCOVERY_SCHEMA
|
2019-01-14 20:01:42 +00:00
|
|
|
|
2022-11-07 07:24:49 +00:00
|
|
|
def _setup_from_config(self, config: ConfigType) -> None:
|
2019-04-10 20:56:34 +00:00
|
|
|
"""(Re)Setup the entity."""
|
2023-01-23 13:48:07 +00:00
|
|
|
self._optimistic = (
|
|
|
|
config[CONF_OPTIMISTIC] or self._config.get(CONF_STATE_TOPIC) is None
|
|
|
|
)
|
|
|
|
|
|
|
|
self._compiled_pattern = config.get(CONF_CODE_FORMAT)
|
|
|
|
self._attr_code_format = (
|
|
|
|
self._compiled_pattern.pattern if self._compiled_pattern else None
|
|
|
|
)
|
|
|
|
|
|
|
|
self._command_template = MqttCommandTemplate(
|
|
|
|
config.get(CONF_COMMAND_TEMPLATE), entity=self
|
|
|
|
).async_render
|
2019-04-10 20:56:34 +00:00
|
|
|
|
2022-01-03 15:07:40 +00:00
|
|
|
self._value_template = MqttValueTemplate(
|
2022-11-07 07:24:49 +00:00
|
|
|
config.get(CONF_VALUE_TEMPLATE),
|
2022-01-03 15:07:40 +00:00
|
|
|
entity=self,
|
|
|
|
).async_render_with_possible_json_value
|
2016-02-28 17:25:28 +00:00
|
|
|
|
2022-11-22 06:15:11 +00:00
|
|
|
self._attr_supported_features = LockEntityFeature(0)
|
|
|
|
if CONF_PAYLOAD_OPEN in config:
|
|
|
|
self._attr_supported_features |= LockEntityFeature.OPEN
|
2022-11-07 07:24:49 +00:00
|
|
|
|
2023-01-16 07:42:43 +00:00
|
|
|
self._valid_states = [config[state] for state in STATE_CONFIG_KEYS]
|
|
|
|
|
2022-11-07 07:24:49 +00:00
|
|
|
def _prepare_subscribe_topics(self) -> None:
|
2020-09-10 18:52:23 +00:00
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
|
2023-01-20 11:19:26 +00:00
|
|
|
topics: dict[str, dict[str, Any]] = {}
|
|
|
|
qos: int = self._config[CONF_QOS]
|
|
|
|
encoding: str | None = self._config[CONF_ENCODING] or None
|
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2020-04-01 18:48:32 +00:00
|
|
|
@log_messages(self.hass, self.entity_id)
|
2022-11-07 07:24:49 +00:00
|
|
|
def message_received(msg: ReceiveMessage) -> None:
|
2023-01-20 11:19:26 +00:00
|
|
|
"""Handle new lock state messages."""
|
2022-01-03 15:07:40 +00:00
|
|
|
payload = self._value_template(msg.payload)
|
2023-01-16 07:42:43 +00:00
|
|
|
if payload in self._valid_states:
|
|
|
|
self._attr_is_locked = payload == self._config[CONF_STATE_LOCKED]
|
|
|
|
self._attr_is_locking = payload == self._config[CONF_STATE_LOCKING]
|
|
|
|
self._attr_is_unlocking = payload == self._config[CONF_STATE_UNLOCKING]
|
2023-01-20 11:19:26 +00:00
|
|
|
self._attr_is_jammed = payload == self._config[CONF_STATE_JAMMED]
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2022-10-11 08:49:54 +00:00
|
|
|
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
|
2016-02-28 17:25:28 +00:00
|
|
|
|
2019-01-14 20:01:42 +00:00
|
|
|
if self._config.get(CONF_STATE_TOPIC) is None:
|
2016-02-28 17:25:28 +00:00
|
|
|
# Force into optimistic mode.
|
|
|
|
self._optimistic = True
|
|
|
|
else:
|
2023-01-20 11:19:26 +00:00
|
|
|
topics[CONF_STATE_TOPIC] = {
|
|
|
|
"topic": self._config.get(CONF_STATE_TOPIC),
|
|
|
|
"msg_callback": message_received,
|
|
|
|
CONF_QOS: qos,
|
|
|
|
CONF_ENCODING: encoding,
|
|
|
|
}
|
|
|
|
|
|
|
|
self._sub_state = subscription.async_prepare_subscribe_topics(
|
|
|
|
self.hass,
|
|
|
|
self._sub_state,
|
|
|
|
topics,
|
|
|
|
)
|
2019-01-14 20:01:42 +00:00
|
|
|
|
2022-11-07 07:24:49 +00:00
|
|
|
async def _subscribe_topics(self) -> None:
|
2022-02-03 01:12:22 +00:00
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
|
|
|
|
2016-02-28 17:25:28 +00:00
|
|
|
@property
|
2022-06-23 14:34:40 +00:00
|
|
|
def assumed_state(self) -> bool:
|
2016-02-28 17:25:28 +00:00
|
|
|
"""Return true if we do optimistic updates."""
|
|
|
|
return self._optimistic
|
|
|
|
|
2022-06-15 13:23:36 +00:00
|
|
|
async def async_lock(self, **kwargs: Any) -> None:
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Lock the device.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2023-01-23 15:08:25 +00:00
|
|
|
tpl_vars: TemplateVarsType = {
|
|
|
|
ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None
|
|
|
|
}
|
|
|
|
payload = self._command_template(self._config[CONF_PAYLOAD_LOCK], tpl_vars)
|
2022-02-04 16:35:32 +00:00
|
|
|
await self.async_publish(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._config[CONF_COMMAND_TOPIC],
|
2023-01-23 13:48:07 +00:00
|
|
|
payload,
|
2019-01-14 20:01:42 +00:00
|
|
|
self._config[CONF_QOS],
|
2019-07-31 19:25:30 +00:00
|
|
|
self._config[CONF_RETAIN],
|
2022-01-03 08:03:47 +00:00
|
|
|
self._config[CONF_ENCODING],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-02-28 17:25:28 +00:00
|
|
|
if self._optimistic:
|
2019-03-12 21:46:48 +00:00
|
|
|
# Optimistically assume that the lock has changed state.
|
2022-11-07 07:24:49 +00:00
|
|
|
self._attr_is_locked = True
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2022-06-15 13:23:36 +00:00
|
|
|
async def async_unlock(self, **kwargs: Any) -> None:
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Unlock the device.
|
2016-02-28 17:25:28 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2023-01-23 15:08:25 +00:00
|
|
|
tpl_vars: TemplateVarsType = {
|
|
|
|
ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None
|
|
|
|
}
|
|
|
|
payload = self._command_template(self._config[CONF_PAYLOAD_UNLOCK], tpl_vars)
|
2022-02-04 16:35:32 +00:00
|
|
|
await self.async_publish(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._config[CONF_COMMAND_TOPIC],
|
2023-01-23 13:48:07 +00:00
|
|
|
payload,
|
2019-01-14 20:01:42 +00:00
|
|
|
self._config[CONF_QOS],
|
2019-07-31 19:25:30 +00:00
|
|
|
self._config[CONF_RETAIN],
|
2022-01-03 08:03:47 +00:00
|
|
|
self._config[CONF_ENCODING],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-02-28 17:25:28 +00:00
|
|
|
if self._optimistic:
|
2019-03-12 21:46:48 +00:00
|
|
|
# Optimistically assume that the lock has changed state.
|
2022-11-07 07:24:49 +00:00
|
|
|
self._attr_is_locked = False
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2021-11-25 14:54:08 +00:00
|
|
|
|
2022-06-15 13:23:36 +00:00
|
|
|
async def async_open(self, **kwargs: Any) -> None:
|
2021-11-25 14:54:08 +00:00
|
|
|
"""Open the door latch.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2023-01-23 15:08:25 +00:00
|
|
|
tpl_vars: TemplateVarsType = {
|
|
|
|
ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None
|
|
|
|
}
|
|
|
|
payload = self._command_template(self._config[CONF_PAYLOAD_OPEN], tpl_vars)
|
2022-02-04 16:35:32 +00:00
|
|
|
await self.async_publish(
|
2021-11-25 14:54:08 +00:00
|
|
|
self._config[CONF_COMMAND_TOPIC],
|
2023-01-23 13:48:07 +00:00
|
|
|
payload,
|
2021-11-25 14:54:08 +00:00
|
|
|
self._config[CONF_QOS],
|
|
|
|
self._config[CONF_RETAIN],
|
2022-01-03 08:03:47 +00:00
|
|
|
self._config[CONF_ENCODING],
|
2021-11-25 14:54:08 +00:00
|
|
|
)
|
|
|
|
if self._optimistic:
|
|
|
|
# Optimistically assume that the lock unlocks when opened.
|
2022-11-07 07:24:49 +00:00
|
|
|
self._attr_is_locked = False
|
2021-11-25 14:54:08 +00:00
|
|
|
self.async_write_ha_state()
|