Rewrite MQTT & demo Lock tests (#33838)

* Refactor tests to remove tests.lock.common

* Remove unused is_locked hass binding

* Import constants via entity component

* Import constants via entity component

* Fix light vs lock in naming
This commit is contained in:
Franck Nijhof 2020-04-10 00:40:51 +02:00 committed by GitHub
parent 6b2baae0de
commit 30c6ace0f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 154 deletions

View file

@ -1,6 +1,12 @@
"""The tests for the MQTT lock platform."""
from homeassistant.components import lock
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_LOCKED, STATE_UNLOCKED
from homeassistant.components.lock import (
DOMAIN as LOCK_DOMAIN,
SERVICE_LOCK,
SERVICE_UNLOCK,
STATE_LOCKED,
STATE_UNLOCKED,
)
from homeassistant.const import ATTR_ASSUMED_STATE, ATTR_ENTITY_ID
from homeassistant.setup import async_setup_component
from .test_common import (
@ -26,10 +32,9 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.components.lock import common
DEFAULT_CONFIG = {
lock.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
LOCK_DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
}
@ -37,9 +42,9 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
lock.DOMAIN,
LOCK_DOMAIN,
{
lock.DOMAIN: {
LOCK_DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
@ -71,9 +76,9 @@ 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,
{
lock.DOMAIN: {
LOCK_DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
@ -105,9 +110,9 @@ async def test_controlling_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,
{
lock.DOMAIN: {
LOCK_DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
@ -141,9 +146,9 @@ async def test_controlling_non_default_state_via_topic_and_json_message(
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
lock.DOMAIN,
LOCK_DOMAIN,
{
lock.DOMAIN: {
LOCK_DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
@ -175,9 +180,9 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
"""Test optimistic mode without state topic."""
assert await async_setup_component(
hass,
lock.DOMAIN,
LOCK_DOMAIN,
{
lock.DOMAIN: {
LOCK_DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
@ -193,7 +198,9 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_lock(hass, "lock.test")
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
@ -201,7 +208,9 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_unlock(hass, "lock.test")
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
@ -214,9 +223,9 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock):
"""Test optimistic mode without state topic."""
assert await async_setup_component(
hass,
lock.DOMAIN,
LOCK_DOMAIN,
{
lock.DOMAIN: {
LOCK_DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
@ -234,7 +243,9 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock):
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_lock(hass, "lock.test")
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
@ -242,7 +253,9 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock):
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_unlock(hass, "lock.test")
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
mqtt_mock.async_publish.reset_mock()
@ -254,63 +267,63 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock):
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
hass, mqtt_mock, LOCK_DOMAIN, DEFAULT_CONFIG
)
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
hass, mqtt_mock, LOCK_DOMAIN, DEFAULT_CONFIG
)
async def test_custom_availability_payload(hass, mqtt_mock):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock, LOCK_DOMAIN, DEFAULT_CONFIG
)
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(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
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
hass, mqtt_mock, LOCK_DOMAIN, DEFAULT_CONFIG
)
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(
hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock, caplog, LOCK_DOMAIN, DEFAULT_CONFIG
)
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
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(
hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock, caplog, LOCK_DOMAIN, DEFAULT_CONFIG
)
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG
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: [
LOCK_DOMAIN: [
{
"platform": "mqtt",
"name": "Test 1",
@ -327,13 +340,13 @@ async def test_unique_id(hass):
},
]
}
await help_test_unique_id(hass, lock.DOMAIN, config)
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)
await help_test_discovery_removal(hass, mqtt_mock, caplog, LOCK_DOMAIN, data)
async def test_discovery_update_lock(hass, mqtt_mock, caplog):
@ -350,60 +363,60 @@ async def test_discovery_update_lock(hass, mqtt_mock, caplog):
' "command_topic": "command_topic",'
' "availability_topic": "availability_topic2" }'
)
await help_test_discovery_update(hass, mqtt_mock, caplog, lock.DOMAIN, data1, data2)
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)
await help_test_discovery_broken(hass, mqtt_mock, caplog, LOCK_DOMAIN, data1, data2)
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
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(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
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(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock, LOCK_DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_remove(hass, mqtt_mock):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG
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
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
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
hass, mqtt_mock, LOCK_DOMAIN, DEFAULT_CONFIG
)