mirror of
https://github.com/home-assistant/core.git
synced 2025-11-16 14:30:22 +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:
committed by
Paulus Schoutsen
parent
e7a616e8e4
commit
4894e2e5a4
@@ -125,7 +125,7 @@ from .test_common import (
|
||||
help_test_setting_blocked_attribute_via_mqtt_json_message,
|
||||
help_test_setup_manual_entity_from_yaml,
|
||||
help_test_unique_id,
|
||||
help_test_update_with_json_attrs_bad_JSON,
|
||||
help_test_update_with_json_attrs_bad_json,
|
||||
help_test_update_with_json_attrs_not_dict,
|
||||
)
|
||||
|
||||
@@ -1830,28 +1830,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, light.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.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, light.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test availability by default payload with defined topic."""
|
||||
await help_test_default_availability_payload(
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
await help_test_custom_availability_payload(
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -1860,7 +1860,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, light.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -1872,7 +1872,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
|
||||
)
|
||||
|
||||
@@ -1880,7 +1880,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
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, light.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -1893,20 +1893,20 @@ async def test_update_with_json_attrs_not_dict(
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
light.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,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
@@ -1917,31 +1917,31 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
caplog,
|
||||
light.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 light per unique_id."""
|
||||
config = {
|
||||
light.DOMAIN: [
|
||||
{
|
||||
"platform": "mqtt",
|
||||
"name": "Test 1",
|
||||
"schema": "json",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "test_topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
{
|
||||
"platform": "mqtt",
|
||||
"name": "Test 2",
|
||||
"schema": "json",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "test_topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
]
|
||||
mqtt.DOMAIN: {
|
||||
light.DOMAIN: [
|
||||
{
|
||||
"name": "Test 1",
|
||||
"schema": "json",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "test_topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
{
|
||||
"name": "Test 2",
|
||||
"schema": "json",
|
||||
"state_topic": "test-topic",
|
||||
"command_topic": "test_topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
await help_test_unique_id(
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, config
|
||||
@@ -2033,7 +2033,7 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
@@ -2043,7 +2043,7 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
@@ -2053,7 +2053,7 @@ async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
)
|
||||
|
||||
|
||||
@@ -2063,27 +2063,21 @@ async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
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,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
hass, mqtt_mock_entry_with_yaml_config, light.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,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -2093,7 +2087,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
light.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
DEFAULT_CONFIG,
|
||||
light.SERVICE_TURN_ON,
|
||||
command_payload='{"state":"ON"}',
|
||||
state_payload='{"state":"ON"}',
|
||||
@@ -2158,9 +2152,9 @@ async def test_publishing_with_custom_encoding(
|
||||
):
|
||||
"""Test publishing MQTT payload with different encoding."""
|
||||
domain = light.DOMAIN
|
||||
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
if topic == "effect_command_topic":
|
||||
config["effect_list"] = ["random", "color_loop"]
|
||||
config[mqtt.DOMAIN][domain]["effect_list"] = ["random", "color_loop"]
|
||||
|
||||
await help_test_publishing_with_custom_encoding(
|
||||
hass,
|
||||
@@ -2181,12 +2175,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 = light.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 = light.DOMAIN
|
||||
@@ -2217,7 +2213,7 @@ async def test_encoding_subscribable_topics(
|
||||
init_payload,
|
||||
):
|
||||
"""Test handling of incoming encoded payload."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN])
|
||||
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN])
|
||||
config["color_mode"] = True
|
||||
config["supported_color_modes"] = [
|
||||
"color_temp",
|
||||
@@ -2245,11 +2241,8 @@ async def test_encoding_subscribable_topics(
|
||||
async def test_setup_manual_entity_from_yaml(hass):
|
||||
"""Test setup manual configured MQTT entity."""
|
||||
platform = light.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")
|
||||
|
||||
|
||||
# Test deprecated YAML configuration under the platform key
|
||||
|
||||
Reference in New Issue
Block a user