core/homeassistant/components/mqtt/lock.py

234 lines
7.8 KiB
Python
Raw Normal View History

"""Support for MQTT locks."""
from __future__ import annotations
from collections.abc import Callable
import functools
from typing import Any
2016-02-28 17:25:28 +00:00
import voluptuous as vol
from homeassistant.components import lock
from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2016-02-28 17:25:28 +00:00
from . import subscription
from .config import MQTT_RW_SCHEMA
from .const import (
CONF_COMMAND_TOPIC,
CONF_ENCODING,
CONF_QOS,
CONF_RETAIN,
CONF_STATE_TOPIC,
)
from .debug_info import log_messages
from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA,
MqttEntity,
async_setup_entry_helper,
warn_for_legacy_schema,
)
from .models import MqttValueTemplate, ReceiveMessage, ReceivePayloadType
from .util import get_mqtt_data
2019-07-31 19:25:30 +00:00
CONF_PAYLOAD_LOCK = "payload_lock"
CONF_PAYLOAD_UNLOCK = "payload_unlock"
CONF_PAYLOAD_OPEN = "payload_open"
CONF_STATE_LOCKED = "state_locked"
CONF_STATE_UNLOCKED = "state_unlocked"
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "MQTT Lock"
DEFAULT_OPTIMISTIC = False
2019-07-31 19:25:30 +00:00
DEFAULT_PAYLOAD_LOCK = "LOCK"
DEFAULT_PAYLOAD_UNLOCK = "UNLOCK"
DEFAULT_PAYLOAD_OPEN = "OPEN"
DEFAULT_STATE_LOCKED = "LOCKED"
DEFAULT_STATE_UNLOCKED = "UNLOCKED"
MQTT_LOCK_ATTRIBUTES_BLOCKED = frozenset(
{
lock.ATTR_CHANGED_BY,
lock.ATTR_CODE_FORMAT,
}
)
PLATFORM_SCHEMA_MODERN = MQTT_RW_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_PAYLOAD_LOCK, default=DEFAULT_PAYLOAD_LOCK): cv.string,
vol.Optional(CONF_PAYLOAD_UNLOCK, default=DEFAULT_PAYLOAD_UNLOCK): cv.string,
vol.Optional(CONF_PAYLOAD_OPEN): cv.string,
vol.Optional(CONF_STATE_LOCKED, default=DEFAULT_STATE_LOCKED): cv.string,
vol.Optional(CONF_STATE_UNLOCKED, default=DEFAULT_STATE_UNLOCKED): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
2019-07-31 19:25:30 +00:00
# Configuring MQTT Locks under the lock platform key is deprecated in HA Core 2022.6
# Setup for the legacy YAML format was removed in HA Core 2022.12
PLATFORM_SCHEMA = vol.All(
warn_for_legacy_schema(lock.DOMAIN),
)
DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)
2019-07-31 19:25:30 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT lock through configuration.yaml and dynamically through MQTT discovery."""
setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
2019-07-31 19:25:30 +00:00
)
await async_setup_entry_helper(hass, lock.DOMAIN, setup, DISCOVERY_SCHEMA)
2019-07-31 19:25:30 +00:00
async def _async_setup_entity(
hass: HomeAssistant,
async_add_entities: AddEntitiesCallback,
config: ConfigType,
config_entry: ConfigEntry,
discovery_data: DiscoveryInfoType | None = None,
) -> None:
"""Set up the MQTT Lock platform."""
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):
"""Representation of a lock that can be toggled using MQTT."""
2016-03-07 21:13:18 +00:00
_entity_id_format = lock.ENTITY_ID_FORMAT
_attributes_extra_blocked = MQTT_LOCK_ATTRIBUTES_BLOCKED
_optimistic: bool
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
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)
2021-01-09 16:46:53 +00:00
@staticmethod
def config_schema() -> vol.Schema:
2021-01-09 16:46:53 +00:00
"""Return the config schema."""
return DISCOVERY_SCHEMA
def _setup_from_config(self, config: ConfigType) -> None:
"""(Re)Setup the entity."""
self._optimistic = config[CONF_OPTIMISTIC]
self._value_template = MqttValueTemplate(
config.get(CONF_VALUE_TEMPLATE),
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
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
@callback
@log_messages(self.hass, self.entity_id)
def message_received(msg: ReceiveMessage) -> None:
"""Handle new MQTT messages."""
payload = self._value_template(msg.payload)
if payload == self._config[CONF_STATE_LOCKED]:
self._attr_is_locked = True
elif payload == self._config[CONF_STATE_UNLOCKED]:
self._attr_is_locked = False
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
2016-02-28 17:25:28 +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:
self._sub_state = subscription.async_prepare_subscribe_topics(
2019-07-31 19:25:30 +00:00
self.hass,
self._sub_state,
{
"state_topic": {
"topic": self._config.get(CONF_STATE_TOPIC),
"msg_callback": message_received,
"qos": self._config[CONF_QOS],
"encoding": self._config[CONF_ENCODING] or None,
2019-07-31 19:25:30 +00:00
}
},
)
async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
await subscription.async_subscribe_topics(self.hass, self._sub_state)
2016-02-28 17:25:28 +00:00
@property
def assumed_state(self) -> bool:
2016-02-28 17:25:28 +00:00
"""Return true if we do optimistic updates."""
return self._optimistic
async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device.
This method is a coroutine.
"""
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],
self._config[CONF_PAYLOAD_LOCK],
self._config[CONF_QOS],
2019-07-31 19:25:30 +00:00
self._config[CONF_RETAIN],
self._config[CONF_ENCODING],
2019-07-31 19:25:30 +00:00
)
2016-02-28 17:25:28 +00:00
if self._optimistic:
# Optimistically assume that the lock has changed state.
self._attr_is_locked = True
self.async_write_ha_state()
async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the device.
2016-02-28 17:25:28 +00:00
This method is a coroutine.
"""
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],
self._config[CONF_PAYLOAD_UNLOCK],
self._config[CONF_QOS],
2019-07-31 19:25:30 +00:00
self._config[CONF_RETAIN],
self._config[CONF_ENCODING],
2019-07-31 19:25:30 +00:00
)
2016-02-28 17:25:28 +00:00
if self._optimistic:
# Optimistically assume that the lock has changed state.
self._attr_is_locked = False
self.async_write_ha_state()
async def async_open(self, **kwargs: Any) -> None:
"""Open the door latch.
This method is a coroutine.
"""
2022-02-04 16:35:32 +00:00
await self.async_publish(
self._config[CONF_COMMAND_TOPIC],
self._config[CONF_PAYLOAD_OPEN],
self._config[CONF_QOS],
self._config[CONF_RETAIN],
self._config[CONF_ENCODING],
)
if self._optimistic:
# Optimistically assume that the lock unlocks when opened.
self._attr_is_locked = False
self.async_write_ha_state()