core/tests/components/mqtt/test_lock.py

410 lines
13 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the MQTT lock platform."""
from homeassistant.components import lock
2020-03-17 07:09:19 +00:00
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_LOCKED, STATE_UNLOCKED
from homeassistant.setup import async_setup_component
from .test_common import (
2020-03-17 07:09:19 +00:00
help_test_availability_without_topic,
help_test_custom_availability_payload,
help_test_default_availability_payload,
help_test_discovery_broken,
help_test_discovery_removal,
help_test_discovery_update,
help_test_discovery_update_attr,
help_test_entity_debug_info_message,
help_test_entity_device_info_remove,
help_test_entity_device_info_update,
2020-03-17 07:09:19 +00:00
help_test_entity_device_info_with_connection,
help_test_entity_device_info_with_identifier,
help_test_entity_id_update_discovery_update,
help_test_entity_id_update_subscriptions,
help_test_setting_attribute_via_mqtt_json_message,
2020-03-17 07:09:19 +00:00
help_test_setting_attribute_with_template,
help_test_unique_id,
help_test_update_with_json_attrs_bad_JSON,
help_test_update_with_json_attrs_not_dict,
2019-07-31 19:25:30 +00:00
)
from tests.common import async_fire_mqtt_message
from tests.components.lock import common
2018-10-24 20:20:25 +00:00
2020-03-17 07:09:19 +00:00
DEFAULT_CONFIG = {
lock.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
}
2018-10-24 20:20:25 +00:00
async def test_controlling_state_via_topic(hass, mqtt_mock):
"""Test the controlling state via topic."""
2019-07-31 19:25:30 +00:00
assert await async_setup_component(
hass,
lock.DOMAIN,
{
lock.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
2019-07-31 19:25:30 +00:00
}
},
)
2018-10-24 20:20:25 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
2018-10-24 20:20:25 +00:00
assert state.state is STATE_UNLOCKED
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, "state-topic", "LOCKED")
2018-10-24 20:20:25 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
2018-10-24 20:20:25 +00:00
assert state.state is STATE_LOCKED
async_fire_mqtt_message(hass, "state-topic", "UNLOCKED")
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
async def test_controlling_non_default_state_via_topic(hass, mqtt_mock):
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
lock.DOMAIN,
{
lock.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"state_locked": "closed",
"state_unlocked": "open",
}
},
)
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, "state-topic", "closed")
state = hass.states.get("lock.test")
assert state.state is STATE_LOCKED
async_fire_mqtt_message(hass, "state-topic", "open")
2018-10-24 20:20:25 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
2018-10-24 20:20:25 +00:00
assert state.state is STATE_UNLOCKED
async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
"""Test the controlling state via topic and JSON message."""
2019-07-31 19:25:30 +00:00
assert await async_setup_component(
hass,
lock.DOMAIN,
{
lock.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
"value_template": "{{ value_json.val }}",
}
},
)
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
async_fire_mqtt_message(hass, "state-topic", '{"val":"LOCKED"}')
state = hass.states.get("lock.test")
assert state.state is STATE_LOCKED
async_fire_mqtt_message(hass, "state-topic", '{"val":"UNLOCKED"}')
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
async def test_controlling_non_default_state_via_topic_and_json_message(
hass, mqtt_mock
):
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
lock.DOMAIN,
{
lock.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"state_locked": "closed",
"state_unlocked": "open",
2019-07-31 19:25:30 +00:00
"value_template": "{{ value_json.val }}",
}
},
)
2018-10-24 20:20:25 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
2018-10-24 20:20:25 +00:00
assert state.state is STATE_UNLOCKED
async_fire_mqtt_message(hass, "state-topic", '{"val":"closed"}')
2018-10-24 20:20:25 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
2018-10-24 20:20:25 +00:00
assert state.state is STATE_LOCKED
async_fire_mqtt_message(hass, "state-topic", '{"val":"open"}')
2018-10-24 20:20:25 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
2018-10-24 20:20:25 +00:00
assert state.state is STATE_UNLOCKED
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
"""Test optimistic mode without state topic."""
2019-07-31 19:25:30 +00:00
assert await async_setup_component(
hass,
lock.DOMAIN,
{
lock.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
2019-07-31 19:25:30 +00:00
}
},
)
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
2019-07-31 19:25:30 +00:00
await common.async_lock(hass, "lock.test")
2019-07-31 19:25:30 +00:00
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
2019-07-31 19:25:30 +00:00
await common.async_unlock(hass, "lock.test")
2019-07-31 19:25:30 +00:00
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
2019-07-31 19:25:30 +00:00
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):
"""Test optimistic mode without state topic."""
2019-07-31 19:25:30 +00:00
assert await async_setup_component(
hass,
lock.DOMAIN,
{
lock.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
"payload_lock": "LOCK",
"payload_unlock": "UNLOCK",
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
2019-07-31 19:25:30 +00:00
"optimistic": True,
}
},
)
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
2019-07-31 19:25:30 +00:00
await common.async_lock(hass, "lock.test")
2019-07-31 19:25:30 +00:00
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
2019-07-31 19:25:30 +00:00
await common.async_unlock(hass, "lock.test")
2019-07-31 19:25:30 +00:00
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
2019-07-31 19:25:30 +00:00
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
2020-03-17 07:09:19 +00:00
async def test_availability_without_topic(hass, mqtt_mock):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
2019-07-31 19:25:30 +00:00
)
2018-10-24 20:20:25 +00:00
2020-03-17 07:09:19 +00:00
async def test_default_availability_payload(hass, mqtt_mock):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
2018-10-24 20:20:25 +00:00
async def test_custom_availability_payload(hass, mqtt_mock):
"""Test availability by custom payload with defined topic."""
2020-03-17 07:09:19 +00:00
await help_test_custom_availability_payload(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
2019-07-31 19:25:30 +00:00
)
2018-10-24 20:20:25 +00:00
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
2020-03-17 07:09:19 +00:00
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_setting_attribute_with_template(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
2019-07-31 19:25:30 +00:00
)
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
2020-03-17 07:09:19 +00:00
hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG
2019-07-31 19:25:30 +00:00
)
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
2020-03-17 07:09:19 +00:00
hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG
2019-07-31 19:25:30 +00:00
)
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
2020-03-17 07:09:19 +00:00
hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_unique_id(hass):
"""Test unique id option only creates one lock per unique_id."""
config = {
lock.DOMAIN: [
{
"platform": "mqtt",
"name": "Test 1",
"state_topic": "test-topic",
"command_topic": "test_topic",
"unique_id": "TOTALLY_UNIQUE",
},
{
"platform": "mqtt",
"name": "Test 2",
"state_topic": "test-topic",
"command_topic": "test_topic",
"unique_id": "TOTALLY_UNIQUE",
},
]
}
await help_test_unique_id(hass, lock.DOMAIN, config)
async def test_discovery_removal_lock(hass, mqtt_mock, caplog):
"""Test removal of discovered lock."""
data = '{ "name": "test",' ' "command_topic": "test_topic" }'
await help_test_discovery_removal(hass, mqtt_mock, caplog, lock.DOMAIN, data)
async def test_discovery_update_lock(hass, mqtt_mock, caplog):
"""Test update of discovered lock."""
data1 = (
'{ "name": "Beer",'
' "state_topic": "test_topic",'
' "command_topic": "command_topic",'
' "availability_topic": "availability_topic1" }'
)
data2 = (
'{ "name": "Milk",'
' "state_topic": "test_topic2",'
' "command_topic": "command_topic",'
' "availability_topic": "availability_topic2" }'
)
await help_test_discovery_update(hass, mqtt_mock, caplog, lock.DOMAIN, data1, data2)
async def test_discovery_broken(hass, mqtt_mock, caplog):
"""Test handling of bad discovery message."""
data1 = '{ "name": "Beer" }'
data2 = '{ "name": "Milk",' ' "command_topic": "test_topic" }'
await help_test_discovery_broken(hass, mqtt_mock, caplog, lock.DOMAIN, data1, data2)
2020-03-17 07:09:19 +00:00
async def test_entity_device_info_with_connection(hass, mqtt_mock):
"""Test MQTT lock device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT lock device registry integration."""
await help_test_entity_device_info_with_identifier(
2020-03-17 07:09:19 +00:00
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update."""
await help_test_entity_device_info_update(
2020-03-17 07:09:19 +00:00
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_remove(hass, mqtt_mock):
"""Test device registry remove."""
2020-03-17 07:09:19 +00:00
await help_test_entity_device_info_remove(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_message(hass, mqtt_mock):
"""Test MQTT debug info."""
await help_test_entity_debug_info_message(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
)