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

Update MQTT tests to use the config entry setup (#72373)

* New testframework and tests for fan platform

* Merge test_common_new to test_common

* Add alarm_control_panel

* Add binary_sensor

* Add button

* Add camera

* Add climate

* Add config_flow

* Add cover

* Add device_tracker_disovery

* Add device_trigger

* Add diagnostics

* Add discovery

* Add humidifier

* Add init

* Add lecacy_vacuum

* Add light_json

* Add light_template

* Add light

* Add lock

* Add number

* Add scene

* Add select

* Add sensor

* Add siren

* Add state_vacuum

* Add subscription

* Add switch

* Add tag

* Add trigger

* Add missed tests

* Add another missed test

* Add device_tracker

* Remove commented out code

* Correct tests according comments

* Improve mqtt_mock_entry and recover tests

* Split fixtures with and without yaml setup

* Update fixtures manual_mqtt

* Update fixtures mqtt_json

* Fix test tasmota

* Update fixture mqtt_room

* Revert fixture changes, improve test

* re-add test
This commit is contained in:
Jan Bouwhuis
2022-06-02 14:24:46 +02:00
committed by GitHub
parent 756988fe20
commit 52561ce076
36 changed files with 3612 additions and 1692 deletions

View File

@@ -46,10 +46,13 @@ DEFAULT_CONFIG_DEVICE_INFO_MAC = {
_SENTINEL = object()
async def help_test_availability_when_connection_lost(hass, mqtt_mock, domain, config):
async def help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, domain, config
):
"""Test availability after MQTT disconnection."""
assert await async_setup_component(hass, domain, config)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state != STATE_UNAVAILABLE
@@ -62,11 +65,14 @@ async def help_test_availability_when_connection_lost(hass, mqtt_mock, domain, c
assert state.state == STATE_UNAVAILABLE
async def help_test_availability_without_topic(hass, mqtt_mock, domain, config):
async def help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, domain, config
):
"""Test availability without defined availability topic."""
assert "availability_topic" not in config[domain]
assert await async_setup_component(hass, domain, config)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state != STATE_UNAVAILABLE
@@ -74,7 +80,7 @@ async def help_test_availability_without_topic(hass, mqtt_mock, domain, config):
async def help_test_default_availability_payload(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
domain,
config,
no_assumed_state=False,
@@ -94,6 +100,7 @@ async def help_test_default_availability_payload(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
@@ -124,7 +131,7 @@ async def help_test_default_availability_payload(
async def help_test_default_availability_list_payload(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
domain,
config,
no_assumed_state=False,
@@ -147,6 +154,7 @@ async def help_test_default_availability_list_payload(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
@@ -189,7 +197,7 @@ async def help_test_default_availability_list_payload(
async def help_test_default_availability_list_payload_all(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
domain,
config,
no_assumed_state=False,
@@ -213,6 +221,7 @@ async def help_test_default_availability_list_payload_all(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
@@ -256,7 +265,7 @@ async def help_test_default_availability_list_payload_all(
async def help_test_default_availability_list_payload_any(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
domain,
config,
no_assumed_state=False,
@@ -280,6 +289,7 @@ async def help_test_default_availability_list_payload_any(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
@@ -318,7 +328,7 @@ async def help_test_default_availability_list_payload_any(
async def help_test_default_availability_list_single(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
domain,
config,
@@ -342,6 +352,7 @@ async def help_test_default_availability_list_single(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state is None
@@ -353,7 +364,7 @@ async def help_test_default_availability_list_single(
async def help_test_custom_availability_payload(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
domain,
config,
no_assumed_state=False,
@@ -375,6 +386,7 @@ async def help_test_custom_availability_payload(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
@@ -405,7 +417,7 @@ async def help_test_custom_availability_payload(
async def help_test_discovery_update_availability(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
domain,
config,
no_assumed_state=False,
@@ -416,6 +428,7 @@ async def help_test_discovery_update_availability(
This is a test helper for the MQTTAvailability mixin.
"""
await mqtt_mock_entry_no_yaml_config()
# Add availability settings to config
config1 = copy.deepcopy(config)
config1[domain]["availability_topic"] = "availability-topic1"
@@ -484,7 +497,7 @@ async def help_test_discovery_update_availability(
async def help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock, domain, config
hass, mqtt_mock_entry_with_yaml_config, domain, config
):
"""Test the setting of attribute via MQTT with JSON payload.
@@ -499,6 +512,7 @@ async def help_test_setting_attribute_via_mqtt_json_message(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "attr-topic", '{ "val": "100" }')
state = hass.states.get(f"{domain}.test")
@@ -507,12 +521,13 @@ async def help_test_setting_attribute_via_mqtt_json_message(
async def help_test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock, domain, config, extra_blocked_attributes
hass, mqtt_mock_entry_no_yaml_config, domain, config, extra_blocked_attributes
):
"""Test the setting of blocked attribute via MQTT with JSON payload.
This is a test helper for the MqttAttributes mixin.
"""
await mqtt_mock_entry_no_yaml_config()
extra_blocked_attributes = extra_blocked_attributes or []
# Add JSON attributes settings to config
@@ -534,7 +549,9 @@ async def help_test_setting_blocked_attribute_via_mqtt_json_message(
assert state.attributes.get(attr) != val
async def help_test_setting_attribute_with_template(hass, mqtt_mock, domain, config):
async def help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, domain, config
):
"""Test the setting of attribute via MQTT with JSON payload.
This is a test helper for the MqttAttributes mixin.
@@ -549,6 +566,7 @@ async def help_test_setting_attribute_with_template(hass, mqtt_mock, domain, con
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(
hass, "attr-topic", json.dumps({"Timer1": {"Arm": 0, "Time": "22:18"}})
@@ -560,7 +578,7 @@ async def help_test_setting_attribute_with_template(hass, mqtt_mock, domain, con
async def help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock, caplog, domain, config
hass, mqtt_mock_entry_with_yaml_config, caplog, domain, config
):
"""Test attributes get extracted from a JSON result.
@@ -575,6 +593,7 @@ async def help_test_update_with_json_attrs_not_dict(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "attr-topic", '[ "list", "of", "things"]')
state = hass.states.get(f"{domain}.test")
@@ -584,7 +603,7 @@ async def help_test_update_with_json_attrs_not_dict(
async def help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock, caplog, domain, config
hass, mqtt_mock_entry_with_yaml_config, caplog, domain, config
):
"""Test JSON validation of attributes.
@@ -599,6 +618,7 @@ async def help_test_update_with_json_attrs_bad_JSON(
config,
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "attr-topic", "This is not JSON")
@@ -607,11 +627,14 @@ async def help_test_update_with_json_attrs_bad_JSON(
assert "Erroneous JSON: This is not JSON" in caplog.text
async def help_test_discovery_update_attr(hass, mqtt_mock, caplog, domain, config):
async def help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, domain, config
):
"""Test update of discovered MQTTAttributes.
This is a test helper for the MqttAttributes mixin.
"""
await mqtt_mock_entry_no_yaml_config()
# Add JSON attributes settings to config
config1 = copy.deepcopy(config)
config1[domain]["json_attributes_topic"] = "attr-topic1"
@@ -641,18 +664,22 @@ async def help_test_discovery_update_attr(hass, mqtt_mock, caplog, domain, confi
assert state.attributes.get("val") == "75"
async def help_test_unique_id(hass, mqtt_mock, domain, config):
async def help_test_unique_id(hass, mqtt_mock_entry_with_yaml_config, domain, config):
"""Test unique id option only creates one entity per unique_id."""
assert await async_setup_component(hass, domain, config)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert len(hass.states.async_entity_ids(domain)) == 1
async def help_test_discovery_removal(hass, mqtt_mock, caplog, domain, data):
async def help_test_discovery_removal(
hass, mqtt_mock_entry_no_yaml_config, caplog, domain, data
):
"""Test removal of discovered component.
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done()
@@ -669,7 +696,7 @@ async def help_test_discovery_removal(hass, mqtt_mock, caplog, domain, data):
async def help_test_discovery_update(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
caplog,
domain,
discovery_config1,
@@ -681,6 +708,7 @@ async def help_test_discovery_update(
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
await mqtt_mock_entry_no_yaml_config()
# Add some future configuration to the configurations
config1 = copy.deepcopy(discovery_config1)
config1["some_future_option_1"] = "future_option_1"
@@ -730,12 +758,13 @@ async def help_test_discovery_update(
async def help_test_discovery_update_unchanged(
hass, mqtt_mock, caplog, domain, data1, discovery_update
hass, mqtt_mock_entry_no_yaml_config, caplog, domain, data1, discovery_update
):
"""Test update of discovered component without changes.
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1)
await hass.async_block_till_done()
@@ -749,8 +778,11 @@ async def help_test_discovery_update_unchanged(
assert not discovery_update.called
async def help_test_discovery_broken(hass, mqtt_mock, caplog, domain, data1, data2):
async def help_test_discovery_broken(
hass, mqtt_mock_entry_no_yaml_config, caplog, domain, data1, data2
):
"""Test handling of bad discovery message."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1)
await hass.async_block_till_done()
@@ -769,7 +801,7 @@ async def help_test_discovery_broken(hass, mqtt_mock, caplog, domain, data1, dat
async def help_test_encoding_subscribable_topics(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
domain,
config,
@@ -849,6 +881,7 @@ async def help_test_encoding_subscribable_topics(
hass, domain, {domain: [config1, config2, config3]}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
expected_result = attribute_value or value
@@ -899,11 +932,14 @@ async def help_test_encoding_subscribable_topics(
pass
async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, config):
async def help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test device registry integration.
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -926,11 +962,14 @@ async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain,
assert device.configuration_url == "http://example.com"
async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain, config):
async def help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test device registry integration.
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_MAC)
@@ -955,8 +994,11 @@ async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain,
assert device.configuration_url == "http://example.com"
async def help_test_entity_device_info_remove(hass, mqtt_mock, domain, config):
async def help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test device registry remove."""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -981,11 +1023,14 @@ async def help_test_entity_device_info_remove(hass, mqtt_mock, domain, config):
assert not ent_registry.async_get_entity_id(domain, mqtt.DOMAIN, "veryunique")
async def help_test_entity_device_info_update(hass, mqtt_mock, domain, config):
async def help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test device registry update.
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1012,7 +1057,7 @@ async def help_test_entity_device_info_update(hass, mqtt_mock, domain, config):
async def help_test_entity_id_update_subscriptions(
hass, mqtt_mock, domain, config, topics=None
hass, mqtt_mock_entry_with_yaml_config, domain, config, topics=None
):
"""Test MQTT subscriptions are managed when entity_id is updated."""
# Add unique_id to config
@@ -1026,16 +1071,18 @@ async def help_test_entity_id_update_subscriptions(
topics = ["avty-topic", "test-topic"]
assert len(topics) > 0
registry = mock_registry(hass, {})
assert await async_setup_component(
hass,
domain,
config,
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"{domain}.test")
assert state is not None
assert mqtt_mock.async_subscribe.call_count == len(topics)
assert mqtt_mock.async_subscribe.call_count == len(topics) + 3
for topic in topics:
mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY)
mqtt_mock.async_subscribe.reset_mock()
@@ -1053,10 +1100,11 @@ async def help_test_entity_id_update_subscriptions(
async def help_test_entity_id_update_discovery_update(
hass, mqtt_mock, domain, config, topic=None
hass, mqtt_mock_entry_no_yaml_config, domain, config, topic=None
):
"""Test MQTT discovery update after entity_id is updated."""
# Add unique_id to config
await mqtt_mock_entry_no_yaml_config()
config = copy.deepcopy(config)
config[domain]["unique_id"] = "TOTALLY_UNIQUE"
@@ -1093,11 +1141,14 @@ async def help_test_entity_id_update_discovery_update(
assert state.state != STATE_UNAVAILABLE
async def help_test_entity_debug_info(hass, mqtt_mock, domain, config):
async def help_test_entity_debug_info(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test debug_info.
This is a test helper for MQTT debug_info.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1127,11 +1178,14 @@ async def help_test_entity_debug_info(hass, mqtt_mock, domain, config):
assert len(debug_info_data["triggers"]) == 0
async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, config):
async def help_test_entity_debug_info_max_messages(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test debug_info message overflow.
This is a test helper for MQTT debug_info.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1181,7 +1235,7 @@ async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, conf
async def help_test_entity_debug_info_message(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
domain,
config,
service,
@@ -1196,6 +1250,7 @@ async def help_test_entity_debug_info_message(
This is a test helper for MQTT debug_info.
"""
# Add device settings to config
await mqtt_mock_entry_no_yaml_config()
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
config["unique_id"] = "veryunique"
@@ -1290,11 +1345,14 @@ async def help_test_entity_debug_info_message(
assert debug_info_data["entities"][0]["transmitted"] == expected_transmissions
async def help_test_entity_debug_info_remove(hass, mqtt_mock, domain, config):
async def help_test_entity_debug_info_remove(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test debug_info.
This is a test helper for MQTT debug_info.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1333,11 +1391,14 @@ async def help_test_entity_debug_info_remove(hass, mqtt_mock, domain, config):
assert entity_id not in hass.data[debug_info.DATA_MQTT_DEBUG_INFO]["entities"]
async def help_test_entity_debug_info_update_entity_id(hass, mqtt_mock, domain, config):
async def help_test_entity_debug_info_update_entity_id(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test debug_info.
This is a test helper for MQTT debug_info.
"""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1389,8 +1450,11 @@ async def help_test_entity_debug_info_update_entity_id(hass, mqtt_mock, domain,
)
async def help_test_entity_disabled_by_default(hass, mqtt_mock, domain, config):
async def help_test_entity_disabled_by_default(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test device registry remove."""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1425,8 +1489,11 @@ async def help_test_entity_disabled_by_default(hass, mqtt_mock, domain, config):
assert not dev_registry.async_get_device({("mqtt", "helloworld")})
async def help_test_entity_category(hass, mqtt_mock, domain, config):
async def help_test_entity_category(
hass, mqtt_mock_entry_no_yaml_config, domain, config
):
"""Test device registry remove."""
await mqtt_mock_entry_no_yaml_config()
# Add device settings to config
config = copy.deepcopy(config[domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
@@ -1468,7 +1535,7 @@ async def help_test_entity_category(hass, mqtt_mock, domain, config):
async def help_test_publishing_with_custom_encoding(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
domain,
config,
@@ -1519,6 +1586,7 @@ async def help_test_publishing_with_custom_encoding(
{domain: setup_config},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
# 1) test with default encoding
await hass.services.async_call(
@@ -1602,7 +1670,9 @@ async def help_test_reload_with_config(hass, caplog, tmp_path, domain, config):
assert "<Event event_mqtt_reloaded[L]>" in caplog.text
async def help_test_reloadable(hass, mqtt_mock, caplog, tmp_path, domain, config):
async def help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
):
"""Test reloading an MQTT platform."""
# Create and test an old config of 2 entities based on the config supplied
old_config_1 = copy.deepcopy(config)
@@ -1614,6 +1684,7 @@ async def help_test_reloadable(hass, mqtt_mock, caplog, tmp_path, domain, config
hass, domain, {domain: [old_config_1, old_config_2]}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test_old_1")
assert hass.states.get(f"{domain}.test_old_2")