Add command template and code_format support for MQTT lock (#85830)

* Add command template for MQTT lock

* Fix tests
This commit is contained in:
Jan Bouwhuis 2023-01-23 14:48:07 +01:00 committed by GitHub
parent 00e5f23249
commit f719ecf086
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 6 deletions

View file

@ -18,6 +18,7 @@ from homeassistant.components.lock import (
from homeassistant.components.mqtt.lock import MQTT_LOCK_ATTRIBUTES_BLOCKED
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_CODE,
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
Platform,
@ -298,6 +299,67 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_sending_mqtt_commands_with_template(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test sending commands with template."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"code_format": "^\\d{4}$",
"command_topic": "command-topic",
"command_template": '{ "{{ value }}": "{{ code }}" }',
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"payload_open": "OPEN",
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
}
}
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
lock.DOMAIN,
SERVICE_LOCK,
{ATTR_ENTITY_ID: "lock.test", ATTR_CODE: "1234"},
blocking=True,
)
mqtt_mock.async_publish.assert_called_once_with(
"command-topic", '{ "LOCK": "1234" }', 0, False
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("lock.test")
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
lock.DOMAIN,
SERVICE_UNLOCK,
{ATTR_ENTITY_ID: "lock.test", ATTR_CODE: "1234"},
blocking=True,
)
mqtt_mock.async_publish.assert_called_once_with(
"command-topic", '{ "UNLOCK": "1234" }', 0, False
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_sending_mqtt_commands_and_explicit_optimistic(
hass, mqtt_mock_entry_with_yaml_config
):