1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 04:50:05 +00:00

Prepare MQTT common tests part3 (#90022)

This commit is contained in:
Jan Bouwhuis
2023-03-21 09:19:20 +01:00
committed by GitHub
parent 23f136e9d6
commit d865440012
25 changed files with 1023 additions and 969 deletions

View File

@@ -1,6 +1,7 @@
"""The tests for the mqtt climate component."""
import copy
import json
from typing import Any
from unittest.mock import call, patch
import pytest
@@ -54,7 +55,6 @@ from .test_common import (
help_test_setting_attribute_via_mqtt_json_message,
help_test_setting_attribute_with_template,
help_test_setting_blocked_attribute_via_mqtt_json_message,
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,
@@ -1562,31 +1562,34 @@ async def test_discovery_update_attr(
)
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
climate.DOMAIN: [
{
"name": "Test 1",
"mode_state_topic": "test_topic1/state",
"mode_command_topic": "test_topic1/command",
"unique_id": "TOTALLY_UNIQUE",
},
{
"name": "Test 2",
"mode_state_topic": "test_topic2/state",
"mode_command_topic": "test_topic2/command",
"unique_id": "TOTALLY_UNIQUE",
},
]
}
}
],
)
async def test_unique_id(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test unique id option only creates one climate per unique_id."""
config = {
mqtt.DOMAIN: {
climate.DOMAIN: [
{
"name": "Test 1",
"mode_state_topic": "test_topic1/state",
"mode_command_topic": "test_topic1/command",
"unique_id": "TOTALLY_UNIQUE",
},
{
"name": "Test 2",
"mode_state_topic": "test_topic2/state",
"mode_command_topic": "test_topic2/command",
"unique_id": "TOTALLY_UNIQUE",
},
]
}
}
await help_test_unique_id(
hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, config
)
await help_test_unique_id(hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN)
@pytest.mark.parametrize(
@@ -1609,19 +1612,17 @@ async def test_unique_id(
)
async def test_encoding_subscribable_topics(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
topic,
value,
attribute,
attribute_value,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
topic: str,
value: str,
attribute: str | None,
attribute_value: Any,
) -> None:
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][climate.DOMAIN])
await help_test_encoding_subscribable_topics(
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
mqtt_mock_entry_no_yaml_config,
climate.DOMAIN,
config,
topic,
@@ -1727,7 +1728,7 @@ async def test_entity_device_info_remove(
async def test_entity_id_update_subscriptions(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test MQTT subscriptions are managed when entity_id is updated."""
config = {
@@ -1741,7 +1742,7 @@ async def test_entity_id_update_subscriptions(
}
await help_test_entity_id_update_subscriptions(
hass,
mqtt_mock_entry_with_yaml_config,
mqtt_mock_entry_no_yaml_config,
climate.DOMAIN,
config,
["test-topic", "avty-topic"],
@@ -1919,13 +1920,13 @@ async def test_precision_whole(
)
async def test_publishing_with_custom_encoding(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
service,
topic,
parameters,
payload,
template,
service: str,
topic: str,
parameters: dict[str, Any],
payload: str,
template: str | None,
) -> None:
"""Test publishing MQTT payload with different encoding."""
domain = climate.DOMAIN
@@ -1936,7 +1937,7 @@ async def test_publishing_with_custom_encoding(
await help_test_publishing_with_custom_encoding(
hass,
mqtt_mock_entry_with_yaml_config,
mqtt_mock_entry_no_yaml_config,
caplog,
domain,
config,
@@ -2016,10 +2017,13 @@ async def test_reloadable(
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
async def test_setup_manual_entity_from_yaml(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test setup manual configured MQTT entity."""
await mqtt_mock_entry_no_yaml_config()
platform = climate.DOMAIN
await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG)
assert hass.states.get(f"{platform}.test")