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

@@ -169,6 +169,7 @@ mqtt:
"""
import copy
from typing import Any
from unittest.mock import call, patch
import pytest
@@ -186,6 +187,7 @@ from homeassistant.components.mqtt.light.schema_basic import (
CONF_XY_COMMAND_TOPIC,
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
from homeassistant.components.mqtt.models import PublishPayloadType
from homeassistant.const import (
ATTR_ASSUMED_STATE,
STATE_OFF,
@@ -219,7 +221,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,
@@ -2247,31 +2248,34 @@ async def test_discovery_update_attr(
)
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: [
{
"name": "Test 1",
"state_topic": "test-topic",
"command_topic": "test_topic",
"unique_id": "TOTALLY_UNIQUE",
},
{
"name": "Test 2",
"state_topic": "test-topic",
"command_topic": "test_topic",
"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 light per unique_id."""
config = {
mqtt.DOMAIN: {
light.DOMAIN: [
{
"name": "Test 1",
"state_topic": "test-topic",
"command_topic": "test_topic",
"unique_id": "TOTALLY_UNIQUE",
},
{
"name": "Test 2",
"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
)
await help_test_unique_id(hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN)
async def test_discovery_removal_light(
@@ -2859,11 +2863,11 @@ 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."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
)
@@ -2999,15 +3003,15 @@ async def test_max_mireds(
)
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,
tpl_par,
tpl_output,
service: str,
topic: str,
parameters: dict[str, Any],
payload: str,
template: str | None,
tpl_par: str,
tpl_output: PublishPayloadType,
) -> None:
"""Test publishing MQTT payload with different encoding."""
domain = light.DOMAIN
@@ -3019,7 +3023,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,
@@ -3075,13 +3079,12 @@ async def test_reloadable(
)
async def test_encoding_subscribable_topics(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
topic,
value,
attribute,
attribute_value,
init_payload,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
topic: str,
value: str,
attribute: str | None,
attribute_value: Any,
init_payload: tuple[str, str] | None,
) -> None:
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN])
@@ -3098,8 +3101,7 @@ async def test_encoding_subscribable_topics(
await help_test_encoding_subscribable_topics(
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
config,
topic,
@@ -3118,13 +3120,13 @@ async def test_encoding_subscribable_topics(
)
async def test_encoding_subscribable_topics_brightness(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
topic,
value,
attribute,
attribute_value,
init_payload,
topic: str,
value: str,
attribute: str,
attribute_value: int,
init_payload: tuple[str, str] | None,
) -> None:
"""Test handling of incoming encoded payload for a brightness only light."""
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN])
@@ -3132,8 +3134,7 @@ async def test_encoding_subscribable_topics_brightness(
await help_test_encoding_subscribable_topics(
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
config,
topic,
@@ -3298,10 +3299,13 @@ async def test_sending_mqtt_xy_command_with_template(
assert state.attributes["xy_color"] == (0.151, 0.343)
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 = light.DOMAIN
await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG)
assert hass.states.get(f"{platform}.test")