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:
Jan Bouwhuis
2023-02-09 09:48:22 +01:00
committed by GitHub
parent b0cbe5cb69
commit df76e31cdf
29 changed files with 1749 additions and 984 deletions

View File

@ -80,6 +80,7 @@ light:
"""
import copy
import json
from pathlib import Path
from unittest.mock import call, patch
import pytest
@ -130,6 +131,7 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
@ -176,7 +178,7 @@ async def test_fail_setup_if_no_command_topic(hass: HomeAssistant, caplog) -> No
@pytest.mark.parametrize("deprecated", ("color_temp", "hs", "rgb", "xy"))
async def test_fail_setup_if_color_mode_deprecated(
hass: HomeAssistant, caplog, deprecated
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, deprecated
) -> None:
"""Test if setup fails if color mode is combined with deprecated config keys."""
supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"]
@ -212,7 +214,7 @@ async def test_fail_setup_if_color_mode_deprecated(
],
)
async def test_fail_setup_if_color_modes_invalid(
hass: HomeAssistant, caplog, supported_color_modes, error
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, supported_color_modes, error
) -> None:
"""Test if setup fails if supported color modes is invalid."""
config = {
@ -234,7 +236,7 @@ async def test_fail_setup_if_color_modes_invalid(
async def test_legacy_rgb_light(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test legacy RGB light flags expected features and color modes."""
assert await async_setup_component(
@ -262,7 +264,7 @@ async def test_legacy_rgb_light(
async def test_no_color_brightness_color_temp_if_no_topics(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test for no RGB, brightness, color temp, effector XY."""
assert await async_setup_component(
@ -316,7 +318,7 @@ async def test_no_color_brightness_color_temp_if_no_topics(
async def test_controlling_state_via_topic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the controlling of the state via topic."""
assert await async_setup_component(
@ -460,7 +462,9 @@ async def test_controlling_state_via_topic(
async def test_controlling_state_via_topic2(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling of the state via topic for a light supporting color mode."""
supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "white", "xy"]
@ -630,7 +634,7 @@ async def test_controlling_state_via_topic2(
async def test_sending_mqtt_commands_and_optimistic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the sending of command in optimistic mode."""
fake_state = State(
@ -777,7 +781,7 @@ async def test_sending_mqtt_commands_and_optimistic(
async def test_sending_mqtt_commands_and_optimistic2(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the sending of command in optimistic mode for a light supporting color mode."""
supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "white", "xy"]
@ -1009,7 +1013,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
async def test_sending_hs_color(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with hs color sends hs color parameters."""
assert await async_setup_component(
@ -1072,7 +1076,7 @@ async def test_sending_hs_color(
async def test_sending_rgb_color_no_brightness(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(
@ -1129,7 +1133,7 @@ async def test_sending_rgb_color_no_brightness(
async def test_sending_rgb_color_no_brightness2(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with hs color sends rgb color parameters."""
supported_color_modes = ["rgb", "rgbw", "rgbww"]
@ -1210,7 +1214,7 @@ async def test_sending_rgb_color_no_brightness2(
async def test_sending_rgb_color_with_brightness(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(
@ -1278,7 +1282,7 @@ async def test_sending_rgb_color_with_brightness(
async def test_sending_rgb_color_with_scaled_brightness(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(
@ -1347,7 +1351,7 @@ async def test_sending_rgb_color_with_scaled_brightness(
async def test_sending_scaled_white(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with scaled white."""
assert await async_setup_component(
@ -1394,7 +1398,7 @@ async def test_sending_scaled_white(
async def test_sending_xy_color(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test light.turn_on with hs color sends xy color parameters."""
assert await async_setup_component(
@ -1520,7 +1524,7 @@ async def test_effect(hass: HomeAssistant, mqtt_mock_entry_with_yaml_config) ->
async def test_flash_short_and_long(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test for flash length being sent when included."""
assert await async_setup_component(
@ -1585,7 +1589,7 @@ async def test_flash_short_and_long(
async def test_transition(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test for transition time being sent when included."""
assert await async_setup_component(
@ -1635,7 +1639,7 @@ async def test_transition(
async def test_brightness_scale(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test for brightness scaling."""
assert await async_setup_component(
@ -1680,7 +1684,7 @@ async def test_brightness_scale(
async def test_white_scale(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test for white scaling."""
assert await async_setup_component(
@ -1741,7 +1745,7 @@ async def test_white_scale(
async def test_invalid_values(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test that invalid color/brightness/etc. values are ignored."""
assert await async_setup_component(
@ -1870,7 +1874,7 @@ async def test_invalid_values(
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(
@ -1879,7 +1883,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(
@ -1888,7 +1892,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(
@ -1897,7 +1901,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(
@ -1906,7 +1910,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(
@ -1915,7 +1919,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(
@ -1928,7 +1932,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(
@ -1937,7 +1941,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(
@ -1950,7 +1956,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(
@ -1963,7 +1971,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(
@ -2003,7 +2013,9 @@ async def test_unique_id(hass: HomeAssistant, mqtt_mock_entry_with_yaml_config)
async def test_discovery_removal(
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 mqtt_json lights."""
data = '{ "name": "test", "schema": "json", "command_topic": "test_topic" }'
@ -2017,7 +2029,9 @@ async def test_discovery_removal(
async def test_discovery_update_light(
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 light."""
config1 = {
@ -2043,7 +2057,9 @@ async def test_discovery_update_light(
async def test_discovery_update_unchanged_light(
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 light."""
data1 = (
@ -2067,7 +2083,9 @@ async def test_discovery_update_unchanged_light(
@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" }'
@ -2088,7 +2106,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 light device registry integration."""
await help_test_entity_device_info_with_connection(
@ -2100,7 +2118,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 light device registry integration."""
await help_test_entity_device_info_with_identifier(
@ -2112,7 +2130,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(
@ -2124,7 +2142,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(
@ -2136,7 +2154,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(
@ -2145,7 +2163,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(
@ -2154,7 +2172,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(
@ -2169,7 +2187,7 @@ async def test_entity_debug_info_message(
async def test_max_mireds(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test setting min_mireds and max_mireds."""
config = {
@ -2216,8 +2234,8 @@ async def test_max_mireds(
)
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,
@ -2249,7 +2267,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 = light.DOMAIN
@ -2273,8 +2294,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,