Add separate command and state topics for mqtt lock (#29808)
* Update lock.py Allow different command and state topic + different command and state values. * Formatting updated after black run * TC updated to reflect different state & cmd values * Abbreviations for lock states added * additional non-default state test * whitespaces fixed * black formatting run
This commit is contained in:
1 parent
fd0375ac20
commit
1c2618d99a
3 files changed
+98
-6
No files matched your search
@@ -135,6 +135,8 @@ ABBREVIATIONS = {
|
||||
"stat_off": "state_off",
|
||||
"stat_on": "state_on",
|
||||
"stat_open": "state_open",
|
||||
"stat_locked": "state_locked",
|
||||
"stat_unlocked": "state_unlocked",
|
||||
"stat_t": "state_topic",
|
||||
"stat_tpl": "state_template",
|
||||
"stat_val_tpl": "state_value_template",
|
||||
|
||||
@@ -36,10 +36,16 @@ _LOGGER = logging.getLogger(__name__)
|
||||
CONF_PAYLOAD_LOCK = "payload_lock"
|
||||
CONF_PAYLOAD_UNLOCK = "payload_unlock"
|
||||
|
||||
CONF_STATE_LOCKED = "state_locked"
|
||||
CONF_STATE_UNLOCKED = "state_unlocked"
|
||||
|
||||
DEFAULT_NAME = "MQTT Lock"
|
||||
DEFAULT_OPTIMISTIC = False
|
||||
DEFAULT_PAYLOAD_LOCK = "LOCK"
|
||||
DEFAULT_PAYLOAD_UNLOCK = "UNLOCK"
|
||||
DEFAULT_STATE_LOCKED = "LOCKED"
|
||||
DEFAULT_STATE_UNLOCKED = "UNLOCKED"
|
||||
|
||||
PLATFORM_SCHEMA = (
|
||||
mqtt.MQTT_RW_PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
@@ -50,6 +56,10 @@ PLATFORM_SCHEMA = (
|
||||
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_UNIQUE_ID): cv.string,
|
||||
}
|
||||
)
|
||||
@@ -152,9 +162,9 @@ class MqttLock(
|
||||
payload = msg.payload
|
||||
if value_template is not None:
|
||||
payload = value_template.async_render_with_possible_json_value(payload)
|
||||
if payload == self._config[CONF_PAYLOAD_LOCK]:
|
||||
if payload == self._config[CONF_STATE_LOCKED]:
|
||||
self._state = True
|
||||
elif payload == self._config[CONF_PAYLOAD_UNLOCK]:
|
||||
elif payload == self._config[CONF_STATE_UNLOCKED]:
|
||||
self._state = False
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
Reference in new issue
Block a user