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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 1023 additions and 969 deletions

View file

@ -17,6 +17,7 @@ from homeassistant.components.mqtt import debug_info
from homeassistant.components.mqtt.config_integration import PLATFORM_CONFIG_SCHEMA_BASE
from homeassistant.components.mqtt.const import MQTT_DISCONNECTED
from homeassistant.components.mqtt.mixins import MQTT_ATTRIBUTES_BLOCKED
from homeassistant.components.mqtt.models import PublishPayloadType
from homeassistant.config import async_log_exception
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import (
@ -73,7 +74,7 @@ MQTT_YAML_SCHEMA = vol.Schema({mqtt.DOMAIN: PLATFORM_CONFIG_SCHEMA_BASE})
def help_test_validate_platform_config(
hass: HomeAssistant, domain: str, config: ConfigType
hass: HomeAssistant, config: ConfigType
) -> ConfigType | None:
"""Test the schema validation."""
try:
@ -82,7 +83,7 @@ def help_test_validate_platform_config(
return True
except vol.Error as exc:
# log schema exceptions
async_log_exception(exc, domain, config, hass)
async_log_exception(exc, mqtt.DOMAIN, config, hass)
return False
@ -394,10 +395,10 @@ async def help_test_default_availability_list_single(
{"topic": "availability-topic1"},
]
config[mqtt.DOMAIN][domain]["availability_topic"] = "availability-topic"
help_test_validate_platform_config(hass, domain, config)
help_test_validate_platform_config(hass, config)
assert (
f"Invalid config for [{domain}]: two or more values in the same group of exclusion 'availability'"
"Invalid config for [mqtt]: two or more values in the same group of exclusion 'availability'"
in caplog.text
)
@ -698,14 +699,12 @@ async def help_test_discovery_update_attr(
async def help_test_unique_id(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
domain: str,
config: ConfigType,
) -> None:
"""Test unique id option only creates one entity per unique_id."""
assert await async_setup_component(hass, mqtt.DOMAIN, config)
await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert len(hass.states.async_entity_ids(domain)) == 1
@ -854,15 +853,14 @@ async def help_test_discovery_broken(
async def help_test_encoding_subscribable_topics(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
domain: str,
config: ConfigType,
topic: str,
value: Any,
attribute: str | None = None,
attribute_value: Any = None,
init_payload: str | None = None,
init_payload: tuple[str, str] | None = None,
skip_raw_test: bool = False,
) -> None:
"""Test handling of incoming encoded payload."""
@ -929,13 +927,17 @@ async def help_test_encoding_subscribable_topics(
init_payload_value_utf8 = init_payload[1].encode("utf-8")
init_payload_value_utf16 = init_payload[1].encode("utf-16")
await hass.async_block_till_done()
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {domain: [config1, config2, config3]}}
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(
hass, f"homeassistant/{domain}/item1/config", json.dumps(config1)
)
async_fire_mqtt_message(
hass, f"homeassistant/{domain}/item2/config", json.dumps(config2)
)
async_fire_mqtt_message(
hass, f"homeassistant/{domain}/item3/config", json.dumps(config3)
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
expected_result = attribute_value or value
@ -1124,7 +1126,7 @@ async def help_test_entity_device_info_update(
async def help_test_entity_id_update_subscriptions(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
domain: str,
config: ConfigType,
topics: list[str] | None = None,
@ -1142,13 +1144,10 @@ async def help_test_entity_id_update_subscriptions(
assert len(topics) > 0
entity_registry = er.async_get(hass)
assert await async_setup_component(
hass,
mqtt.DOMAIN,
config,
mqtt_mock = await help_setup_component(
hass, mqtt_mock_entry_no_yaml_config, domain, config, True
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
assert mqtt_mock is not None
state = hass.states.get(f"{domain}.test")
assert state is not None
@ -1632,7 +1631,7 @@ async def help_test_entity_category(
async def help_test_publishing_with_custom_encoding(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
domain: str,
config: ConfigType,
@ -1642,7 +1641,7 @@ async def help_test_publishing_with_custom_encoding(
payload: str,
template: str | None,
tpl_par: str = "value",
tpl_output: str | None = None,
tpl_output: PublishPayloadType = None,
) -> None:
"""Test a service with publishing MQTT payload with different encoding."""
# prepare config for tests
@ -1676,14 +1675,16 @@ async def help_test_publishing_with_custom_encoding(
if parameters:
service_data[test_id].update(parameters)
# setup test entities
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{mqtt.DOMAIN: {domain: setup_config}},
)
# setup test entities using discovery
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
item: int = 0
for component_config in setup_config:
conf = json.dumps(component_config)
item += 1
async_fire_mqtt_message(
hass, f"homeassistant/{domain}/component_{item}/config", conf
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
# 1) test with default encoding
await hass.services.async_call(
@ -1692,6 +1693,7 @@ async def help_test_publishing_with_custom_encoding(
service_data["test1"],
blocking=True,
)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_any_call("cmd/test1", str(payload), 0, False)
mqtt_mock.async_publish.reset_mock()
@ -1816,8 +1818,7 @@ async def help_test_reloadable(
# We should call await mqtt.async_setup_entry(hass, entry) when async_setup
# is removed (this is planned with #87987). Until then we set up the mqtt component
# to test reload after the async_setup setup has set the initial config
await async_setup_component(hass, mqtt.DOMAIN, old_config)
await hass.async_block_till_done()
await help_setup_component(hass, None, domain, old_config, use_discovery=False)
assert hass.states.get(f"{domain}.test_old_1")
assert hass.states.get(f"{domain}.test_old_2")
@ -1852,23 +1853,6 @@ async def help_test_reloadable(
assert hass.states.get(f"{domain}.test_new_3")
async def help_test_setup_manual_entity_from_yaml(
hass: HomeAssistant, config: ConfigType
) -> None:
"""Help to test setup from yaml through configuration entry."""
# until `async_setup` does the initial config setup, we need to use
# async_setup_component to test with other yaml config
assert await async_setup_component(hass, mqtt.DOMAIN, config)
# Mock config entry
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry.add_to_hass(hass)
with patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = lambda *args: 0
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def help_test_unload_config_entry(hass: HomeAssistant) -> None:
"""Test unloading the MQTT config entry."""
mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
@ -1892,9 +1876,9 @@ async def help_test_unload_config_entry_with_platform(
config_setup: dict[str, dict[str, Any]] = copy.deepcopy(config)
config_setup[mqtt.DOMAIN][domain]["name"] = "config_setup"
config_name = config_setup
# To be replaced with entry setup when `async_setup` is removed.
assert await async_setup_component(hass, mqtt.DOMAIN, config_setup)
await hass.async_block_till_done()
await help_setup_component(
hass, mqtt_mock_entry_no_yaml_config, domain, config_setup
)
# prepare setup through discovery
discovery_setup = copy.deepcopy(config[mqtt.DOMAIN][domain])