1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 05:57:01 +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

@@ -53,7 +53,7 @@ DEFAULT_CONFIG = {
}
async def test_controlling_state_via_topic(hass, mqtt_mock):
async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
@@ -71,6 +71,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
@@ -93,7 +94,9 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
assert state.state == STATE_UNKNOWN
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
async def test_sending_mqtt_commands_and_optimistic(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the sending MQTT commands in optimistic mode."""
fake_state = ha.State("switch.test", "on")
@@ -116,6 +119,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("switch.test")
assert state.state == STATE_ON
@@ -139,7 +143,9 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
assert state.state == STATE_OFF
async def test_sending_inital_state_and_optimistic(hass, mqtt_mock):
async def test_sending_inital_state_and_optimistic(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the initial state in optimistic mode."""
assert await async_setup_component(
hass,
@@ -153,13 +159,16 @@ async def test_sending_inital_state_and_optimistic(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
async def test_controlling_state_via_topic_and_json_message(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
@@ -177,6 +186,7 @@ async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
@@ -197,21 +207,23 @@ async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
assert state.state == STATE_UNKNOWN
async def test_availability_when_connection_lost(hass, mqtt_mock):
async def test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_availability_without_topic(hass, mqtt_mock):
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_payload(hass, mqtt_mock):
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
config = {
switch.DOMAIN: {
@@ -225,11 +237,17 @@ async def test_default_availability_payload(hass, mqtt_mock):
}
await help_test_default_availability_payload(
hass, mqtt_mock, switch.DOMAIN, config, True, "state-topic", "1"
hass,
mqtt_mock_entry_with_yaml_config,
switch.DOMAIN,
config,
True,
"state-topic",
"1",
)
async def test_custom_availability_payload(hass, mqtt_mock):
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
config = {
switch.DOMAIN: {
@@ -243,11 +261,17 @@ async def test_custom_availability_payload(hass, mqtt_mock):
}
await help_test_custom_availability_payload(
hass, mqtt_mock, switch.DOMAIN, config, True, "state-topic", "1"
hass,
mqtt_mock_entry_with_yaml_config,
switch.DOMAIN,
config,
True,
"state-topic",
"1",
)
async def test_custom_state_payload(hass, mqtt_mock):
async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test the state payload."""
assert await async_setup_component(
hass,
@@ -266,6 +290,7 @@ async def test_custom_state_payload(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
@@ -282,49 +307,57 @@ async def test_custom_state_payload(hass, mqtt_mock):
assert state.state == STATE_OFF
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_setting_blocked_attribute_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_no_yaml_config
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG, {}
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG, {}
)
async def test_setting_attribute_with_template(hass, mqtt_mock):
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
async def test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock, caplog, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
async def test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock, caplog, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_unique_id(hass, mqtt_mock):
async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
"""Test unique id option only creates one switch per unique_id."""
config = {
switch.DOMAIN: [
@@ -344,20 +377,26 @@ async def test_unique_id(hass, mqtt_mock):
},
]
}
await help_test_unique_id(hass, mqtt_mock, switch.DOMAIN, config)
await help_test_unique_id(
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, config
)
async def test_discovery_removal_switch(hass, mqtt_mock, caplog):
async def test_discovery_removal_switch(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test removal of discovered switch."""
data = (
'{ "name": "test",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
await help_test_discovery_removal(hass, mqtt_mock, caplog, switch.DOMAIN, data)
await help_test_discovery_removal(
hass, mqtt_mock_entry_no_yaml_config, caplog, switch.DOMAIN, data
)
async def test_discovery_update_switch_topic_template(hass, mqtt_mock, caplog):
async def test_discovery_update_switch_topic_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered switch."""
config1 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
@@ -382,7 +421,7 @@ async def test_discovery_update_switch_topic_template(hass, mqtt_mock, caplog):
await help_test_discovery_update(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
caplog,
switch.DOMAIN,
config1,
@@ -392,7 +431,9 @@ async def test_discovery_update_switch_topic_template(hass, mqtt_mock, caplog):
)
async def test_discovery_update_switch_template(hass, mqtt_mock, caplog):
async def test_discovery_update_switch_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered switch."""
config1 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
@@ -415,7 +456,7 @@ async def test_discovery_update_switch_template(hass, mqtt_mock, caplog):
await help_test_discovery_update(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
caplog,
switch.DOMAIN,
config1,
@@ -425,7 +466,9 @@ async def test_discovery_update_switch_template(hass, mqtt_mock, caplog):
)
async def test_discovery_update_unchanged_switch(hass, mqtt_mock, caplog):
async def test_discovery_update_unchanged_switch(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered switch."""
data1 = (
'{ "name": "Beer",'
@@ -437,12 +480,17 @@ async def test_discovery_update_unchanged_switch(hass, mqtt_mock, caplog):
"homeassistant.components.mqtt.switch.MqttSwitch.discovery_update"
) as discovery_update:
await help_test_discovery_update_unchanged(
hass, mqtt_mock, caplog, switch.DOMAIN, data1, discovery_update
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
switch.DOMAIN,
data1,
discovery_update,
)
@pytest.mark.no_fail_on_log_exception
async def test_discovery_broken(hass, mqtt_mock, caplog):
async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test handling of bad discovery message."""
data1 = '{ "name": "Beer" }'
data2 = (
@@ -451,56 +499,60 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
' "command_topic": "test_topic" }'
)
await help_test_discovery_broken(
hass, mqtt_mock, caplog, switch.DOMAIN, data1, data2
hass, mqtt_mock_entry_no_yaml_config, caplog, switch.DOMAIN, data1, data2
)
async def test_entity_device_info_with_connection(hass, mqtt_mock):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT switch device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT switch device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_update(hass, mqtt_mock):
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_remove(hass, mqtt_mock):
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock):
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock):
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_message(hass, mqtt_mock):
async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT debug info."""
await help_test_entity_debug_info_message(
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG, switch.SERVICE_TURN_ON
hass,
mqtt_mock_entry_no_yaml_config,
switch.DOMAIN,
DEFAULT_CONFIG,
switch.SERVICE_TURN_ON,
)
@@ -525,7 +577,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock):
)
async def test_publishing_with_custom_encoding(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
service,
topic,
@@ -539,7 +591,7 @@ async def test_publishing_with_custom_encoding(
await help_test_publishing_with_custom_encoding(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
domain,
config,
@@ -551,11 +603,13 @@ async def test_publishing_with_custom_encoding(
)
async def test_reloadable(hass, mqtt_mock, caplog, tmp_path):
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG[domain]
await help_test_reloadable(hass, mqtt_mock, caplog, tmp_path, domain, config)
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
@@ -572,12 +626,18 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
],
)
async def test_encoding_subscribable_topics(
hass, mqtt_mock, caplog, topic, value, attribute, attribute_value
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
topic,
value,
attribute,
attribute_value,
):
"""Test handling of incoming encoded payload."""
await help_test_encoding_subscribable_topics(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
switch.DOMAIN,
DEFAULT_CONFIG[switch.DOMAIN],