Mqtt prepare test base part1 (#89796)

* Refactor test_reloadable

* Refactor test_disabling_and_enabling_entry

* optimize test_unload_config_entry

* Cleanup help_test_unload_config_entry

* cleanup test_unload_entry

* Update test tls_version

* More tests to entry only

* Add validate and hassconfig patch

* Revert fixture changes patch_hass_config

* Follow up comments
pull/89804/head
Jan Bouwhuis 2023-03-16 15:57:01 +01:00 committed by GitHub
parent 886c2635ad
commit c81a38effb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 389 additions and 516 deletions

View File

@ -1,3 +1,11 @@
"""Test fixtures for mqtt component."""
import pytest
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
from tests.components.light.conftest import mock_light_profiles # noqa: F401
@pytest.fixture(autouse=True)
def patch_hass_config(mock_hass_config: None) -> None:
"""Patch configuration].yaml."""

View File

@ -1,7 +1,6 @@
"""The tests the MQTT alarm control panel component."""
import copy
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -64,11 +63,12 @@ from .test_common import (
help_test_unload_config_entry_with_platform,
help_test_update_with_json_attrs_bad_json,
help_test_update_with_json_attrs_not_dict,
help_test_validate_platform_config,
)
from tests.common import async_fire_mqtt_message
from tests.components.alarm_control_panel import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
CODE_NUMBER = "1234"
CODE_TEXT = "HELLO_CODE"
@ -173,26 +173,18 @@ async def test_fail_setup_without_state_or_command_topic(
) -> None:
"""Test for failing setup with no state or command topic."""
assert (
await async_setup_component(
hass,
mqtt.DOMAIN,
config,
)
is valid
help_test_validate_platform_config(hass, alarm_control_panel.DOMAIN, config)
== valid
)
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
async def test_update_state_via_state_topic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test updating with via state topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
DEFAULT_CONFIG,
)
await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
entity_id = "alarm_control_panel.test"
@ -1057,16 +1049,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = alarm_control_panel.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -1078,12 +1066,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = alarm_control_panel.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -54,7 +54,7 @@ from tests.common import (
async_fire_time_changed,
mock_restore_cache,
)
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -1071,16 +1071,12 @@ async def test_entity_debug_info_message(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = binary_sensor.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -1187,12 +1183,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = binary_sensor.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,6 +1,5 @@
"""The tests for the MQTT button platform."""
import copy
from pathlib import Path
from unittest.mock import patch
import pytest
@ -43,7 +42,7 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict,
)
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {button.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -525,16 +524,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = button.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -546,12 +541,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = button.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -2,7 +2,6 @@
from base64 import b64encode
from http import HTTPStatus
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -42,7 +41,11 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import ClientSessionGenerator, MqttMockHAClientGenerator
from tests.typing import (
ClientSessionGenerator,
MqttMockHAClientGenerator,
MqttMockPahoClient,
)
DEFAULT_CONFIG = {mqtt.DOMAIN: {camera.DOMAIN: {"name": "test", "topic": "test_topic"}}}
@ -427,16 +430,12 @@ async def test_entity_debug_info_message(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = camera.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -448,12 +447,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = camera.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,7 +1,6 @@
"""The tests for the mqtt climate component."""
import copy
import json
from pathlib import Path
from unittest.mock import call, patch
import pytest
@ -64,7 +63,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message
from tests.components.climate import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
ENTITY_CLIMATE = "climate.test"
@ -2007,16 +2006,12 @@ async def test_humidity_configuration_validity(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = climate.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -2028,12 +2023,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = climate.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -8,13 +8,16 @@ from typing import Any
from unittest.mock import ANY, MagicMock, patch
import pytest
import voluptuous as vol
import yaml
from homeassistant import config as hass_config
from homeassistant import config as module_hass_config
from homeassistant.components import mqtt
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.config import async_log_exception
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import (
ATTR_ASSUMED_STATE,
@ -30,7 +33,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG_DEVICE_INFO_ID = {
"identifiers": ["helloworld"],
@ -62,6 +65,22 @@ _MqttMessageType = list[tuple[str, str]]
_AttributesType = list[tuple[str, Any]]
_StateDataType = list[tuple[_MqttMessageType, str | None, _AttributesType | None]]
MQTT_YAML_SCHEMA = vol.Schema({mqtt.DOMAIN: PLATFORM_CONFIG_SCHEMA_BASE})
def help_test_validate_platform_config(
hass: HomeAssistant, domain: str, config: ConfigType
) -> ConfigType | None:
"""Test the schema validation."""
try:
# validate the schema
MQTT_YAML_SCHEMA(config)
return True
except vol.Error as exc:
# log schema exceptions
async_log_exception(exc, domain, config, hass)
return False
async def help_test_availability_when_connection_lost(
hass: HomeAssistant,
@ -1764,7 +1783,7 @@ async def help_test_reload_with_config(
new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_text() == new_yaml_config
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
with patch.object(module_hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
await hass.services.async_call(
"mqtt",
SERVICE_RELOAD,
@ -1785,9 +1804,9 @@ async def help_test_entry_reload_with_new_config(
new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_text() == new_yaml_config
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file), patch(
"paho.mqtt.client.Client"
) as mock_client:
with patch.object(
module_hass_config, "YAML_CONFIG_FILE", new_yaml_config_file
), patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = lambda *args: 0
# reload the config entry
assert await hass.config_entries.async_reload(mqtt_config_entry.entry_id)
@ -1797,13 +1816,12 @@ async def help_test_entry_reload_with_new_config(
async def help_test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
domain: str,
config: ConfigType,
) -> None:
"""Test reloading an MQTT platform."""
# Set up with empty config
config = copy.deepcopy(config[mqtt.DOMAIN][domain])
# Create and test an old config of 2 entities based on the config supplied
old_config_1 = copy.deepcopy(config)
@ -1814,10 +1832,15 @@ async def help_test_reloadable(
old_config = {
mqtt.DOMAIN: {domain: [old_config_1, old_config_2]},
}
assert await async_setup_component(hass, mqtt.DOMAIN, old_config)
# Start the MQTT entry with the old config
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry.add_to_hass(hass)
mqtt_client_mock.connect.return_value = 0
# 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 mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test_old_1")
assert hass.states.get(f"{domain}.test_old_2")
@ -1835,8 +1858,15 @@ async def help_test_reloadable(
new_config = {
mqtt.DOMAIN: {domain: [new_config_1, new_config_2, new_config_extra]},
}
await help_test_reload_with_config(hass, caplog, tmp_path, new_config)
module_hass_config.load_yaml_config_file.return_value = new_config
# Reload the mqtt entry with the new config
await hass.services.async_call(
"mqtt",
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert len(hass.states.async_all(domain)) == 3
@ -1849,38 +1879,24 @@ async def help_test_setup_manual_entity_from_yaml(
hass: HomeAssistant, config: ConfigType
) -> None:
"""Help to test setup from yaml through configuration entry."""
calls = MagicMock()
async def mock_reload(hass: HomeAssistant) -> None:
"""Mock reload."""
calls()
# 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(
"homeassistant.components.mqtt.async_reload_manual_mqtt_items",
side_effect=mock_reload,
), patch("paho.mqtt.client.Client") as mock_client:
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()
calls.assert_called_once()
async def help_test_unload_config_entry(
hass: HomeAssistant, tmp_path: Path, newconfig: ConfigType
) -> None:
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]
assert mqtt_config_entry.state is ConfigEntryState.LOADED
new_yaml_config_file = tmp_path / "configuration.yaml"
new_yaml_config = yaml.dump(newconfig)
new_yaml_config_file.write_text(new_yaml_config)
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id)
# work-a-round mypy bug https://github.com/python/mypy/issues/9005#issuecomment-1280985006
updated_config_entry = mqtt_config_entry
@ -1890,8 +1906,7 @@ async def help_test_unload_config_entry(
async def help_test_unload_config_entry_with_platform(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
domain: str,
config: dict[str, dict[str, Any]],
) -> None:
@ -1900,9 +1915,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 mqtt_mock_entry_with_yaml_config()
# prepare setup through discovery
discovery_setup = copy.deepcopy(config[mqtt.DOMAIN][domain])
@ -1919,7 +1934,7 @@ async def help_test_unload_config_entry_with_platform(
discovery_setup_entity = hass.states.get(f"{domain}.discovery_setup")
assert discovery_setup_entity
await help_test_unload_config_entry(hass, tmp_path, config_setup)
await help_test_unload_config_entry(hass)
async_fire_mqtt_message(
hass, f"homeassistant/{domain}/bla/config", json.dumps(discovery_setup)

View File

@ -1,5 +1,4 @@
"""The tests for the MQTT cover platform."""
from pathlib import Path
from unittest.mock import patch
import pytest
@ -78,7 +77,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {cover.DOMAIN: {"name": "test", "state_topic": "test-topic"}}
@ -3497,16 +3496,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = cover.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -3551,12 +3546,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = cover.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,11 +1,9 @@
"""The tests for MQTT device triggers."""
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from homeassistant import config as hass_config
import homeassistant.components.automation as automation
from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.mqtt import _LOGGER, DOMAIN, debug_info
@ -1443,7 +1441,6 @@ async def test_unload_entry(
calls,
device_registry: dr.DeviceRegistry,
mqtt_mock: MqttMockHAClient,
tmp_path: Path,
) -> None:
"""Test unloading the MQTT entry."""
@ -1486,7 +1483,7 @@ async def test_unload_entry(
await hass.async_block_till_done()
assert len(calls) == 1
await help_test_unload_config_entry(hass, tmp_path, {})
await help_test_unload_config_entry(hass)
# Rediscover message and fake short press 2 (non impact)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
@ -1495,12 +1492,8 @@ async def test_unload_entry(
await hass.async_block_till_done()
assert len(calls) == 1
# Start entry again
mqtt_entry = hass.config_entries.async_entries("mqtt")[0]
# Load the entry again
new_yaml_config_file = tmp_path / "configuration.yaml"
new_yaml_config_file.write_text("")
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
await hass.config_entries.async_setup(mqtt_entry.entry_id)
# Rediscover and fake short press 3

View File

@ -1588,7 +1588,7 @@ async def test_clean_up_registry_monitoring(
# Enload the entry
# The monitoring should be cleared
await help_test_unload_config_entry(hass, tmp_path, {})
await help_test_unload_config_entry(hass)
assert len(hooks) == 0

View File

@ -1,6 +1,5 @@
"""Test MQTT fans."""
import copy
from pathlib import Path
from unittest.mock import patch
import pytest
@ -66,7 +65,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message
from tests.components.fan import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -2004,16 +2003,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = fan.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -2025,12 +2020,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = fan.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,6 +1,5 @@
"""Test MQTT humidifiers."""
import copy
from pathlib import Path
from unittest.mock import patch
import pytest
@ -67,7 +66,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -1368,16 +1367,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = humidifier.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -1400,12 +1395,11 @@ async def test_config_schema_validation(hass: HomeAssistant) -> None:
async def test_unload_config_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = humidifier.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -14,7 +14,7 @@ import pytest
import voluptuous as vol
import yaml
from homeassistant import config as hass_config
from homeassistant import config as module_hass_config
from homeassistant.components import mqtt
from homeassistant.components.mqtt import CONFIG_SCHEMA, debug_info
from homeassistant.components.mqtt.client import EnsureJobAfterCooldown
@ -25,6 +25,7 @@ from homeassistant.const import (
ATTR_ASSUMED_STATE,
EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP,
SERVICE_RELOAD,
Platform,
UnitOfTemperature,
)
@ -41,7 +42,6 @@ from homeassistant.util.dt import utcnow
from .test_common import (
help_test_entry_reload_with_new_config,
help_test_reload_with_config,
help_test_setup_manual_entity_from_yaml,
)
@ -121,20 +121,6 @@ def record_calls(calls: list[ReceiveMessage]) -> MessageCallbackType:
return record_calls
@pytest.fixture
def empty_mqtt_config(
hass: HomeAssistant, tmp_path: Path
) -> Generator[Path, None, None]:
"""Fixture to provide an empty config from yaml."""
new_yaml_config_file = tmp_path / "configuration.yaml"
new_yaml_config_file.write_text("")
with patch.object(
hass_config, "YAML_CONFIG_FILE", new_yaml_config_file
) as empty_config:
yield empty_config
async def test_mqtt_connects_on_home_assistant_mqtt_setup(
hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient,
@ -303,8 +289,21 @@ async def test_command_template_value(hass: HomeAssistant) -> None:
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SELECT])
@pytest.mark.parametrize(
"config",
[
{
"command_topic": "test/select",
"name": "Test Select",
"options": ["milk", "beer"],
"command_template": '{"option": "{{ value }}", "entity_id": "{{ entity_id }}", "name": "{{ name }}", "this_object_state": "{{ this.state }}"}',
}
],
)
async def test_command_template_variables(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
config: ConfigType,
) -> None:
"""Test the rendering of entity variables."""
topic = "test/select"
@ -312,22 +311,10 @@ async def test_command_template_variables(
fake_state = ha.State("select.test_select", "milk")
mock_restore_cache(hass, (fake_state,))
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
"select": {
"command_topic": topic,
"name": "Test Select",
"options": ["milk", "beer"],
"command_template": '{"option": "{{ value }}", "entity_id": "{{ entity_id }}", "name": "{{ name }}", "this_object_state": "{{ this.state }}"}',
}
}
},
)
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()
async_fire_mqtt_message(hass, "homeassistant/select/bla/config", json.dumps(config))
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("select.test_select")
assert state and state.state == "milk"
@ -1694,7 +1681,6 @@ async def test_initial_setup_logs_error(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mqtt_client_mock: MqttMockPahoClient,
empty_mqtt_config: Path,
) -> None:
"""Test for setup failure if initial client connection fails."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
@ -1838,7 +1824,7 @@ async def test_setup_override_configuration(
new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_text() == new_yaml_config
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
with patch.object(module_hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
# Mock config entry
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
@ -1914,26 +1900,43 @@ async def test_setup_manual_mqtt_empty_platform(
@patch("homeassistant.components.mqtt.PLATFORMS", [])
@pytest.mark.parametrize(
("mqtt_config_entry_data", "protocol"),
[
(
{
mqtt.CONF_BROKER: "mock-broker",
mqtt.CONF_PROTOCOL: "3.1",
},
3,
),
(
{
mqtt.CONF_BROKER: "mock-broker",
mqtt.CONF_PROTOCOL: "3.1.1",
},
4,
),
(
{
mqtt.CONF_BROKER: "mock-broker",
mqtt.CONF_PROTOCOL: "5",
},
5,
),
],
)
async def test_setup_mqtt_client_protocol(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
protocol: int,
) -> None:
"""Test MQTT client protocol setup."""
with patch("paho.mqtt.client.Client") as mock_client:
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
mqtt.config_integration.CONF_PROTOCOL: "3.1",
}
},
)
mock_client.on_connect(return_value=0)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
await mqtt_mock_entry_no_yaml_config()
# check if protocol setup was correctly
assert mock_client.call_args[1]["protocol"] == 3
assert mock_client.call_args[1]["protocol"] == protocol
@patch("homeassistant.components.mqtt.client.TIMEOUT_ACK", 0.2)
@ -1999,18 +2002,20 @@ async def test_setup_raises_config_entry_not_ready_if_no_connect_broker(
@pytest.mark.parametrize(
("config", "insecure_param"),
("mqtt_config_entry_data", "insecure_param"),
[
({"certificate": "auto"}, "not set"),
({"certificate": "auto", "tls_insecure": False}, False),
({"certificate": "auto", "tls_insecure": True}, True),
({"broker": "test-broker", "certificate": "auto"}, "not set"),
(
{"broker": "test-broker", "certificate": "auto", "tls_insecure": False},
False,
),
({"broker": "test-broker", "certificate": "auto", "tls_insecure": True}, True),
],
)
@patch("homeassistant.components.mqtt.PLATFORMS", [])
async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
config: ConfigType,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
insecure_param: bool | str,
) -> None:
"""Test setup uses bundled certs when certificate is set to auto and insecure."""
@ -2028,13 +2033,8 @@ async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure(
with patch("paho.mqtt.client.Client") as mock_client:
mock_client().tls_set = mock_tls_set
mock_client().tls_insecure_set = mock_tls_insecure_set
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{mqtt.DOMAIN: config},
)
await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert calls
@ -2047,29 +2047,27 @@ async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure(
assert insecure_check["insecure"] == insecure_param
@pytest.mark.parametrize(
"mqtt_config_entry_data",
[
{
mqtt.CONF_BROKER: "mock-broker",
mqtt.CONF_CERTIFICATE: "auto",
}
],
)
async def test_tls_version(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test setup defaults for tls."""
calls = []
def mock_tls_set(
certificate, certfile=None, keyfile=None, tls_version=None
) -> None:
calls.append((certificate, certfile, keyfile, tls_version))
with patch("paho.mqtt.client.Client") as mock_client:
mock_client().tls_set = mock_tls_set
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{mqtt.DOMAIN: {"certificate": "auto"}},
)
await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert calls
assert calls[0][3] == ssl.PROTOCOL_TLS_CLIENT
assert (
mqtt_client_mock.tls_set.mock_calls[0][2]["tls_version"]
== ssl.PROTOCOL_TLS_CLIENT
)
@pytest.mark.parametrize(
@ -3258,7 +3256,6 @@ async def test_unload_config_entry(
hass: HomeAssistant,
mqtt_mock: MqttMockHAClient,
mqtt_client_mock: MqttMockPahoClient,
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test unloading the MQTT entry."""
@ -3272,10 +3269,6 @@ async def test_unload_config_entry(
mqtt_client_mock.reset_mock()
mqtt.publish(hass, "just_in_time", "published", qos=0, retain=False)
new_yaml_config_file = tmp_path / "configuration.yaml"
new_yaml_config = yaml.dump({})
new_yaml_config_file.write_text(new_yaml_config)
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file):
assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id)
new_mqtt_config_entry = mqtt_config_entry
mqtt_client_mock.publish.assert_any_call("just_in_time", "published", 0, False)
@ -3318,90 +3311,64 @@ async def test_publish_or_subscribe_without_valid_config_entry(
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
async def test_reload_entry_with_new_config(
hass: HomeAssistant, tmp_path: Path
) -> None:
"""Test reloading the config entry with a new yaml config."""
config_old = {
"mqtt": {"light": [{"name": "test_old1", "command_topic": "test-topic_old"}]}
}
config_yaml_new = {
@pytest.mark.parametrize(
"hass_config",
[
{
"mqtt": {
"light": [{"name": "test_new_modern", "command_topic": "test-topic_new"}]
},
"light": [
{"name": "test_new_modern", "command_topic": "test-topic_new"}
]
}
await help_test_setup_manual_entity_from_yaml(hass, config_old)
assert hass.states.get("light.test_old1") is not None
await help_test_entry_reload_with_new_config(hass, tmp_path, config_yaml_new)
assert hass.states.get("light.test_old1") is None
assert hass.states.get("light.test_new_modern") is not None
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
}
],
)
async def test_disabling_and_enabling_entry(
hass: HomeAssistant, tmp_path: Path
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test disabling and enabling the config entry."""
config_old = {
"mqtt": {"light": [{"name": "test_old1", "command_topic": "test-topic_old"}]}
}
config_yaml_new = {
"mqtt": {
"light": [{"name": "test_new_modern", "command_topic": "test-topic_new"}]
},
}
await help_test_setup_manual_entity_from_yaml(hass, config_old)
assert hass.states.get("light.test_old1") is not None
mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
assert mqtt_config_entry.state is ConfigEntryState.LOADED
new_yaml_config_file = tmp_path / "configuration.yaml"
new_yaml_config = yaml.dump(config_yaml_new)
new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_text() == new_yaml_config
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file), patch(
"paho.mqtt.client.Client"
) as mock_client:
mock_client().connect = lambda *args: 0
await mqtt_mock_entry_no_yaml_config()
entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
assert entry.state is ConfigEntryState.LOADED
# Late discovery of a light
config = '{"name": "abc", "command_topic": "test-topic"}'
async_fire_mqtt_message(hass, "homeassistant/light/abc/config", config)
# Disable MQTT config entry
await hass.config_entries.async_set_disabled_by(
mqtt_config_entry.entry_id, ConfigEntryDisabler.USER
entry.entry_id, ConfigEntryDisabler.USER
)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert (
"MQTT integration is disabled, skipping setup of discovered item MQTT light"
in caplog.text
)
new_mqtt_config_entry = mqtt_config_entry
new_mqtt_config_entry = entry
assert new_mqtt_config_entry.state is ConfigEntryState.NOT_LOADED
assert hass.states.get("light.test_old1") is None
# Enable the entry again
await hass.config_entries.async_set_disabled_by(
mqtt_config_entry.entry_id, None
)
await hass.config_entries.async_set_disabled_by(entry.entry_id, None)
await hass.async_block_till_done()
await hass.async_block_till_done()
new_mqtt_config_entry = mqtt_config_entry
new_mqtt_config_entry = entry
assert new_mqtt_config_entry.state is ConfigEntryState.LOADED
assert hass.states.get("light.test_old1") is None
assert hass.states.get("light.test_new_modern") is not None
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
@pytest.mark.parametrize(
("config", "unique"),
("hass_config", "unique"),
[
(
[
{
mqtt.DOMAIN: {
"light": [
{
"name": "test1",
"unique_id": "very_not_unique_deadbeef",
@ -3412,11 +3379,15 @@ async def test_disabling_and_enabling_entry(
"unique_id": "very_not_unique_deadbeef",
"command_topic": "test-topic_unique",
},
],
]
}
},
False,
),
(
[
{
mqtt.DOMAIN: {
"light": [
{
"name": "test1",
"unique_id": "very_unique_deadbeef1",
@ -3427,22 +3398,21 @@ async def test_disabling_and_enabling_entry(
"unique_id": "very_unique_deadbeef2",
"command_topic": "test-topic_unique",
},
],
]
}
},
True,
),
],
)
async def test_setup_manual_items_with_unique_ids(
hass: HomeAssistant,
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
config: ConfigType,
hass_config: ConfigType,
unique: bool,
) -> None:
"""Test setup manual items is generating unique id's."""
await help_test_setup_manual_entity_from_yaml(
hass, {mqtt.DOMAIN: {"light": config}}
)
await help_test_setup_manual_entity_from_yaml(hass, hass_config)
assert hass.states.get("light.test1") is not None
assert (hass.states.get("light.test2") is not None) == unique
@ -3450,9 +3420,13 @@ async def test_setup_manual_items_with_unique_ids(
# reload and assert again
caplog.clear()
await help_test_entry_reload_with_new_config(
hass, tmp_path, {"mqtt": {"light": config}}
await hass.services.async_call(
"mqtt",
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("light.test1") is not None
assert (hass.states.get("light.test2") is not None) == unique
@ -3489,11 +3463,10 @@ async def test_remove_unknown_conf_entry_options(
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
async def test_link_config_entry(
hass: HomeAssistant, tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""Test manual and dynamically setup entities are linked to the config entry."""
config_manual = {
@pytest.mark.parametrize(
"hass_config",
[
{
"mqtt": {
"light": [
{
@ -3504,6 +3477,12 @@ async def test_link_config_entry(
]
}
}
],
)
async def test_link_config_entry(
hass: HomeAssistant, hass_config: ConfigType, caplog: pytest.LogCaptureFixture
) -> None:
"""Test manual and dynamically setup entities are linked to the config entry."""
config_discovery = {
"name": "test_discovery",
"unique_id": "test_discovery_unique456",
@ -3511,7 +3490,7 @@ async def test_link_config_entry(
}
# set up manual item
await help_test_setup_manual_entity_from_yaml(hass, config_manual)
await help_test_setup_manual_entity_from_yaml(hass, hass_config)
# set up item through discovery
async_fire_mqtt_message(
@ -3540,7 +3519,10 @@ async def test_link_config_entry(
assert _check_entities() == 2
# reload entry and assert again
await help_test_entry_reload_with_new_config(hass, tmp_path, config_manual)
with patch("paho.mqtt.client.Client"):
await hass.config_entries.async_reload(mqtt_config_entry.entry_id)
await hass.async_block_till_done()
# manual set up item should remain
assert _check_entities() == 1
# set up item through discovery
@ -3551,7 +3533,13 @@ async def test_link_config_entry(
assert _check_entities() == 2
# reload manual configured items and assert again
await help_test_reload_with_config(hass, caplog, tmp_path, config_manual)
await hass.services.async_call(
"mqtt",
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert _check_entities() == 2

View File

@ -1,7 +1,6 @@
"""The tests for the Legacy Mqtt vacuum platform."""
from copy import deepcopy
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -65,7 +64,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message
from tests.components.vacuum import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -998,16 +997,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = vacuum.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(

View File

@ -169,7 +169,6 @@ mqtt:
"""
import copy
from pathlib import Path
from unittest.mock import call, patch
import pytest
@ -229,7 +228,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {light.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -3034,16 +3033,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = light.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -3310,12 +3305,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = light.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -79,7 +79,6 @@ light:
brightness_scale: 99
"""
import copy
from pathlib import Path
from unittest.mock import call, patch
import pytest
@ -131,7 +130,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -2280,16 +2279,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = light.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(

View File

@ -25,7 +25,6 @@ If your light doesn't support color temp feature, omit `color_temp_template`.
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
"""
import copy
from pathlib import Path
from unittest.mock import patch
import pytest
@ -77,7 +76,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -1252,16 +1251,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = light.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -1306,12 +1301,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = light.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -58,7 +58,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {lock.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -970,16 +970,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = lock.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -1022,12 +1018,11 @@ async def test_setup_manual_entity_from_yaml(
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = lock.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,6 +1,5 @@
"""The tests for mqtt number component."""
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -63,7 +62,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message, mock_restore_cache_with_extra_data
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {number.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -965,16 +964,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = number.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -1016,12 +1011,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = number.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,6 +1,5 @@
"""The tests for the MQTT scene platform."""
import copy
from pathlib import Path
from unittest.mock import patch
import pytest
@ -26,7 +25,7 @@ from .test_common import (
)
from tests.common import mock_restore_cache
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -244,16 +243,12 @@ async def test_discovery_broken(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = scene.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
@ -265,12 +260,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = scene.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,7 +1,6 @@
"""The tests for mqtt select component."""
import copy
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -55,7 +54,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -711,16 +710,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = select.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -764,14 +759,13 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = select.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -67,7 +67,7 @@ from tests.common import (
async_fire_time_changed,
mock_restore_cache_with_extra_data,
)
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {sensor.DOMAIN: {"name": "test", "state_topic": "test-topic"}}
@ -1265,16 +1265,12 @@ async def test_value_template_with_entity_id(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = sensor.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
async def test_cleanup_triggers_and_restoring_state(
@ -1410,12 +1406,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = sensor.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,6 +1,5 @@
"""The tests for the MQTT siren platform."""
import copy
from pathlib import Path
from typing import Any
from unittest.mock import patch
@ -53,7 +52,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {siren.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -1005,16 +1004,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = siren.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -1055,12 +1050,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = siren.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,7 +1,6 @@
"""The tests for the State vacuum Mqtt platform."""
from copy import deepcopy
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -62,7 +61,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message
from tests.components.vacuum import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
COMMAND_TOPIC = "vacuum/command"
SEND_COMMAND_TOPIC = "vacuum/send_command"
@ -718,16 +717,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = vacuum.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(

View File

@ -1,6 +1,5 @@
"""The tests for the MQTT switch platform."""
import copy
from pathlib import Path
from unittest.mock import patch
import pytest
@ -49,7 +48,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.switch import common
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {switch.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -681,16 +680,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -731,12 +726,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,7 +1,6 @@
"""The tests for MQTT tag scanner."""
import copy
import json
from pathlib import Path
from unittest.mock import ANY, patch
import pytest
@ -893,7 +892,6 @@ async def test_unload_entry(
device_registry: dr.DeviceRegistry,
mqtt_mock: MqttMockHAClient,
tag_mock,
tmp_path: Path,
) -> None:
"""Test unloading the MQTT entry."""
@ -910,7 +908,7 @@ async def test_unload_entry(
tag_mock.reset_mock()
await help_test_unload_config_entry(hass, tmp_path, {})
await help_test_unload_config_entry(hass)
await hass.async_block_till_done()
# Fake tag scan, should not be processed

View File

@ -1,7 +1,6 @@
"""The tests for the MQTT text platform."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pytest
@ -47,7 +46,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {text.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
@ -695,16 +694,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = text.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
@ -745,12 +740,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = text.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)

View File

@ -1,6 +1,5 @@
"""The tests for mqtt update component."""
import json
from pathlib import Path
from unittest.mock import patch
import pytest
@ -43,7 +42,7 @@ from .test_common import (
)
from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -678,26 +677,21 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test unloading the config entry."""
domain = update.DOMAIN
config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
hass, mqtt_mock_entry_no_yaml_config, domain, config
)
async def test_reloadable(
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test reloading the MQTT platform."""
domain = update.DOMAIN
config = DEFAULT_CONFIG
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
await help_test_reloadable(hass, mqtt_client_mock, domain, config)