From 185d6d74d75290cb331c2ebc98546c2ad17d9beb Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Thu, 23 Mar 2023 19:17:27 +0100 Subject: [PATCH] Prepare MQTT platform tests part9 (#90133) * Tests switch * Tests text * Tests update --- tests/components/mqtt/test_switch.py | 117 +++++++------ tests/components/mqtt/test_text.py | 137 +++++++-------- tests/components/mqtt/test_update.py | 238 +++++++++++++-------------- 3 files changed, 249 insertions(+), 243 deletions(-) diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index 83580edf003..79f2bcc4a7c 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -15,7 +15,6 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant, State -from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, @@ -62,13 +61,9 @@ def switch_platform_only(): yield -async def test_controlling_state_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { switch.DOMAIN: { @@ -80,10 +75,14 @@ async def test_controlling_state_via_topic( "device_class": "switch", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("switch.test") assert state.state == STATE_UNKNOWN @@ -106,16 +105,9 @@ async def test_controlling_state_via_topic( assert state.state == STATE_UNKNOWN -async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending MQTT commands in optimistic mode.""" - fake_state = State("switch.test", "on") - mock_restore_cache(hass, (fake_state,)) - - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { switch.DOMAIN: { @@ -126,10 +118,17 @@ async def test_sending_mqtt_commands_and_optimistic( "qos": "2", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_and_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending MQTT commands in optimistic mode.""" + fake_state = State("switch.test", "on") + mock_restore_cache(hass, (fake_state,)) + + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("switch.test") assert state.state == STATE_ON @@ -153,13 +152,9 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.state == STATE_OFF -async def test_sending_inital_state_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the initial state in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { switch.DOMAIN: { @@ -167,23 +162,23 @@ async def test_sending_inital_state_and_optimistic( "command_topic": "command-topic", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_inital_state_and_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the initial state in optimistic mode.""" + await mqtt_mock_entry_no_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: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via topic and JSON message.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { switch.DOMAIN: { @@ -195,10 +190,14 @@ async def test_controlling_state_via_topic_and_json_message( "value_template": "{{ value_json.val }}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic_and_json_message( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via topic and JSON message.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("switch.test") assert state.state == STATE_UNKNOWN @@ -292,13 +291,9 @@ async def test_custom_availability_payload( ) -async def test_custom_state_payload( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the state payload.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { switch.DOMAIN: { @@ -311,10 +306,14 @@ async def test_custom_state_payload( "state_off": "LOW", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_custom_state_payload( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the state payload.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("switch.test") assert state.state == STATE_UNKNOWN diff --git a/tests/components/mqtt/test_text.py b/tests/components/mqtt/test_text.py index d12a03a9fa1..10e9f0780d5 100644 --- a/tests/components/mqtt/test_text.py +++ b/tests/components/mqtt/test_text.py @@ -14,7 +14,6 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, @@ -72,13 +71,9 @@ async def async_set_value( ) -async def test_controlling_state_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { text.DOMAIN: { @@ -88,10 +83,14 @@ async def test_controlling_state_via_topic( "mode": "password", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("text.test") assert state.state == STATE_UNKNOWN @@ -114,15 +113,9 @@ async def test_controlling_state_via_topic( assert state.state == "" -async def test_controlling_validation_state_via_topic( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the validation of a received state.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { text.DOMAIN: { @@ -135,10 +128,16 @@ async def test_controlling_validation_state_via_topic( "pattern": "(y|n)", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_validation_state_via_topic( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the validation of a received state.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("text.test") assert state.state == STATE_UNKNOWN @@ -188,11 +187,9 @@ async def test_controlling_validation_state_via_topic( assert state.state == "no" -async def test_attribute_validation_max_greater_then_min(hass: HomeAssistant) -> None: - """Test the validation of min and max configuration attributes.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { text.DOMAIN: { @@ -202,17 +199,20 @@ async def test_attribute_validation_max_greater_then_min(hass: HomeAssistant) -> "max": 10, } } - }, - ) - - -async def test_attribute_validation_max_not_greater_then_max_state_length( - hass: HomeAssistant, + } + ], +) +async def test_attribute_validation_max_greater_then_min( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: - """Test the max value of of max configuration attribute.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, + """Test the validation of min and max configuration attributes.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() + + +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { text.DOMAIN: { @@ -222,17 +222,20 @@ async def test_attribute_validation_max_not_greater_then_max_state_length( "max": 257, } } - }, - ) - - -async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + } + ], +) +async def test_attribute_validation_max_not_greater_then_max_state_length( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: - """Test the sending MQTT commands in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, + """Test the max value of of max configuration attribute.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() + + +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { text.DOMAIN: { @@ -241,10 +244,14 @@ async def test_sending_mqtt_commands_and_optimistic( "qos": "2", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_and_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending MQTT commands in optimistic mode.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("text.test") assert state.state == STATE_UNKNOWN @@ -269,13 +276,9 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.state == "some new state" -async def test_set_text_validation( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the initial state in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { text.DOMAIN: { @@ -287,10 +290,14 @@ async def test_set_text_validation( "pattern": "(y|n)", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_text_validation( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the initial state in optimistic mode.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("text.test") assert state.state == STATE_UNKNOWN diff --git a/tests/components/mqtt/test_update.py b/tests/components/mqtt/test_update.py index 200a3ca6dd8..bdd85768b85 100644 --- a/tests/components/mqtt/test_update.py +++ b/tests/components/mqtt/test_update.py @@ -14,7 +14,6 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, @@ -63,20 +62,14 @@ def update_platform_only(): yield -async def test_run_update_setup( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test that it fetches the given payload.""" - installed_version_topic = "test/installed-version" - latest_version_topic = "test/latest-version" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": installed_version_topic, - "latest_version_topic": latest_version_topic, + "state_topic": "test/installed-version", + "latest_version_topic": "test/latest-version", "name": "Test Update", "release_summary": "Test release summary", "release_url": "https://example.com/release", @@ -84,10 +77,16 @@ async def test_run_update_setup( "entity_picture": "https://example.com/icon.png", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_run_update_setup( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test that it fetches the given payload.""" + installed_version_topic = "test/installed-version" + latest_version_topic = "test/latest-version" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, installed_version_topic, "1.9.0") async_fire_mqtt_message(hass, latest_version_topic, "1.9.0") @@ -113,20 +112,14 @@ async def test_run_update_setup( assert state.attributes.get("latest_version") == "2.0.0" -async def test_run_update_setup_float( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test that it fetches the given payload when the version is parsable as a number.""" - installed_version_topic = "test/installed-version" - latest_version_topic = "test/latest-version" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": installed_version_topic, - "latest_version_topic": latest_version_topic, + "state_topic": "test/installed-version", + "latest_version_topic": "test/latest-version", "name": "Test Update", "release_summary": "Test release summary", "release_url": "https://example.com/release", @@ -134,10 +127,16 @@ async def test_run_update_setup_float( "entity_picture": "https://example.com/icon.png", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_run_update_setup_float( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test that it fetches the given payload when the version is parsable as a number.""" + installed_version_topic = "test/installed-version" + latest_version_topic = "test/latest-version" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, installed_version_topic, "1.9") async_fire_mqtt_message(hass, latest_version_topic, "1.9") @@ -163,29 +162,29 @@ async def test_run_update_setup_float( assert state.attributes.get("latest_version") == "2.0" -async def test_value_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test that it fetches the given payload with a template.""" - installed_version_topic = "test/installed-version" - latest_version_topic = "test/latest-version" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": installed_version_topic, + "state_topic": "test/installed-version", "value_template": "{{ value_json.installed }}", - "latest_version_topic": latest_version_topic, + "latest_version_topic": "test/latest-version", "latest_version_template": "{{ value_json.latest }}", "name": "Test Update", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_value_template( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test that it fetches the given payload with a template.""" + installed_version_topic = "test/installed-version" + latest_version_topic = "test/latest-version" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, installed_version_topic, '{"installed":"1.9.0"}') async_fire_mqtt_message(hass, latest_version_topic, '{"latest":"1.9.0"}') @@ -211,29 +210,29 @@ async def test_value_template( assert state.attributes.get("latest_version") == "2.0.0" -async def test_value_template_float( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test that it fetches the given payload with a template when the version is parsable as a number.""" - installed_version_topic = "test/installed-version" - latest_version_topic = "test/latest-version" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": installed_version_topic, + "state_topic": "test/installed-version", "value_template": "{{ value_json.installed }}", - "latest_version_topic": latest_version_topic, + "latest_version_topic": "test/latest-version", "latest_version_template": "{{ value_json.latest }}", "name": "Test Update", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_value_template_float( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test that it fetches the given payload with a template when the version is parsable as a number.""" + installed_version_topic = "test/installed-version" + latest_version_topic = "test/latest-version" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, installed_version_topic, '{"installed":"1.9"}') async_fire_mqtt_message(hass, latest_version_topic, '{"latest":"1.9"}') @@ -259,25 +258,25 @@ async def test_value_template_float( assert state.attributes.get("latest_version") == "2.0" -async def test_empty_json_state_message( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test an empty JSON payload.""" - state_topic = "test/state-topic" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": state_topic, + "state_topic": "test/state-topic", "name": "Test Update", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_empty_json_state_message( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test an empty JSON payload.""" + state_topic = "test/state-topic" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, state_topic, "{}") @@ -287,25 +286,25 @@ async def test_empty_json_state_message( assert state.state == STATE_UNKNOWN -async def test_json_state_message( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test whether it fetches data from a JSON payload.""" - state_topic = "test/state-topic" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": state_topic, + "state_topic": "test/state-topic", "name": "Test Update", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_json_state_message( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test whether it fetches data from a JSON payload.""" + state_topic = "test/state-topic" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message( hass, @@ -343,26 +342,27 @@ async def test_json_state_message( assert state.attributes.get("entity_picture") == "https://example.com/icon2.png" -async def test_json_state_message_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test whether it fetches data from a JSON payload with template.""" - state_topic = "test/state-topic" - await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { update.DOMAIN: { - "state_topic": state_topic, - "value_template": '{{ {"installed_version": value_json.installed, "latest_version": value_json.latest} | to_json }}', + "state_topic": "test/state-topic", + "value_template": '{{ {"installed_version": value_json.installed, ' + '"latest_version": value_json.latest} | to_json }}', "name": "Test Update", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_json_state_message_with_template( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test whether it fetches data from a JSON payload with template.""" + state_topic = "test/state-topic" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, state_topic, '{"installed":"1.9.0","latest":"1.9.0"}') @@ -383,31 +383,31 @@ async def test_json_state_message_with_template( assert state.attributes.get("latest_version") == "2.0.0" +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + update.DOMAIN: { + "state_topic": "test/installed-version", + "latest_version_topic": "test/latest-version", + "command_topic": "test/install-command", + "payload_install": "install", + "name": "Test Update", + } + } + } + ], +) async def test_run_install_service( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test that install service works.""" installed_version_topic = "test/installed-version" latest_version_topic = "test/latest-version" command_topic = "test/install-command" - await async_setup_component( - hass, - mqtt.DOMAIN, - { - mqtt.DOMAIN: { - update.DOMAIN: { - "state_topic": installed_version_topic, - "latest_version_topic": latest_version_topic, - "command_topic": command_topic, - "payload_install": "install", - "name": "Test Update", - } - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, installed_version_topic, "1.9.0") async_fire_mqtt_message(hass, latest_version_topic, "2.0.0")