mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Refactor common MQTT tests to use modern schema (#77583)
* Common tests availability * Common tests attributes * Common tests unique id * Common tests discovery * Common tests encoding * Common tests device info * Common tests entity_id updated * Common tests entity debug info * Common test entity category * Common tests setup reload unload+corrections * Cleanup sweep * Comments from curent change * Cleanup * Remove unused legacy config
This commit is contained in:
@@ -43,7 +43,7 @@ from .test_common import (
|
||||
help_test_setup_manual_entity_from_yaml,
|
||||
help_test_unique_id,
|
||||
help_test_unload_config_entry_with_platform,
|
||||
help_test_update_with_json_attrs_bad_JSON,
|
||||
help_test_update_with_json_attrs_bad_json,
|
||||
help_test_update_with_json_attrs_not_dict,
|
||||
)
|
||||
|
||||
@@ -227,27 +227,28 @@ async def test_availability_when_connection_lost(
|
||||
):
|
||||
"""Test availability after MQTT disconnection."""
|
||||
await help_test_availability_when_connection_lost(
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test availability without defined availability topic."""
|
||||
await help_test_availability_without_topic(
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test availability by default payload with defined topic."""
|
||||
config = {
|
||||
switch.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"payload_on": 1,
|
||||
"payload_off": 0,
|
||||
mqtt.DOMAIN: {
|
||||
switch.DOMAIN: {
|
||||
"name": "test",
|
||||
"state_topic": "state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"payload_on": 1,
|
||||
"payload_off": 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,13 +266,14 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf
|
||||
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
config = {
|
||||
switch.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"payload_on": 1,
|
||||
"payload_off": 0,
|
||||
mqtt.DOMAIN: {
|
||||
switch.DOMAIN: {
|
||||
"name": "test",
|
||||
"state_topic": "state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"payload_on": 1,
|
||||
"payload_off": 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,7 +330,7 @@ async def test_setting_attribute_via_mqtt_json_message(
|
||||
):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
await help_test_setting_attribute_via_mqtt_json_message(
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -337,14 +339,14 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
await help_test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY, {}
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG, {}
|
||||
)
|
||||
|
||||
|
||||
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
await help_test_setting_attribute_with_template(
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -357,20 +359,20 @@ async def test_update_with_json_attrs_not_dict(
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
switch.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
async def test_update_with_json_attrs_bad_JSON(
|
||||
async def test_update_with_json_attrs_bad_json(
|
||||
hass, mqtt_mock_entry_with_yaml_config, caplog
|
||||
):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_bad_JSON(
|
||||
await help_test_update_with_json_attrs_bad_json(
|
||||
hass,
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
switch.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
@@ -381,29 +383,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
caplog,
|
||||
switch.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test unique id option only creates one switch per unique_id."""
|
||||
config = {
|
||||
switch.DOMAIN: [
|
||||
{
|
||||
"platform": "mqtt",
|
||||
"name": "Test 1",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "command-topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
{
|
||||
"platform": "mqtt",
|
||||
"name": "Test 2",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "command-topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
]
|
||||
mqtt.DOMAIN: {
|
||||
switch.DOMAIN: [
|
||||
{
|
||||
"name": "Test 1",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "command-topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
{
|
||||
"name": "Test 2",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "command-topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
await help_test_unique_id(
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, config
|
||||
@@ -426,8 +428,8 @@ async def test_discovery_update_switch_topic_template(
|
||||
hass, mqtt_mock_entry_no_yaml_config, caplog
|
||||
):
|
||||
"""Test update of discovered switch."""
|
||||
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
|
||||
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
|
||||
config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN])
|
||||
config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN])
|
||||
config1["name"] = "Beer"
|
||||
config2["name"] = "Milk"
|
||||
config1["state_topic"] = "switch/state1"
|
||||
@@ -463,8 +465,8 @@ async def test_discovery_update_switch_template(
|
||||
hass, mqtt_mock_entry_no_yaml_config, caplog
|
||||
):
|
||||
"""Test update of discovered switch."""
|
||||
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
|
||||
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
|
||||
config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN])
|
||||
config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN])
|
||||
config1["name"] = "Beer"
|
||||
config2["name"] = "Milk"
|
||||
config1["state_topic"] = "switch/state1"
|
||||
@@ -534,42 +536,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
|
||||
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
|
||||
"""Test MQTT switch device registry integration."""
|
||||
await help_test_entity_device_info_with_connection(
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
|
||||
"""Test MQTT switch device registry integration."""
|
||||
await help_test_entity_device_info_with_identifier(
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
|
||||
"""Test device registry update."""
|
||||
await help_test_entity_device_info_update(
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
|
||||
"""Test device registry remove."""
|
||||
await help_test_entity_device_info_remove(
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||
await help_test_entity_id_update_subscriptions(
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
|
||||
"""Test MQTT discovery update when entity_id is updated."""
|
||||
await help_test_entity_id_update_discovery_update(
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -579,7 +581,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
switch.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
switch.SERVICE_TURN_ON,
|
||||
)
|
||||
|
||||
@@ -615,7 +617,7 @@ async def test_publishing_with_custom_encoding(
|
||||
):
|
||||
"""Test publishing MQTT payload with different encoding."""
|
||||
domain = switch.DOMAIN
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
config = DEFAULT_CONFIG
|
||||
|
||||
await help_test_publishing_with_custom_encoding(
|
||||
hass,
|
||||
@@ -634,12 +636,14 @@ async def test_publishing_with_custom_encoding(
|
||||
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
|
||||
"""Test reloading the MQTT platform."""
|
||||
domain = switch.DOMAIN
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
config = DEFAULT_CONFIG
|
||||
await help_test_reloadable(
|
||||
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
|
||||
)
|
||||
|
||||
|
||||
# Test deprecated YAML configuration under the platform key
|
||||
# Scheduled to be removed in HA core 2022.12
|
||||
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
|
||||
"""Test reloading the MQTT platform with late entry setup."""
|
||||
domain = switch.DOMAIN
|
||||
@@ -668,7 +672,7 @@ async def test_encoding_subscribable_topics(
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
switch.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY[switch.DOMAIN],
|
||||
DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN],
|
||||
topic,
|
||||
value,
|
||||
attribute,
|
||||
@@ -679,17 +683,14 @@ async def test_encoding_subscribable_topics(
|
||||
async def test_setup_manual_entity_from_yaml(hass):
|
||||
"""Test setup manual configured MQTT entity."""
|
||||
platform = switch.DOMAIN
|
||||
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
|
||||
config["name"] = "test"
|
||||
del config["platform"]
|
||||
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
|
||||
assert hass.states.get(f"{platform}.test") is not None
|
||||
await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG)
|
||||
assert hass.states.get(f"{platform}.test")
|
||||
|
||||
|
||||
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
|
||||
"""Test unloading the config entry."""
|
||||
domain = switch.DOMAIN
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
config = DEFAULT_CONFIG
|
||||
await help_test_unload_config_entry_with_platform(
|
||||
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user