Refactor MQTT tests to use modern platform schema part 2 (#77525)
* Tests light json * Tests light template * Missed test light json * Tests light * Tests lock * Tests number * Tests scene * Tests select * Tests sensor * Tests siren * Tests state vacuuum * Tests switch * Derive DEFAULT_CONFIG_LEGACY from DEFAULT_CONFIG * Suggested comment changes
This commit is contained in:
parent
a4792998a2
commit
13188a5c63
11 changed files with 1782 additions and 1414 deletions
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
|||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import select
|
||||
from homeassistant.components import mqtt, select
|
||||
from homeassistant.components.mqtt.select import MQTT_SELECT_ATTRIBUTES_BLOCKED
|
||||
from homeassistant.components.select import (
|
||||
ATTR_OPTION,
|
||||
|
@ -56,14 +56,20 @@ from .test_common import (
|
|||
from tests.common import async_fire_mqtt_message, mock_restore_cache
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
select.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"command_topic": "test-topic",
|
||||
"options": ["milk", "beer"],
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"name": "test",
|
||||
"command_topic": "test-topic",
|
||||
"options": ["milk", "beer"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Test deprecated YAML configuration under the platform key
|
||||
# Scheduled to be removed in HA core 2022.12
|
||||
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
|
||||
DEFAULT_CONFIG_LEGACY[select.DOMAIN]["platform"] = mqtt.DOMAIN
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def select_platform_only():
|
||||
|
@ -77,14 +83,15 @@ async def test_run_select_setup(hass, mqtt_mock_entry_with_yaml_config):
|
|||
topic = "test/select"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"select",
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -111,15 +118,16 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
|
|||
topic = "test/select"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"select",
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
"value_template": "{{ value_json.val }}",
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
"value_template": "{{ value_json.val }}",
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -157,13 +165,14 @@ async def test_run_select_service_optimistic(hass, mqtt_mock_entry_with_yaml_con
|
|||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
select.DOMAIN,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -198,14 +207,15 @@ async def test_run_select_service_optimistic_with_command_template(
|
|||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
select.DOMAIN,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
"command_template": '{"option": "{{ value }}"}',
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
"command_template": '{"option": "{{ value }}"}',
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -238,14 +248,15 @@ async def test_run_select_service(hass, mqtt_mock_entry_with_yaml_config):
|
|||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
select.DOMAIN,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"command_topic": cmd_topic,
|
||||
"state_topic": state_topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"command_topic": cmd_topic,
|
||||
"state_topic": state_topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -276,15 +287,16 @@ async def test_run_select_service_with_command_template(
|
|||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
select.DOMAIN,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"command_topic": cmd_topic,
|
||||
"state_topic": state_topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
"command_template": '{"option": "{{ value }}"}',
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"command_topic": cmd_topic,
|
||||
"state_topic": state_topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
"command_template": '{"option": "{{ value }}"}',
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -311,28 +323,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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
|
@ -341,7 +353,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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
|
@ -353,7 +365,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
|
|||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
select.DOMAIN,
|
||||
DEFAULT_CONFIG,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
MQTT_SELECT_ATTRIBUTES_BLOCKED,
|
||||
)
|
||||
|
||||
|
@ -361,7 +373,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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
|
@ -370,7 +382,11 @@ async def test_update_with_json_attrs_not_dict(
|
|||
):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_not_dict(
|
||||
hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass,
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
select.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
)
|
||||
|
||||
|
||||
|
@ -379,14 +395,22 @@ async def test_update_with_json_attrs_bad_JSON(
|
|||
):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_bad_JSON(
|
||||
hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass,
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
select.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
)
|
||||
|
||||
|
||||
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
|
||||
"""Test update of discovered MQTTAttributes."""
|
||||
await help_test_discovery_update_attr(
|
||||
hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
caplog,
|
||||
select.DOMAIN,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
)
|
||||
|
||||
|
||||
|
@ -419,7 +443,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
|
|||
|
||||
async def test_discovery_removal_select(hass, mqtt_mock_entry_no_yaml_config, caplog):
|
||||
"""Test removal of discovered select."""
|
||||
data = json.dumps(DEFAULT_CONFIG[select.DOMAIN])
|
||||
data = json.dumps(DEFAULT_CONFIG_LEGACY[select.DOMAIN])
|
||||
await help_test_discovery_removal(
|
||||
hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, data
|
||||
)
|
||||
|
@ -477,42 +501,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 select device registry integration."""
|
||||
await help_test_entity_device_info_with_connection(
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
|
||||
"""Test MQTT select device registry integration."""
|
||||
await help_test_entity_device_info_with_identifier(
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
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, select.DOMAIN, DEFAULT_CONFIG
|
||||
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
|
||||
)
|
||||
|
||||
|
||||
|
@ -522,7 +546,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
|
|||
hass,
|
||||
mqtt_mock_entry_no_yaml_config,
|
||||
select.DOMAIN,
|
||||
DEFAULT_CONFIG,
|
||||
DEFAULT_CONFIG_LEGACY,
|
||||
select.SERVICE_SELECT_OPTION,
|
||||
service_parameters={ATTR_OPTION: "beer"},
|
||||
command_payload="beer",
|
||||
|
@ -536,14 +560,15 @@ async def test_options_attributes(hass, mqtt_mock_entry_with_yaml_config, option
|
|||
topic = "test/select"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"select",
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test select",
|
||||
"options": options,
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test select",
|
||||
"options": options,
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -561,14 +586,15 @@ async def test_mqtt_payload_not_an_option_warning(
|
|||
topic = "test/select"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"select",
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
"select": {
|
||||
"platform": "mqtt",
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
mqtt.DOMAIN: {
|
||||
select.DOMAIN: {
|
||||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Select",
|
||||
"options": ["milk", "beer"],
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -609,7 +635,7 @@ async def test_publishing_with_custom_encoding(
|
|||
):
|
||||
"""Test publishing MQTT payload with different encoding."""
|
||||
domain = select.DOMAIN
|
||||
config = DEFAULT_CONFIG[domain]
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
config["options"] = ["milk", "beer"]
|
||||
|
||||
await help_test_publishing_with_custom_encoding(
|
||||
|
@ -629,7 +655,7 @@ 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 = select.DOMAIN
|
||||
config = DEFAULT_CONFIG[domain]
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
await help_test_reloadable(
|
||||
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
|
||||
)
|
||||
|
@ -638,7 +664,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
|
|||
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
|
||||
"""Test reloading the MQTT platform with late entry setup."""
|
||||
domain = select.DOMAIN
|
||||
config = DEFAULT_CONFIG[domain]
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
|
||||
|
||||
|
||||
|
@ -659,7 +685,7 @@ async def test_encoding_subscribable_topics(
|
|||
attribute_value,
|
||||
):
|
||||
"""Test handling of incoming encoded payload."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG["select"])
|
||||
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY["select"])
|
||||
config["options"] = ["milk", "beer"]
|
||||
await help_test_encoding_subscribable_topics(
|
||||
hass,
|
||||
|
@ -677,7 +703,7 @@ async def test_encoding_subscribable_topics(
|
|||
async def test_setup_manual_entity_from_yaml(hass):
|
||||
"""Test setup manual configured MQTT entity."""
|
||||
platform = select.DOMAIN
|
||||
config = copy.deepcopy(DEFAULT_CONFIG[platform])
|
||||
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
|
||||
config["name"] = "test"
|
||||
del config["platform"]
|
||||
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
|
||||
|
@ -687,7 +713,20 @@ async def test_setup_manual_entity_from_yaml(hass):
|
|||
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
|
||||
"""Test unloading the config entry."""
|
||||
domain = select.DOMAIN
|
||||
config = DEFAULT_CONFIG[domain]
|
||||
config = DEFAULT_CONFIG_LEGACY[domain]
|
||||
await help_test_unload_config_entry_with_platform(
|
||||
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
|
||||
)
|
||||
|
||||
|
||||
# Test deprecated YAML configuration under the platform key
|
||||
# Scheduled to be removed in HA core 2022.12
|
||||
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test a setup with deprecated yaml platform schema."""
|
||||
domain = select.DOMAIN
|
||||
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
|
||||
config["name"] = "test"
|
||||
assert await async_setup_component(hass, domain, {domain: config})
|
||||
await hass.async_block_till_done()
|
||||
await mqtt_mock_entry_with_yaml_config()
|
||||
assert hass.states.get(f"{domain}.test") is not None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue