1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add hass and None return type on MQTT platform tests (#87713)

Add hass and `None` return type on MQTT tests
This commit is contained in:
Jan Bouwhuis
2023-02-08 18:08:03 +01:00
committed by GitHub
parent ba85fdcd61
commit 1a414f1433
31 changed files with 2673 additions and 1483 deletions

View File

@@ -28,7 +28,7 @@ from homeassistant.const import (
Platform,
UnitOfTemperature,
)
import homeassistant.core as ha
from homeassistant.core import HomeAssistant, State
from homeassistant.setup import async_setup_component
from .test_common import (
@@ -75,7 +75,9 @@ def number_platform_only():
yield
async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
async def test_run_number_setup(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that it fetches the given payload."""
topic = "test/number"
await async_setup_component(
@@ -125,7 +127,9 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "°C"
async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
async def test_value_template(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that it fetches the given payload with a template."""
topic = "test/number"
await async_setup_component(
@@ -167,7 +171,9 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
assert state.state == "unknown"
async def test_restore_native_value(hass, mqtt_mock_entry_with_yaml_config):
async def test_restore_native_value(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that the stored native_value is restored."""
topic = "test/number"
@@ -180,7 +186,7 @@ async def test_restore_native_value(hass, mqtt_mock_entry_with_yaml_config):
}
mock_restore_cache_with_extra_data(
hass, ((ha.State("number.test_number", "abc"), RESTORE_DATA),)
hass, ((State("number.test_number", "abc"), RESTORE_DATA),)
)
assert await async_setup_component(
hass,
@@ -204,7 +210,9 @@ async def test_restore_native_value(hass, mqtt_mock_entry_with_yaml_config):
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_run_number_service_optimistic(hass, mqtt_mock_entry_with_yaml_config):
async def test_run_number_service_optimistic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that set_value service works in optimistic mode."""
topic = "test/number"
@@ -217,7 +225,7 @@ async def test_run_number_service_optimistic(hass, mqtt_mock_entry_with_yaml_con
}
mock_restore_cache_with_extra_data(
hass, ((ha.State("number.test_number", "abc"), RESTORE_DATA),)
hass, ((State("number.test_number", "abc"), RESTORE_DATA),)
)
assert await async_setup_component(
hass,
@@ -279,8 +287,8 @@ async def test_run_number_service_optimistic(hass, mqtt_mock_entry_with_yaml_con
async def test_run_number_service_optimistic_with_command_template(
hass, mqtt_mock_entry_with_yaml_config
):
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that set_value service works in optimistic mode and with a command_template."""
topic = "test/number"
@@ -293,7 +301,7 @@ async def test_run_number_service_optimistic_with_command_template(
}
mock_restore_cache_with_extra_data(
hass, ((ha.State("number.test_number", "abc"), RESTORE_DATA),)
hass, ((State("number.test_number", "abc"), RESTORE_DATA),)
)
assert await async_setup_component(
hass,
@@ -357,7 +365,9 @@ async def test_run_number_service_optimistic_with_command_template(
assert state.state == "42.1"
async def test_run_number_service(hass, mqtt_mock_entry_with_yaml_config):
async def test_run_number_service(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that set_value service works in non optimistic mode."""
cmd_topic = "test/number/set"
state_topic = "test/number"
@@ -394,8 +404,8 @@ async def test_run_number_service(hass, mqtt_mock_entry_with_yaml_config):
async def test_run_number_service_with_command_template(
hass, mqtt_mock_entry_with_yaml_config
):
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test that set_value service works in non optimistic mode and with a command_template."""
cmd_topic = "test/number/set"
state_topic = "test/number"
@@ -435,29 +445,35 @@ async def test_run_number_service_with_command_template(
async def test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config
):
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
async def test_availability_without_topic(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
async def test_default_availability_payload(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
async def test_custom_availability_payload(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
@@ -465,8 +481,8 @@ async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_confi
async def test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config
):
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
@@ -474,8 +490,8 @@ async def test_setting_attribute_via_mqtt_json_message(
async def test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_no_yaml_config
):
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass,
@@ -486,7 +502,9 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
)
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
async def test_setting_attribute_with_template(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
@@ -494,8 +512,8 @@ async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_c
async def test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog
) -> None:
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass,
@@ -507,8 +525,8 @@ async def test_update_with_json_attrs_not_dict(
async def test_update_with_json_attrs_bad_json(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog
) -> None:
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_json(
hass,
@@ -519,7 +537,9 @@ async def test_update_with_json_attrs_bad_json(
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_discovery_update_attr(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None:
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass,
@@ -530,7 +550,7 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo
)
async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
async def test_unique_id(hass: HomeAssistant, mqtt_mock_entry_with_yaml_config) -> None:
"""Test unique id option only creates one number per unique_id."""
config = {
mqtt.DOMAIN: {
@@ -555,7 +575,9 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
)
async def test_discovery_removal_number(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_discovery_removal_number(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None:
"""Test removal of discovered number."""
data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][number.DOMAIN])
await help_test_discovery_removal(
@@ -563,7 +585,9 @@ async def test_discovery_removal_number(hass, mqtt_mock_entry_no_yaml_config, ca
)
async def test_discovery_update_number(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_discovery_update_number(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None:
"""Test update of discovered number."""
config1 = {
"name": "Beer",
@@ -582,8 +606,8 @@ async def test_discovery_update_number(hass, mqtt_mock_entry_no_yaml_config, cap
async def test_discovery_update_unchanged_number(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None:
"""Test update of discovered number."""
data1 = (
'{ "name": "Beer", "state_topic": "test-topic", "command_topic": "test-topic"}'
@@ -602,7 +626,9 @@ async def test_discovery_update_unchanged_number(
@pytest.mark.no_fail_on_log_exception
async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_discovery_broken(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None:
"""Test handling of bad discovery message."""
data1 = '{ "name": "Beer" }'
data2 = (
@@ -614,49 +640,63 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
)
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_device_info_with_connection(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test MQTT number device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_device_info_with_identifier(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test MQTT number device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_device_info_update(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_device_info_remove(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
async def test_entity_id_update_subscriptions(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_id_update_discovery_update(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_debug_info_message(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config
) -> None:
"""Test MQTT debug info."""
await help_test_entity_debug_info_message(
hass,
@@ -670,7 +710,9 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
)
async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config):
async def test_min_max_step_attributes(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test min/max/step attributes."""
topic = "test/number"
await async_setup_component(
@@ -698,7 +740,7 @@ async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config):
assert state.attributes.get(ATTR_STEP) == 20
async def test_invalid_min_max_attributes(hass, caplog):
async def test_invalid_min_max_attributes(hass: HomeAssistant, caplog) -> None:
"""Test invalid min/max attributes."""
topic = "test/number"
assert not await async_setup_component(
@@ -720,7 +762,9 @@ async def test_invalid_min_max_attributes(hass, caplog):
assert f"'{CONF_MAX}' must be > '{CONF_MIN}'" in caplog.text
async def test_default_mode(hass, mqtt_mock_entry_with_yaml_config):
async def test_default_mode(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test default mode."""
topic = "test/number"
await async_setup_component(
@@ -744,7 +788,9 @@ async def test_default_mode(hass, mqtt_mock_entry_with_yaml_config):
@pytest.mark.parametrize("mode", ("auto", "box", "slider"))
async def test_mode(hass, mqtt_mock_entry_with_yaml_config, mode):
async def test_mode(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, mode
) -> None:
"""Test mode."""
topic = "test/number"
await async_setup_component(
@@ -769,7 +815,7 @@ async def test_mode(hass, mqtt_mock_entry_with_yaml_config, mode):
@pytest.mark.parametrize("mode,valid", [("bleh", False), ("auto", True)])
async def test_invalid_mode(hass, mode, valid):
async def test_invalid_mode(hass: HomeAssistant, mode, valid) -> None:
"""Test invalid mode."""
topic = "test/number"
assert (
@@ -792,8 +838,8 @@ async def test_invalid_mode(hass, mode, valid):
async def test_mqtt_payload_not_a_number_warning(
hass, caplog, mqtt_mock_entry_with_yaml_config
):
hass: HomeAssistant, caplog, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test warning for MQTT payload which is not a number."""
topic = "test/number"
assert await async_setup_component(
@@ -820,8 +866,8 @@ async def test_mqtt_payload_not_a_number_warning(
async def test_mqtt_payload_out_of_range_error(
hass, caplog, mqtt_mock_entry_with_yaml_config
):
hass: HomeAssistant, caplog, mqtt_mock_entry_with_yaml_config
) -> None:
"""Test error when MQTT payload is out of min/max range."""
topic = "test/number"
await async_setup_component(
@@ -864,7 +910,7 @@ async def test_mqtt_payload_out_of_range_error(
],
)
async def test_publishing_with_custom_encoding(
hass,
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config,
caplog,
service,
@@ -872,7 +918,7 @@ async def test_publishing_with_custom_encoding(
parameters,
payload,
template,
):
) -> None:
"""Test publishing MQTT payload with different encoding."""
domain = NUMBER_DOMAIN
config = DEFAULT_CONFIG
@@ -891,7 +937,9 @@ async def test_publishing_with_custom_encoding(
)
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
async def test_reloadable(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, caplog, tmp_path
) -> None:
"""Test reloading the MQTT platform."""
domain = number.DOMAIN
config = DEFAULT_CONFIG
@@ -908,14 +956,14 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
],
)
async def test_encoding_subscribable_topics(
hass,
hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config,
caplog,
topic,
value,
attribute,
attribute_value,
):
) -> None:
"""Test handling of incoming encoded payload."""
await help_test_encoding_subscribable_topics(
hass,
@@ -930,14 +978,16 @@ async def test_encoding_subscribable_topics(
)
async def test_setup_manual_entity_from_yaml(hass):
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant) -> None:
"""Test setup manual configured MQTT entity."""
platform = number.DOMAIN
await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG)
assert hass.states.get(f"{platform}.test")
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
async def test_unload_entry(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config, tmp_path
) -> None:
"""Test unloading the config entry."""
domain = number.DOMAIN
config = DEFAULT_CONFIG