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
This commit is contained in:
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.""" """Test fixtures for mqtt component."""
import pytest
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
from tests.components.light.conftest import mock_light_profiles # 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.""" """The tests the MQTT alarm control panel component."""
import copy import copy
import json import json
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -64,11 +63,12 @@ from .test_common import (
help_test_unload_config_entry_with_platform, help_test_unload_config_entry_with_platform,
help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_bad_json,
help_test_update_with_json_attrs_not_dict, help_test_update_with_json_attrs_not_dict,
help_test_validate_platform_config,
) )
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.components.alarm_control_panel import common from tests.components.alarm_control_panel import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
CODE_NUMBER = "1234" CODE_NUMBER = "1234"
CODE_TEXT = "HELLO_CODE" CODE_TEXT = "HELLO_CODE"
@ -173,26 +173,18 @@ async def test_fail_setup_without_state_or_command_topic(
) -> None: ) -> None:
"""Test for failing setup with no state or command topic.""" """Test for failing setup with no state or command topic."""
assert ( assert (
await async_setup_component( help_test_validate_platform_config(hass, alarm_control_panel.DOMAIN, config)
hass, == valid
mqtt.DOMAIN,
config,
)
is valid
) )
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
async def test_update_state_via_state_topic( 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: ) -> None:
"""Test updating with via state topic.""" """Test updating with via state topic."""
assert await async_setup_component( await mqtt_mock_entry_no_yaml_config()
hass,
mqtt.DOMAIN,
DEFAULT_CONFIG,
)
await hass.async_block_till_done() await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
entity_id = "alarm_control_panel.test" entity_id = "alarm_control_panel.test"
@ -1057,16 +1049,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = alarm_control_panel.DOMAIN domain = alarm_control_panel.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = alarm_control_panel.DOMAIN domain = alarm_control_panel.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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, async_fire_time_changed,
mock_restore_cache, mock_restore_cache,
) )
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -1071,16 +1071,12 @@ async def test_entity_debug_info_message(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = binary_sensor.DOMAIN domain = binary_sensor.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -1187,12 +1183,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = binary_sensor.DOMAIN domain = binary_sensor.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for the MQTT button platform."""
import copy import copy
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -43,7 +42,7 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict, help_test_update_with_json_attrs_not_dict,
) )
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {button.DOMAIN: {"name": "test", "command_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = button.DOMAIN domain = button.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = button.DOMAIN domain = button.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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 base64 import b64encode
from http import HTTPStatus from http import HTTPStatus
import json import json
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -42,7 +41,11 @@ from .test_common import (
) )
from tests.common import async_fire_mqtt_message 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"}}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = camera.DOMAIN domain = camera.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = camera.DOMAIN domain = camera.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for the mqtt climate component."""
import copy import copy
import json import json
from pathlib import Path
from unittest.mock import call, patch from unittest.mock import call, patch
import pytest import pytest
@ -64,7 +63,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.components.climate import common from tests.components.climate import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
ENTITY_CLIMATE = "climate.test" ENTITY_CLIMATE = "climate.test"
@ -2007,16 +2006,12 @@ async def test_humidity_configuration_validity(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = climate.DOMAIN domain = climate.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = climate.DOMAIN domain = climate.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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 from unittest.mock import ANY, MagicMock, patch
import pytest import pytest
import voluptuous as vol
import yaml 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 import mqtt
from homeassistant.components.mqtt import debug_info 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.const import MQTT_DISCONNECTED
from homeassistant.components.mqtt.mixins import MQTT_ATTRIBUTES_BLOCKED from homeassistant.components.mqtt.mixins import MQTT_ATTRIBUTES_BLOCKED
from homeassistant.config import async_log_exception
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ( from homeassistant.const import (
ATTR_ASSUMED_STATE, ATTR_ASSUMED_STATE,
@ -30,7 +33,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, async_fire_mqtt_message 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 = { DEFAULT_CONFIG_DEVICE_INFO_ID = {
"identifiers": ["helloworld"], "identifiers": ["helloworld"],
@ -62,6 +65,22 @@ _MqttMessageType = list[tuple[str, str]]
_AttributesType = list[tuple[str, Any]] _AttributesType = list[tuple[str, Any]]
_StateDataType = list[tuple[_MqttMessageType, str | None, _AttributesType | None]] _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( async def help_test_availability_when_connection_lost(
hass: HomeAssistant, hass: HomeAssistant,
@ -1764,7 +1783,7 @@ async def help_test_reload_with_config(
new_yaml_config_file.write_text(new_yaml_config) new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_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( await hass.services.async_call(
"mqtt", "mqtt",
SERVICE_RELOAD, SERVICE_RELOAD,
@ -1785,9 +1804,9 @@ async def help_test_entry_reload_with_new_config(
new_yaml_config_file.write_text(new_yaml_config) new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_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( with patch.object(
"paho.mqtt.client.Client" module_hass_config, "YAML_CONFIG_FILE", new_yaml_config_file
) as mock_client: ), patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = lambda *args: 0 mock_client().connect = lambda *args: 0
# reload the config entry # reload the config entry
assert await hass.config_entries.async_reload(mqtt_config_entry.entry_id) 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( async def help_test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
domain: str, domain: str,
config: ConfigType, config: ConfigType,
) -> None: ) -> None:
"""Test reloading an MQTT platform.""" """Test reloading an MQTT platform."""
# Set up with empty config
config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config = copy.deepcopy(config[mqtt.DOMAIN][domain])
# Create and test an old config of 2 entities based on the config supplied # Create and test an old config of 2 entities based on the config supplied
old_config_1 = copy.deepcopy(config) old_config_1 = copy.deepcopy(config)
@ -1814,10 +1832,15 @@ async def help_test_reloadable(
old_config = { old_config = {
mqtt.DOMAIN: {domain: [old_config_1, old_config_2]}, mqtt.DOMAIN: {domain: [old_config_1, old_config_2]},
} }
# Start the MQTT entry with the old config
assert await async_setup_component(hass, mqtt.DOMAIN, 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 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_1")
assert hass.states.get(f"{domain}.test_old_2") assert hass.states.get(f"{domain}.test_old_2")
@ -1835,8 +1858,15 @@ async def help_test_reloadable(
new_config = { new_config = {
mqtt.DOMAIN: {domain: [new_config_1, new_config_2, new_config_extra]}, mqtt.DOMAIN: {domain: [new_config_1, new_config_2, new_config_extra]},
} }
module_hass_config.load_yaml_config_file.return_value = new_config
await help_test_reload_with_config(hass, caplog, tmp_path, 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 assert len(hass.states.async_all(domain)) == 3
@ -1849,49 +1879,34 @@ async def help_test_setup_manual_entity_from_yaml(
hass: HomeAssistant, config: ConfigType hass: HomeAssistant, config: ConfigType
) -> None: ) -> None:
"""Help to test setup from yaml through configuration entry.""" """Help to test setup from yaml through configuration entry."""
calls = MagicMock() # until `async_setup` does the initial config setup, we need to use
# async_setup_component to test with other yaml config
async def mock_reload(hass: HomeAssistant) -> None:
"""Mock reload."""
calls()
assert await async_setup_component(hass, mqtt.DOMAIN, config) assert await async_setup_component(hass, mqtt.DOMAIN, config)
# Mock config entry # Mock config entry
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}) entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry.add_to_hass(hass) entry.add_to_hass(hass)
with patch( with patch("paho.mqtt.client.Client") as mock_client:
"homeassistant.components.mqtt.async_reload_manual_mqtt_items",
side_effect=mock_reload,
), patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = lambda *args: 0 mock_client().connect = lambda *args: 0
assert await hass.config_entries.async_setup(entry.entry_id) assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
calls.assert_called_once()
async def help_test_unload_config_entry( async def help_test_unload_config_entry(hass: HomeAssistant) -> None:
hass: HomeAssistant, tmp_path: Path, newconfig: ConfigType
) -> None:
"""Test unloading the MQTT config entry.""" """Test unloading the MQTT config entry."""
mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
assert mqtt_config_entry.state is ConfigEntryState.LOADED assert mqtt_config_entry.state is ConfigEntryState.LOADED
new_yaml_config_file = tmp_path / "configuration.yaml" assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id)
new_yaml_config = yaml.dump(newconfig) # work-a-round mypy bug https://github.com/python/mypy/issues/9005#issuecomment-1280985006
new_yaml_config_file.write_text(new_yaml_config) updated_config_entry = mqtt_config_entry
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file): assert updated_config_entry.state is ConfigEntryState.NOT_LOADED
assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id) await hass.async_block_till_done()
# work-a-round mypy bug https://github.com/python/mypy/issues/9005#issuecomment-1280985006
updated_config_entry = mqtt_config_entry
assert updated_config_entry.state is ConfigEntryState.NOT_LOADED
await hass.async_block_till_done()
async def help_test_unload_config_entry_with_platform( async def help_test_unload_config_entry_with_platform(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
domain: str, domain: str,
config: dict[str, dict[str, Any]], config: dict[str, dict[str, Any]],
) -> None: ) -> 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: dict[str, dict[str, Any]] = copy.deepcopy(config)
config_setup[mqtt.DOMAIN][domain]["name"] = "config_setup" config_setup[mqtt.DOMAIN][domain]["name"] = "config_setup"
config_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) assert await async_setup_component(hass, mqtt.DOMAIN, config_setup)
await hass.async_block_till_done() await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
# prepare setup through discovery # prepare setup through discovery
discovery_setup = copy.deepcopy(config[mqtt.DOMAIN][domain]) 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") discovery_setup_entity = hass.states.get(f"{domain}.discovery_setup")
assert discovery_setup_entity 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( async_fire_mqtt_message(
hass, f"homeassistant/{domain}/bla/config", json.dumps(discovery_setup) hass, f"homeassistant/{domain}/bla/config", json.dumps(discovery_setup)

View file

@ -1,5 +1,4 @@
"""The tests for the MQTT cover platform.""" """The tests for the MQTT cover platform."""
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -78,7 +77,7 @@ from .test_common import (
) )
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {cover.DOMAIN: {"name": "test", "state_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = cover.DOMAIN domain = cover.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -3551,12 +3546,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = cover.DOMAIN domain = cover.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for MQTT device triggers."""
import json import json
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
from homeassistant import config as hass_config
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.mqtt import _LOGGER, DOMAIN, debug_info from homeassistant.components.mqtt import _LOGGER, DOMAIN, debug_info
@ -1443,7 +1441,6 @@ async def test_unload_entry(
calls, calls,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock: MqttMockHAClient, mqtt_mock: MqttMockHAClient,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the MQTT entry.""" """Test unloading the MQTT entry."""
@ -1486,7 +1483,7 @@ async def test_unload_entry(
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 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) # Rediscover message and fake short press 2 (non impact)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
@ -1495,13 +1492,9 @@ async def test_unload_entry(
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
# Start entry again
mqtt_entry = hass.config_entries.async_entries("mqtt")[0] mqtt_entry = hass.config_entries.async_entries("mqtt")[0]
await hass.config_entries.async_setup(mqtt_entry.entry_id)
# 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 # Rediscover and fake short press 3
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)

View file

@ -1588,7 +1588,7 @@ async def test_clean_up_registry_monitoring(
# Enload the entry # Enload the entry
# The monitoring should be cleared # 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 assert len(hooks) == 0

View file

@ -1,6 +1,5 @@
"""Test MQTT fans.""" """Test MQTT fans."""
import copy import copy
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -66,7 +65,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.components.fan import common from tests.components.fan import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -2004,16 +2003,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = fan.DOMAIN domain = fan.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = fan.DOMAIN domain = fan.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """Test MQTT humidifiers."""
import copy import copy
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -67,7 +66,7 @@ from .test_common import (
) )
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -1368,16 +1367,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = humidifier.DOMAIN domain = humidifier.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_config_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = humidifier.DOMAIN domain = humidifier.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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 voluptuous as vol
import yaml 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 import mqtt
from homeassistant.components.mqtt import CONFIG_SCHEMA, debug_info from homeassistant.components.mqtt import CONFIG_SCHEMA, debug_info
from homeassistant.components.mqtt.client import EnsureJobAfterCooldown from homeassistant.components.mqtt.client import EnsureJobAfterCooldown
@ -25,6 +25,7 @@ from homeassistant.const import (
ATTR_ASSUMED_STATE, ATTR_ASSUMED_STATE,
EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
SERVICE_RELOAD,
Platform, Platform,
UnitOfTemperature, UnitOfTemperature,
) )
@ -41,7 +42,6 @@ from homeassistant.util.dt import utcnow
from .test_common import ( from .test_common import (
help_test_entry_reload_with_new_config, help_test_entry_reload_with_new_config,
help_test_reload_with_config,
help_test_setup_manual_entity_from_yaml, help_test_setup_manual_entity_from_yaml,
) )
@ -121,20 +121,6 @@ def record_calls(calls: list[ReceiveMessage]) -> MessageCallbackType:
return record_calls 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( async def test_mqtt_connects_on_home_assistant_mqtt_setup(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient, mqtt_client_mock: MqttMockPahoClient,
@ -303,8 +289,21 @@ async def test_command_template_value(hass: HomeAssistant) -> None:
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SELECT]) @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( 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: ) -> None:
"""Test the rendering of entity variables.""" """Test the rendering of entity variables."""
topic = "test/select" topic = "test/select"
@ -312,22 +311,10 @@ async def test_command_template_variables(
fake_state = ha.State("select.test_select", "milk") fake_state = ha.State("select.test_select", "milk")
mock_restore_cache(hass, (fake_state,)) mock_restore_cache(hass, (fake_state,))
assert await async_setup_component( mqtt_mock = await mqtt_mock_entry_no_yaml_config()
hass, await hass.async_block_till_done()
mqtt.DOMAIN, async_fire_mqtt_message(hass, "homeassistant/select/bla/config", json.dumps(config))
{
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 }}"}',
}
}
},
)
await hass.async_block_till_done() await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state and state.state == "milk" assert state and state.state == "milk"
@ -1694,7 +1681,6 @@ async def test_initial_setup_logs_error(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
mqtt_client_mock: MqttMockPahoClient, mqtt_client_mock: MqttMockPahoClient,
empty_mqtt_config: Path,
) -> None: ) -> None:
"""Test for setup failure if initial client connection fails.""" """Test for setup failure if initial client connection fails."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}) 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) new_yaml_config_file.write_text(new_yaml_config)
assert new_yaml_config_file.read_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 # Mock config entry
entry = MockConfigEntry( entry = MockConfigEntry(
domain=mqtt.DOMAIN, domain=mqtt.DOMAIN,
@ -1914,26 +1900,43 @@ async def test_setup_manual_mqtt_empty_platform(
@patch("homeassistant.components.mqtt.PLATFORMS", []) @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( 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: ) -> None:
"""Test MQTT client protocol setup.""" """Test MQTT client protocol setup."""
with patch("paho.mqtt.client.Client") as mock_client: with patch("paho.mqtt.client.Client") as mock_client:
assert await async_setup_component( await mqtt_mock_entry_no_yaml_config()
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()
# check if protocol setup was correctly # 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) @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( @pytest.mark.parametrize(
("config", "insecure_param"), ("mqtt_config_entry_data", "insecure_param"),
[ [
({"certificate": "auto"}, "not set"), ({"broker": "test-broker", "certificate": "auto"}, "not set"),
({"certificate": "auto", "tls_insecure": False}, False), (
({"certificate": "auto", "tls_insecure": True}, True), {"broker": "test-broker", "certificate": "auto", "tls_insecure": False},
False,
),
({"broker": "test-broker", "certificate": "auto", "tls_insecure": True}, True),
], ],
) )
@patch("homeassistant.components.mqtt.PLATFORMS", []) @patch("homeassistant.components.mqtt.PLATFORMS", [])
async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure( async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
config: ConfigType,
insecure_param: bool | str, insecure_param: bool | str,
) -> None: ) -> None:
"""Test setup uses bundled certs when certificate is set to auto and insecure.""" """Test setup uses bundled certs when certificate is set to auto and insecure."""
@ -2028,48 +2033,41 @@ async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure(
with patch("paho.mqtt.client.Client") as mock_client: with patch("paho.mqtt.client.Client") as mock_client:
mock_client().tls_set = mock_tls_set mock_client().tls_set = mock_tls_set
mock_client().tls_insecure_set = mock_tls_insecure_set mock_client().tls_insecure_set = mock_tls_insecure_set
assert await async_setup_component( await mqtt_mock_entry_no_yaml_config()
hass,
mqtt.DOMAIN,
{mqtt.DOMAIN: config},
)
await hass.async_block_till_done() await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert calls assert calls
import certifi import certifi
expected_certificate = certifi.where() expected_certificate = certifi.where()
assert calls[0][0] == expected_certificate assert calls[0][0] == expected_certificate
# test if insecure is set # test if insecure is set
assert insecure_check["insecure"] == insecure_param 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( 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: ) -> None:
"""Test setup defaults for tls.""" """Test setup defaults for tls."""
calls = [] await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()
def mock_tls_set( assert (
certificate, certfile=None, keyfile=None, tls_version=None mqtt_client_mock.tls_set.mock_calls[0][2]["tls_version"]
) -> None: == ssl.PROTOCOL_TLS_CLIENT
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 hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert calls
assert calls[0][3] == ssl.PROTOCOL_TLS_CLIENT
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -3258,7 +3256,6 @@ async def test_unload_config_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock: MqttMockHAClient, mqtt_mock: MqttMockHAClient,
mqtt_client_mock: MqttMockPahoClient, mqtt_client_mock: MqttMockPahoClient,
tmp_path: Path,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test unloading the MQTT entry.""" """Test unloading the MQTT entry."""
@ -3272,15 +3269,11 @@ async def test_unload_config_entry(
mqtt_client_mock.reset_mock() mqtt_client_mock.reset_mock()
mqtt.publish(hass, "just_in_time", "published", qos=0, retain=False) mqtt.publish(hass, "just_in_time", "published", qos=0, retain=False)
new_yaml_config_file = tmp_path / "configuration.yaml" assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id)
new_yaml_config = yaml.dump({}) new_mqtt_config_entry = mqtt_config_entry
new_yaml_config_file.write_text(new_yaml_config) mqtt_client_mock.publish.assert_any_call("just_in_time", "published", 0, False)
with patch.object(hass_config, "YAML_CONFIG_FILE", new_yaml_config_file): assert new_mqtt_config_entry.state is ConfigEntryState.NOT_LOADED
assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id) await hass.async_block_till_done()
new_mqtt_config_entry = mqtt_config_entry
mqtt_client_mock.publish.assert_any_call("just_in_time", "published", 0, False)
assert new_mqtt_config_entry.state is ConfigEntryState.NOT_LOADED
await hass.async_block_till_done()
assert not hass.services.has_service(mqtt.DOMAIN, "dump") assert not hass.services.has_service(mqtt.DOMAIN, "dump")
assert not hass.services.has_service(mqtt.DOMAIN, "publish") assert not hass.services.has_service(mqtt.DOMAIN, "publish")
assert "No ACK from MQTT server" not in caplog.text assert "No ACK from MQTT server" not in caplog.text
@ -3318,131 +3311,108 @@ async def test_publish_or_subscribe_without_valid_config_entry(
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]) @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
async def test_reload_entry_with_new_config( @pytest.mark.parametrize(
hass: HomeAssistant, tmp_path: Path "hass_config",
[
{
"mqtt": {
"light": [
{"name": "test_new_modern", "command_topic": "test-topic_new"}
]
}
}
],
)
async def test_disabling_and_enabling_entry(
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test reloading the config entry with a new yaml config.""" """Test disabling and enabling the config entry."""
config_old = { await mqtt_mock_entry_no_yaml_config()
"mqtt": {"light": [{"name": "test_old1", "command_topic": "test-topic_old"}]} entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
} assert entry.state is ConfigEntryState.LOADED
config_yaml_new = { # Late discovery of a light
"mqtt": { config = '{"name": "abc", "command_topic": "test-topic"}'
"light": [{"name": "test_new_modern", "command_topic": "test-topic_new"}] async_fire_mqtt_message(hass, "homeassistant/light/abc/config", config)
},
} # Disable MQTT config entry
await help_test_setup_manual_entity_from_yaml(hass, config_old) await hass.config_entries.async_set_disabled_by(
assert hass.states.get("light.test_old1") is not None 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 = entry
assert new_mqtt_config_entry.state is ConfigEntryState.NOT_LOADED
# Enable the entry again
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 = entry
assert new_mqtt_config_entry.state is ConfigEntryState.LOADED
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 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
) -> 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
# 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
)
await hass.async_block_till_done()
await hass.async_block_till_done()
new_mqtt_config_entry = mqtt_config_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.async_block_till_done()
await hass.async_block_till_done()
new_mqtt_config_entry = mqtt_config_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]) @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
@pytest.mark.parametrize( @pytest.mark.parametrize(
("config", "unique"), ("hass_config", "unique"),
[ [
( (
[ {
{ mqtt.DOMAIN: {
"name": "test1", "light": [
"unique_id": "very_not_unique_deadbeef", {
"command_topic": "test-topic_unique", "name": "test1",
}, "unique_id": "very_not_unique_deadbeef",
{ "command_topic": "test-topic_unique",
"name": "test2", },
"unique_id": "very_not_unique_deadbeef", {
"command_topic": "test-topic_unique", "name": "test2",
}, "unique_id": "very_not_unique_deadbeef",
], "command_topic": "test-topic_unique",
},
]
}
},
False, False,
), ),
( (
[ {
{ mqtt.DOMAIN: {
"name": "test1", "light": [
"unique_id": "very_unique_deadbeef1", {
"command_topic": "test-topic_unique", "name": "test1",
}, "unique_id": "very_unique_deadbeef1",
{ "command_topic": "test-topic_unique",
"name": "test2", },
"unique_id": "very_unique_deadbeef2", {
"command_topic": "test-topic_unique", "name": "test2",
}, "unique_id": "very_unique_deadbeef2",
], "command_topic": "test-topic_unique",
},
]
}
},
True, True,
), ),
], ],
) )
async def test_setup_manual_items_with_unique_ids( async def test_setup_manual_items_with_unique_ids(
hass: HomeAssistant, hass: HomeAssistant,
tmp_path: Path,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
config: ConfigType, hass_config: ConfigType,
unique: bool, unique: bool,
) -> None: ) -> None:
"""Test setup manual items is generating unique id's.""" """Test setup manual items is generating unique id's."""
await help_test_setup_manual_entity_from_yaml( await help_test_setup_manual_entity_from_yaml(hass, hass_config)
hass, {mqtt.DOMAIN: {"light": config}}
)
assert hass.states.get("light.test1") is not None assert hass.states.get("light.test1") is not None
assert (hass.states.get("light.test2") is not None) == unique 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 # reload and assert again
caplog.clear() caplog.clear()
await help_test_entry_reload_with_new_config( await hass.services.async_call(
hass, tmp_path, {"mqtt": {"light": config}} "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.test1") is not None
assert (hass.states.get("light.test2") is not None) == unique assert (hass.states.get("light.test2") is not None) == unique
@ -3489,21 +3463,26 @@ async def test_remove_unknown_conf_entry_options(
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]) @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
@pytest.mark.parametrize(
"hass_config",
[
{
"mqtt": {
"light": [
{
"name": "test_manual",
"unique_id": "test_manual_unique_id123",
"command_topic": "test-topic_manual",
}
]
}
}
],
)
async def test_link_config_entry( async def test_link_config_entry(
hass: HomeAssistant, tmp_path: Path, caplog: pytest.LogCaptureFixture hass: HomeAssistant, hass_config: ConfigType, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test manual and dynamically setup entities are linked to the config entry.""" """Test manual and dynamically setup entities are linked to the config entry."""
config_manual = {
"mqtt": {
"light": [
{
"name": "test_manual",
"unique_id": "test_manual_unique_id123",
"command_topic": "test-topic_manual",
}
]
}
}
config_discovery = { config_discovery = {
"name": "test_discovery", "name": "test_discovery",
"unique_id": "test_discovery_unique456", "unique_id": "test_discovery_unique456",
@ -3511,7 +3490,7 @@ async def test_link_config_entry(
} }
# set up manual item # 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 # set up item through discovery
async_fire_mqtt_message( async_fire_mqtt_message(
@ -3540,7 +3519,10 @@ async def test_link_config_entry(
assert _check_entities() == 2 assert _check_entities() == 2
# reload entry and assert again # 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 # manual set up item should remain
assert _check_entities() == 1 assert _check_entities() == 1
# set up item through discovery # set up item through discovery
@ -3551,7 +3533,13 @@ async def test_link_config_entry(
assert _check_entities() == 2 assert _check_entities() == 2
# reload manual configured items and assert again # 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 assert _check_entities() == 2

View file

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

View file

@ -169,7 +169,6 @@ mqtt:
""" """
import copy import copy
from pathlib import Path
from unittest.mock import call, patch from unittest.mock import call, patch
import pytest import pytest
@ -229,7 +228,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {light.DOMAIN: {"name": "test", "command_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = light.DOMAIN domain = light.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -3310,12 +3305,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = light.DOMAIN domain = light.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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 brightness_scale: 99
""" """
import copy import copy
from pathlib import Path
from unittest.mock import call, patch from unittest.mock import call, patch
import pytest import pytest
@ -131,7 +130,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -2280,16 +2279,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = light.DOMAIN domain = light.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @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`. If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
""" """
import copy import copy
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -77,7 +76,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -1252,16 +1251,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = light.DOMAIN domain = light.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -1306,12 +1301,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = light.DOMAIN domain = light.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {lock.DOMAIN: {"name": "test", "command_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = lock.DOMAIN domain = lock.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -1022,12 +1018,11 @@ async def test_setup_manual_entity_from_yaml(
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = lock.DOMAIN domain = lock.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for mqtt number component."""
import json import json
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest 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.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 = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {number.DOMAIN: {"name": "test", "command_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = number.DOMAIN domain = number.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -1016,12 +1011,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = number.DOMAIN domain = number.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for the MQTT scene platform."""
import copy import copy
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -26,7 +25,7 @@ from .test_common import (
) )
from tests.common import mock_restore_cache from tests.common import mock_restore_cache
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -244,16 +243,12 @@ async def test_discovery_broken(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = scene.DOMAIN domain = scene.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None: 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = scene.DOMAIN domain = scene.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for mqtt select component."""
import copy import copy
import json import json
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -55,7 +54,7 @@ from .test_common import (
) )
from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -711,16 +710,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = select.DOMAIN domain = select.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -764,14 +759,13 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = select.DOMAIN domain = select.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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, async_fire_time_changed,
mock_restore_cache_with_extra_data, mock_restore_cache_with_extra_data,
) )
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {sensor.DOMAIN: {"name": "test", "state_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = sensor.DOMAIN domain = sensor.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_cleanup_triggers_and_restoring_state( 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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = sensor.DOMAIN domain = sensor.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for the MQTT siren platform."""
import copy import copy
from pathlib import Path
from typing import Any from typing import Any
from unittest.mock import patch from unittest.mock import patch
@ -53,7 +52,7 @@ from .test_common import (
) )
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {siren.DOMAIN: {"name": "test", "command_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = siren.DOMAIN domain = siren.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -1055,12 +1050,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = siren.DOMAIN domain = siren.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for the State vacuum Mqtt platform."""
from copy import deepcopy from copy import deepcopy
import json import json
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -62,7 +61,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.components.vacuum import common from tests.components.vacuum import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
COMMAND_TOPIC = "vacuum/command" COMMAND_TOPIC = "vacuum/command"
SEND_COMMAND_TOPIC = "vacuum/send_command" SEND_COMMAND_TOPIC = "vacuum/send_command"
@ -718,16 +717,12 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = vacuum.DOMAIN domain = vacuum.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(

View file

@ -1,6 +1,5 @@
"""The tests for the MQTT switch platform.""" """The tests for the MQTT switch platform."""
import copy import copy
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -49,7 +48,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.switch import common from tests.components.switch import common
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: {switch.DOMAIN: {"name": "test", "command_topic": "test-topic"}} 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( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None: ) -> None:
"""Test reloading the MQTT platform.""" """Test reloading the MQTT platform."""
domain = switch.DOMAIN domain = switch.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_reloadable( await help_test_reloadable(hass, mqtt_client_mock, domain, config)
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -731,12 +726,11 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the config entry.""" """Test unloading the config entry."""
domain = switch.DOMAIN domain = switch.DOMAIN
config = DEFAULT_CONFIG config = DEFAULT_CONFIG
await help_test_unload_config_entry_with_platform( 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.""" """The tests for MQTT tag scanner."""
import copy import copy
import json import json
from pathlib import Path
from unittest.mock import ANY, patch from unittest.mock import ANY, patch
import pytest import pytest
@ -893,7 +892,6 @@ async def test_unload_entry(
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock: MqttMockHAClient, mqtt_mock: MqttMockHAClient,
tag_mock, tag_mock,
tmp_path: Path,
) -> None: ) -> None:
"""Test unloading the MQTT entry.""" """Test unloading the MQTT entry."""
@ -910,7 +908,7 @@ async def test_unload_entry(
tag_mock.reset_mock() 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() await hass.async_block_till_done()
# Fake tag scan, should not be processed # Fake tag scan, should not be processed

View file

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