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

@ -79,6 +79,7 @@ light:
brightness_scale: 99
"""
import copy
from typing import Any
from unittest.mock import call, patch
import pytest
@ -87,6 +88,7 @@ from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.light.schema_basic import (
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
from homeassistant.components.mqtt.models import PublishPayloadType
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_SUPPORTED_FEATURES,
@ -122,7 +124,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_update_with_json_attrs_bad_json,
help_test_update_with_json_attrs_not_dict,
@ -1996,33 +1997,36 @@ async def test_discovery_update_attr(
)
@pytest.mark.parametrize(
"hass_config",
[
{
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",
},
]
}
}
],
)
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",
"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
)
await help_test_unique_id(hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN)
async def test_discovery_removal(
@ -2167,11 +2171,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
)
@ -2247,15 +2251,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
@ -2265,7 +2269,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,
@ -2303,13 +2307,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])
@ -2324,8 +2327,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,
@ -2337,8 +2339,11 @@ async def test_encoding_subscribable_topics(
)
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")