core/homeassistant/components/mqtt/lock.py

178 lines
5.9 KiB
Python
Raw Normal View History

"""Support for MQTT locks."""
import functools
2016-02-28 17:25:28 +00:00
import voluptuous as vol
from homeassistant.components import lock
from homeassistant.components.lock import LockEntity
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.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType
2016-02-28 17:25:28 +00:00
from . import PLATFORMS, subscription
from .. import mqtt
from .const import CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN, CONF_STATE_TOPIC, DOMAIN
from .debug_info import log_messages
from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper
2019-07-31 19:25:30 +00:00
CONF_PAYLOAD_LOCK = "payload_lock"
CONF_PAYLOAD_UNLOCK = "payload_unlock"
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_STATE_LOCKED = "LOCKED"
DEFAULT_STATE_UNLOCKED = "UNLOCKED"
MQTT_LOCK_ATTRIBUTES_BLOCKED = frozenset(
{
lock.ATTR_CHANGED_BY,
lock.ATTR_CODE_FORMAT,
}
)
PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_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_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
async def async_setup_platform(
hass: HomeAssistant, config: ConfigType, async_add_entities, discovery_info=None
2019-07-31 19:25:30 +00:00
):
"""Set up MQTT lock panel through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(hass, async_add_entities, config)
2018-03-18 16:26:07 +00:00
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up MQTT lock 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, PLATFORM_SCHEMA)
2019-07-31 19:25:30 +00:00
async def _async_setup_entity(
hass, async_add_entities, config, config_entry=None, discovery_data=None
2019-07-31 19:25:30 +00:00
):
"""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
_attributes_extra_blocked = MQTT_LOCK_ATTRIBUTES_BLOCKED
def __init__(self, hass, config, config_entry, discovery_data):
2016-03-07 21:13:18 +00:00
"""Initialize the lock."""
self._state = False
self._optimistic = 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():
"""Return the config schema."""
return PLATFORM_SCHEMA
def _setup_from_config(self, config):
"""(Re)Setup the entity."""
self._optimistic = config[CONF_OPTIMISTIC]
value_template = self._config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
value_template.hass = self.hass
2016-02-28 17:25:28 +00:00
async def _subscribe_topics(self):
"""(Re)Subscribe to topics."""
@callback
@log_messages(self.hass, self.entity_id)
def message_received(msg):
"""Handle new MQTT messages."""
payload = msg.payload
value_template = self._config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
2019-07-31 19:25:30 +00:00
payload = value_template.async_render_with_possible_json_value(payload)
if payload == self._config[CONF_STATE_LOCKED]:
2016-02-28 17:25:28 +00:00
self._state = True
elif payload == self._config[CONF_STATE_UNLOCKED]:
2016-02-28 17:25:28 +00:00
self._state = False
self.async_write_ha_state()
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 = await subscription.async_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],
}
},
)
2016-02-28 17:25:28 +00:00
@property
def is_locked(self):
"""Return true if lock is locked."""
return self._state
@property
def assumed_state(self):
"""Return true if we do optimistic updates."""
return self._optimistic
async def async_lock(self, **kwargs):
"""Lock the device.
This method is a coroutine.
"""
mqtt.async_publish(
2019-07-31 19:25:30 +00:00
self.hass,
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],
)
2016-02-28 17:25:28 +00:00
if self._optimistic:
# Optimistically assume that the lock has changed state.
2016-02-28 17:25:28 +00:00
self._state = True
self.async_write_ha_state()
async def async_unlock(self, **kwargs):
"""Unlock the device.
2016-02-28 17:25:28 +00:00
This method is a coroutine.
"""
mqtt.async_publish(
2019-07-31 19:25:30 +00:00
self.hass,
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],
)
2016-02-28 17:25:28 +00:00
if self._optimistic:
# Optimistically assume that the lock has changed state.
2016-02-28 17:25:28 +00:00
self._state = False
self.async_write_ha_state()