mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Apply MQTT fixture types on platform tests (#87722)
* Apply MQTT fixture types on platform tests * Add caplog type hint * Add hass_ws_client type hint * Add tmp_path type hint * Add hass_client_no_auth type hint
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""The tests for mqtt number component."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
@@ -62,6 +63,7 @@ from .test_common import (
|
||||
)
|
||||
|
||||
from tests.common import async_fire_mqtt_message, mock_restore_cache_with_extra_data
|
||||
from tests.typing import MqttMockHAClientGenerator
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
mqtt.DOMAIN: {number.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
|
||||
@@ -76,7 +78,7 @@ def number_platform_only():
|
||||
|
||||
|
||||
async def test_run_number_setup(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that it fetches the given payload."""
|
||||
topic = "test/number"
|
||||
@@ -128,7 +130,7 @@ async def test_run_number_setup(
|
||||
|
||||
|
||||
async def test_value_template(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that it fetches the given payload with a template."""
|
||||
topic = "test/number"
|
||||
@@ -172,7 +174,7 @@ async def test_value_template(
|
||||
|
||||
|
||||
async def test_restore_native_value(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that the stored native_value is restored."""
|
||||
topic = "test/number"
|
||||
@@ -211,7 +213,7 @@ async def test_restore_native_value(
|
||||
|
||||
|
||||
async def test_run_number_service_optimistic(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that set_value service works in optimistic mode."""
|
||||
topic = "test/number"
|
||||
@@ -287,7 +289,7 @@ async def test_run_number_service_optimistic(
|
||||
|
||||
|
||||
async def test_run_number_service_optimistic_with_command_template(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that set_value service works in optimistic mode and with a command_template."""
|
||||
topic = "test/number"
|
||||
@@ -366,7 +368,7 @@ async def test_run_number_service_optimistic_with_command_template(
|
||||
|
||||
|
||||
async def test_run_number_service(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that set_value service works in non optimistic mode."""
|
||||
cmd_topic = "test/number/set"
|
||||
@@ -404,7 +406,7 @@ async def test_run_number_service(
|
||||
|
||||
|
||||
async def test_run_number_service_with_command_template(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test that set_value service works in non optimistic mode and with a command_template."""
|
||||
cmd_topic = "test/number/set"
|
||||
@@ -445,7 +447,7 @@ async def test_run_number_service_with_command_template(
|
||||
|
||||
|
||||
async def test_availability_when_connection_lost(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test availability after MQTT disconnection."""
|
||||
await help_test_availability_when_connection_lost(
|
||||
@@ -454,7 +456,7 @@ async def test_availability_when_connection_lost(
|
||||
|
||||
|
||||
async def test_availability_without_topic(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test availability without defined availability topic."""
|
||||
await help_test_availability_without_topic(
|
||||
@@ -463,7 +465,7 @@ async def test_availability_without_topic(
|
||||
|
||||
|
||||
async def test_default_availability_payload(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test availability by default payload with defined topic."""
|
||||
await help_test_default_availability_payload(
|
||||
@@ -472,7 +474,7 @@ async def test_default_availability_payload(
|
||||
|
||||
|
||||
async def test_custom_availability_payload(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
await help_test_custom_availability_payload(
|
||||
@@ -481,7 +483,7 @@ async def test_custom_availability_payload(
|
||||
|
||||
|
||||
async def test_setting_attribute_via_mqtt_json_message(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
await help_test_setting_attribute_via_mqtt_json_message(
|
||||
@@ -490,7 +492,7 @@ async def test_setting_attribute_via_mqtt_json_message(
|
||||
|
||||
|
||||
async def test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
await help_test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
@@ -503,7 +505,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
|
||||
|
||||
|
||||
async def test_setting_attribute_with_template(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
await help_test_setting_attribute_with_template(
|
||||
@@ -512,7 +514,9 @@ async def test_setting_attribute_with_template(
|
||||
|
||||
|
||||
async def test_update_with_json_attrs_not_dict(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_not_dict(
|
||||
@@ -525,7 +529,9 @@ async def test_update_with_json_attrs_not_dict(
|
||||
|
||||
|
||||
async def test_update_with_json_attrs_bad_json(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_bad_json(
|
||||
@@ -538,7 +544,9 @@ async def test_update_with_json_attrs_bad_json(
|
||||
|
||||
|
||||
async def test_discovery_update_attr(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test update of discovered MQTTAttributes."""
|
||||
await help_test_discovery_update_attr(
|
||||
@@ -576,7 +584,9 @@ async def test_unique_id(hass: HomeAssistant, mqtt_mock_entry_with_yaml_config)
|
||||
|
||||
|
||||
async def test_discovery_removal_number(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test removal of discovered number."""
|
||||
data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][number.DOMAIN])
|
||||
@@ -586,7 +596,9 @@ async def test_discovery_removal_number(
|
||||
|
||||
|
||||
async def test_discovery_update_number(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test update of discovered number."""
|
||||
config1 = {
|
||||
@@ -606,7 +618,9 @@ async def test_discovery_update_number(
|
||||
|
||||
|
||||
async def test_discovery_update_unchanged_number(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test update of discovered number."""
|
||||
data1 = (
|
||||
@@ -627,7 +641,9 @@ async def test_discovery_update_unchanged_number(
|
||||
|
||||
@pytest.mark.no_fail_on_log_exception
|
||||
async def test_discovery_broken(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test handling of bad discovery message."""
|
||||
data1 = '{ "name": "Beer" }'
|
||||
@@ -641,7 +657,7 @@ async def test_discovery_broken(
|
||||
|
||||
|
||||
async def test_entity_device_info_with_connection(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test MQTT number device registry integration."""
|
||||
await help_test_entity_device_info_with_connection(
|
||||
@@ -650,7 +666,7 @@ async def test_entity_device_info_with_connection(
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test MQTT number device registry integration."""
|
||||
await help_test_entity_device_info_with_identifier(
|
||||
@@ -659,7 +675,7 @@ async def test_entity_device_info_with_identifier(
|
||||
|
||||
|
||||
async def test_entity_device_info_update(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test device registry update."""
|
||||
await help_test_entity_device_info_update(
|
||||
@@ -668,7 +684,7 @@ async def test_entity_device_info_update(
|
||||
|
||||
|
||||
async def test_entity_device_info_remove(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test device registry remove."""
|
||||
await help_test_entity_device_info_remove(
|
||||
@@ -677,7 +693,7 @@ async def test_entity_device_info_remove(
|
||||
|
||||
|
||||
async def test_entity_id_update_subscriptions(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||
await help_test_entity_id_update_subscriptions(
|
||||
@@ -686,7 +702,7 @@ async def test_entity_id_update_subscriptions(
|
||||
|
||||
|
||||
async def test_entity_id_update_discovery_update(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test MQTT discovery update when entity_id is updated."""
|
||||
await help_test_entity_id_update_discovery_update(
|
||||
@@ -695,7 +711,7 @@ async def test_entity_id_update_discovery_update(
|
||||
|
||||
|
||||
async def test_entity_debug_info_message(
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test MQTT debug info."""
|
||||
await help_test_entity_debug_info_message(
|
||||
@@ -711,7 +727,7 @@ async def test_entity_debug_info_message(
|
||||
|
||||
|
||||
async def test_min_max_step_attributes(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test min/max/step attributes."""
|
||||
topic = "test/number"
|
||||
@@ -763,7 +779,7 @@ async def test_invalid_min_max_attributes(hass: HomeAssistant, caplog) -> None:
|
||||
|
||||
|
||||
async def test_default_mode(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test default mode."""
|
||||
topic = "test/number"
|
||||
@@ -789,7 +805,9 @@ async def test_default_mode(
|
||||
|
||||
@pytest.mark.parametrize("mode", ("auto", "box", "slider"))
|
||||
async def test_mode(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, mode
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
mode,
|
||||
) -> None:
|
||||
"""Test mode."""
|
||||
topic = "test/number"
|
||||
@@ -838,7 +856,9 @@ async def test_invalid_mode(hass: HomeAssistant, mode, valid) -> None:
|
||||
|
||||
|
||||
async def test_mqtt_payload_not_a_number_warning(
|
||||
hass: HomeAssistant, caplog, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
) -> None:
|
||||
"""Test warning for MQTT payload which is not a number."""
|
||||
topic = "test/number"
|
||||
@@ -866,7 +886,9 @@ async def test_mqtt_payload_not_a_number_warning(
|
||||
|
||||
|
||||
async def test_mqtt_payload_out_of_range_error(
|
||||
hass: HomeAssistant, caplog, mqtt_mock_entry_with_yaml_config
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
) -> None:
|
||||
"""Test error when MQTT payload is out of min/max range."""
|
||||
topic = "test/number"
|
||||
@@ -911,8 +933,8 @@ async def test_mqtt_payload_out_of_range_error(
|
||||
)
|
||||
async def test_publishing_with_custom_encoding(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
service,
|
||||
topic,
|
||||
parameters,
|
||||
@@ -938,7 +960,10 @@ async def test_publishing_with_custom_encoding(
|
||||
|
||||
|
||||
async def test_reloadable(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog, tmp_path
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test reloading the MQTT platform."""
|
||||
domain = number.DOMAIN
|
||||
@@ -957,8 +982,8 @@ async def test_reloadable(
|
||||
)
|
||||
async def test_encoding_subscribable_topics(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config,
|
||||
caplog,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
topic,
|
||||
value,
|
||||
attribute,
|
||||
@@ -986,7 +1011,9 @@ async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
|
||||
|
||||
|
||||
async def test_unload_entry(
|
||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, tmp_path
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test unloading the config entry."""
|
||||
domain = number.DOMAIN
|
||||
|
||||
Reference in New Issue
Block a user