diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index a09edcd25e0..dd28d0919bd 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -45,7 +45,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, @@ -91,13 +90,9 @@ def cover_platform_only(): yield -async def test_state_via_state_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: { cover.DOMAIN: { @@ -110,10 +105,14 @@ async def test_state_via_state_topic( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_via_state_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("cover.test") assert state.state == STATE_UNKNOWN @@ -130,13 +129,9 @@ async def test_state_via_state_topic( assert state.state == STATE_OPEN -async def test_opening_and_closing_state_via_custom_state_payload( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling opening and closing state via a custom payload.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -151,10 +146,14 @@ async def test_opening_and_closing_state_via_custom_state_payload( "state_closing": "--43", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_opening_and_closing_state_via_custom_state_payload( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling opening and closing state via a custom payload.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -176,13 +175,9 @@ async def test_opening_and_closing_state_via_custom_state_payload( assert state.state == STATE_CLOSED -async def test_open_closed_state_from_position_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the state after setting the position using optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -196,10 +191,14 @@ async def test_open_closed_state_from_position_optimistic( "optimistic": True, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_open_closed_state_from_position_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the state after setting the position using optimistic mode.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -227,13 +226,9 @@ async def test_open_closed_state_from_position_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) -async def test_position_via_position_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: { cover.DOMAIN: { @@ -248,10 +243,14 @@ async def test_position_via_position_topic( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_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("cover.test") assert state.state == STATE_UNKNOWN @@ -268,13 +267,9 @@ async def test_position_via_position_topic( assert state.state == STATE_OPEN -async def test_state_via_template( - 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: { cover.DOMAIN: { @@ -290,10 +285,14 @@ async def test_state_via_template( {% endif %}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_via_template( + 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("cover.test") assert state.state == STATE_UNKNOWN @@ -309,13 +308,9 @@ async def test_state_via_template( assert state.state == STATE_CLOSED -async def test_state_via_template_and_entity_id( - 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: { cover.DOMAIN: { @@ -331,10 +326,14 @@ async def test_state_via_template_and_entity_id( {% endif %}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_via_template_and_entity_id( + 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("cover.test") assert state.state == STATE_UNKNOWN @@ -352,15 +351,9 @@ async def test_state_via_template_and_entity_id( assert state.state == STATE_CLOSED -async def test_state_via_template_with_json_value( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic with JSON value.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -371,10 +364,16 @@ async def test_state_via_template_with_json_value( "value_template": "{{ value_json.Var1 }}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_via_template_with_json_value( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic with JSON value.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -397,13 +396,9 @@ async def test_state_via_template_with_json_value( ) in caplog.text -async def test_position_via_template_and_entity_id( - 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: { cover.DOMAIN: { @@ -419,10 +414,14 @@ async def test_position_via_template_and_entity_id( {% endif %}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_template_and_entity_id( + 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("cover.test") assert state.state == STATE_UNKNOWN @@ -443,30 +442,77 @@ async def test_position_via_template_and_entity_id( @pytest.mark.parametrize( - ("config", "assumed_state"), + ("hass_config", "assumed_state"), [ - ({"command_topic": "abc"}, True), - ({"command_topic": "abc", "state_topic": "abc"}, False), + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: {"name": "test", "qos": 0, "command_topic": "abc"} + } + }, + True, + ), + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: { + "name": "test", + "qos": 0, + "command_topic": "abc", + "state_topic": "abc", + } + } + }, + False, + ), # ({"set_position_topic": "abc"}, True), - not a valid configuration - ({"set_position_topic": "abc", "position_topic": "abc"}, False), - ({"tilt_command_topic": "abc"}, True), - ({"tilt_command_topic": "abc", "tilt_status_topic": "abc"}, False), + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: { + "name": "test", + "qos": 0, + "set_position_topic": "abc", + "position_topic": "abc", + } + } + }, + False, + ), + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: { + "name": "test", + "qos": 0, + "tilt_command_topic": "abc", + } + } + }, + True, + ), + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: { + "name": "test", + "qos": 0, + "tilt_command_topic": "abc", + "tilt_status_topic": "abc", + } + } + }, + False, + ), ], ) async def test_optimistic_flag( hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - config, - assumed_state, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + assumed_state: bool, ) -> None: """Test assumed_state is set correctly.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {cover.DOMAIN: {**config, "name": "test", "qos": 0}}}, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -476,13 +522,9 @@ async def test_optimistic_flag( assert ATTR_ASSUMED_STATE not in state.attributes -async def test_optimistic_state_change( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test changing state optimistically.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -491,10 +533,14 @@ async def test_optimistic_state_change( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_optimistic_state_change( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test changing state optimistically.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -536,13 +582,9 @@ async def test_optimistic_state_change( assert state.state == STATE_CLOSED -async def test_optimistic_state_change_with_position( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test changing state optimistically.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -553,10 +595,14 @@ async def test_optimistic_state_change_with_position( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_optimistic_state_change_with_position( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test changing state optimistically.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -603,13 +649,9 @@ async def test_optimistic_state_change_with_position( assert state.attributes.get(ATTR_CURRENT_POSITION) == 0 -async def test_send_open_cover_command( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending of open_cover.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -619,10 +661,14 @@ async def test_send_open_cover_command( "qos": 2, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_send_open_cover_command( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending of open_cover.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -636,13 +682,9 @@ async def test_send_open_cover_command( assert state.state == STATE_UNKNOWN -async def test_send_close_cover_command( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending of close_cover.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -652,10 +694,14 @@ async def test_send_close_cover_command( "qos": 2, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_send_close_cover_command( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending of close_cover.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -669,13 +715,9 @@ async def test_send_close_cover_command( assert state.state == STATE_UNKNOWN -async def test_send_stop__cover_command( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending of stop_cover.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -685,10 +727,14 @@ async def test_send_stop__cover_command( "qos": 2, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_send_stop_cover_command( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending of stop_cover.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -702,13 +748,9 @@ async def test_send_stop__cover_command( assert state.state == STATE_UNKNOWN -async def test_current_cover_position( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the current cover position.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -722,10 +764,14 @@ async def test_current_cover_position( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_current_cover_position( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the current cover position.""" + await mqtt_mock_entry_no_yaml_config() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION not in state_attributes_dict @@ -757,13 +803,9 @@ async def test_current_cover_position( assert current_cover_position == 100 -async def test_current_cover_position_inverted( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the current cover position.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -777,10 +819,14 @@ async def test_current_cover_position_inverted( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_current_cover_position_inverted( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the current cover position.""" + await mqtt_mock_entry_no_yaml_config() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION not in state_attributes_dict @@ -823,13 +869,9 @@ async def test_current_cover_position_inverted( assert hass.states.get("cover.test").state == STATE_CLOSED -async def test_optimistic_position( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture -) -> None: - """Test optimistic position is not supported.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -838,21 +880,26 @@ async def test_optimistic_position( "set_position_topic": "set-position-topic", } } - }, - ) + } + ], +) +async def test_optimistic_position( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test optimistic position is not supported.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( "Invalid config for [mqtt]: 'set_position_topic' must be set together with 'position_topic'" in caplog.text ) -async def test_position_update( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test cover position update from received MQTT message.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -867,10 +914,14 @@ async def test_position_update( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_update( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test cover position update from received MQTT message.""" + await mqtt_mock_entry_no_yaml_config() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION not in state_attributes_dict @@ -888,39 +939,58 @@ async def test_position_update( @pytest.mark.parametrize( - ("pos_template", "pos_call", "pos_message"), - [("{{position-1}}", 43, "42"), ("{{100-62}}", 100, "38")], + ("hass_config", "pos_call", "pos_message"), + [ + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: { + "name": "test", + "position_topic": "get-position-topic", + "command_topic": "command-topic", + "position_open": 100, + "position_closed": 0, + "set_position_topic": "set-position-topic", + "set_position_template": "{{position-1}}", + "payload_open": "OPEN", + "payload_close": "CLOSE", + "payload_stop": "STOP", + } + } + }, + 43, + "42", + ), + ( + { + mqtt.DOMAIN: { + cover.DOMAIN: { + "name": "test", + "position_topic": "get-position-topic", + "command_topic": "command-topic", + "position_open": 100, + "position_closed": 0, + "set_position_topic": "set-position-topic", + "set_position_template": "{{100-62}}", + "payload_open": "OPEN", + "payload_close": "CLOSE", + "payload_stop": "STOP", + } + } + }, + 100, + "38", + ), + ], ) async def test_set_position_templated( hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - pos_template, - pos_call, - pos_message, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + pos_call: int, + pos_message: str, ) -> None: """Test setting cover position via template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, - { - mqtt.DOMAIN: { - cover.DOMAIN: { - "name": "test", - "position_topic": "get-position-topic", - "command_topic": "command-topic", - "position_open": 100, - "position_closed": 0, - "set_position_topic": "set-position-topic", - "set_position_template": pos_template, - "payload_open": "OPEN", - "payload_close": "CLOSE", - "payload_stop": "STOP", - } - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -934,13 +1004,9 @@ async def test_set_position_templated( ) -async def test_set_position_templated_and_attributes( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test setting cover position via template and using entities attributes.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -965,10 +1031,14 @@ async def test_set_position_templated_and_attributes( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_position_templated_and_attributes( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test setting cover position via template and using entities attributes.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -980,13 +1050,9 @@ async def test_set_position_templated_and_attributes( mqtt_mock.async_publish.assert_called_once_with("set-position-topic", "5", 0, False) -async def test_set_tilt_templated( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test setting cover tilt position via template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1004,10 +1070,14 @@ async def test_set_tilt_templated( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_tilt_templated( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test setting cover tilt position via template.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1021,13 +1091,9 @@ async def test_set_tilt_templated( ) -async def test_set_tilt_templated_and_attributes( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test setting cover tilt position via template and using entities attributes.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1049,10 +1115,14 @@ async def test_set_tilt_templated_and_attributes( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_tilt_templated_and_attributes( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test setting cover tilt position via template and using entities attributes.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1111,13 +1181,9 @@ async def test_set_tilt_templated_and_attributes( ) -async def test_set_position_untemplated( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test setting cover position via template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1130,10 +1196,14 @@ async def test_set_position_untemplated( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_position_untemplated( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test setting cover position via template.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1145,13 +1215,9 @@ async def test_set_position_untemplated( mqtt_mock.async_publish.assert_called_once_with("position-topic", "62", 0, False) -async def test_set_position_untemplated_custom_percentage_range( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test setting cover position via template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1166,10 +1232,14 @@ async def test_set_position_untemplated_custom_percentage_range( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_position_untemplated_custom_percentage_range( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test setting cover position via template.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1181,13 +1251,9 @@ async def test_set_position_untemplated_custom_percentage_range( mqtt_mock.async_publish.assert_called_once_with("position-topic", "62", 0, False) -async def test_no_command_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test with no command topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1200,21 +1266,21 @@ async def test_no_command_topic( "tilt_status_topic": "tilt-status", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_no_command_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test with no command topic.""" + await mqtt_mock_entry_no_yaml_config() assert hass.states.get("cover.test").attributes["supported_features"] == 240 -async def test_no_payload_close( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test with no close payload.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1226,21 +1292,21 @@ async def test_no_payload_close( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_no_payload_close( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test with no close payload.""" + await mqtt_mock_entry_no_yaml_config() assert hass.states.get("cover.test").attributes["supported_features"] == 9 -async def test_no_payload_open( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test with no open payload.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1252,21 +1318,21 @@ async def test_no_payload_open( "payload_stop": "STOP", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_no_payload_open( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test with no open payload.""" + await mqtt_mock_entry_no_yaml_config() assert hass.states.get("cover.test").attributes["supported_features"] == 10 -async def test_no_payload_stop( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test with no stop payload.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1278,21 +1344,21 @@ async def test_no_payload_stop( "payload_stop": None, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_no_payload_stop( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test with no stop payload.""" + await mqtt_mock_entry_no_yaml_config() assert hass.states.get("cover.test").attributes["supported_features"] == 3 -async def test_with_command_topic_and_tilt( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test with command topic and tilt config.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1306,21 +1372,21 @@ async def test_with_command_topic_and_tilt( "tilt_status_topic": "tilt-status", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_with_command_topic_and_tilt( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test with command topic and tilt config.""" + await mqtt_mock_entry_no_yaml_config() assert hass.states.get("cover.test").attributes["supported_features"] == 251 -async def test_tilt_defaults( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the defaults.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1335,23 +1401,23 @@ async def test_tilt_defaults( "tilt_status_topic": "tilt-status", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_defaults( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the defaults.""" + await mqtt_mock_entry_no_yaml_config() state_attributes_dict = hass.states.get("cover.test").attributes # Tilt position is not yet known assert ATTR_CURRENT_TILT_POSITION not in state_attributes_dict -async def test_tilt_via_invocation_defaults( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt defaults on close/open.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1366,10 +1432,15 @@ async def test_tilt_via_invocation_defaults( "tilt_status_topic": "tilt-status-topic", } } - }, - ) + } + ], +) +async def test_tilt_via_invocation_defaults( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt defaults on close/open.""" await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1431,13 +1502,9 @@ async def test_tilt_via_invocation_defaults( mqtt_mock.async_publish.assert_called_once_with("tilt-command-topic", "0", 0, False) -async def test_tilt_given_value( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilting to a given value.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1454,10 +1521,14 @@ async def test_tilt_given_value( "tilt_closed_value": 25, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_given_value( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilting to a given value.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1523,13 +1594,9 @@ async def test_tilt_given_value( ) -async def test_tilt_given_value_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilting to a given value.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1547,10 +1614,14 @@ async def test_tilt_given_value_optimistic( "tilt_optimistic": True, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_given_value_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilting to a given value.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1603,13 +1674,9 @@ async def test_tilt_given_value_optimistic( ) -async def test_tilt_given_value_altered_range( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilting to a given value.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1629,10 +1696,14 @@ async def test_tilt_given_value_altered_range( "tilt_optimistic": True, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_given_value_altered_range( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilting to a given value.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -1683,13 +1754,9 @@ async def test_tilt_given_value_altered_range( ) -async def test_tilt_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt by updating status via MQTT.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1704,10 +1771,14 @@ async def test_tilt_via_topic( "tilt_status_topic": "tilt-status-topic", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt by updating status via MQTT.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "0") @@ -1724,13 +1795,9 @@ async def test_tilt_via_topic( assert current_cover_tilt_position == 50 -async def test_tilt_via_topic_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt by updating status via MQTT and template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1748,10 +1815,14 @@ async def test_tilt_via_topic_template( "tilt_closed_value": 125, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_via_topic_template( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt by updating status via MQTT and template.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "99") @@ -1768,15 +1839,9 @@ async def test_tilt_via_topic_template( assert current_cover_tilt_position == 50 -async def test_tilt_via_topic_template_json_value( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test tilt by updating status via MQTT and template with JSON value.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1794,10 +1859,16 @@ async def test_tilt_via_topic_template_json_value( "tilt_closed_value": 125, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_via_topic_template_json_value( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test tilt by updating status via MQTT and template with JSON value.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", '{"Var1": 9, "Var2": 30}') @@ -1820,13 +1891,9 @@ async def test_tilt_via_topic_template_json_value( ) in caplog.text -async def test_tilt_via_topic_altered_range( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt status via MQTT with altered tilt range.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1843,10 +1910,14 @@ async def test_tilt_via_topic_altered_range( "tilt_max": 50, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_via_topic_altered_range( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt status via MQTT with altered tilt range.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "0") @@ -1870,15 +1941,9 @@ async def test_tilt_via_topic_altered_range( assert current_cover_tilt_position == 50 -async def test_tilt_status_out_of_range_warning( - hass: HomeAssistant, - caplog: pytest.LogCaptureFixture, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, -) -> None: - """Test tilt status via MQTT tilt out of range warning message.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1895,10 +1960,16 @@ async def test_tilt_status_out_of_range_warning( "tilt_max": 50, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_status_out_of_range_warning( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test tilt status via MQTT tilt out of range warning message.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "60") @@ -1907,15 +1978,9 @@ async def test_tilt_status_out_of_range_warning( ) in caplog.text -async def test_tilt_status_not_numeric_warning( - hass: HomeAssistant, - caplog: pytest.LogCaptureFixture, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, -) -> None: - """Test tilt status via MQTT tilt not numeric warning message.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1932,23 +1997,25 @@ async def test_tilt_status_not_numeric_warning( "tilt_max": 50, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_status_not_numeric_warning( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test tilt status via MQTT tilt not numeric warning message.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "abc") assert ("Payload 'abc' is not numeric") in caplog.text -async def test_tilt_via_topic_altered_range_inverted( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt status via MQTT with altered tilt range and inverted tilt position.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -1965,10 +2032,14 @@ async def test_tilt_via_topic_altered_range_inverted( "tilt_max": 0, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_via_topic_altered_range_inverted( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt status via MQTT with altered tilt range and inverted tilt position.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "0") @@ -1992,13 +2063,9 @@ async def test_tilt_via_topic_altered_range_inverted( assert current_cover_tilt_position == 50 -async def test_tilt_via_topic_template_altered_range( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt status via MQTT and template with altered tilt range.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2018,10 +2085,14 @@ async def test_tilt_via_topic_template_altered_range( "tilt_max": 50, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_via_topic_template_altered_range( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt status via MQTT and template with altered tilt range.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "tilt-status-topic", "99") @@ -2045,13 +2116,9 @@ async def test_tilt_via_topic_template_altered_range( assert current_cover_tilt_position == 50 -async def test_tilt_position( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt via method invocation.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2066,10 +2133,14 @@ async def test_tilt_position( "tilt_status_topic": "tilt-status-topic", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_position( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt via method invocation.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -2083,13 +2154,9 @@ async def test_tilt_position( ) -async def test_tilt_position_templated( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt position via template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2105,10 +2172,14 @@ async def test_tilt_position_templated( "tilt_command_template": "{{100-32}}", } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_position_templated( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt position via template.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -2122,13 +2193,9 @@ async def test_tilt_position_templated( ) -async def test_tilt_position_altered_range( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test tilt via method invocation with altered range.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2147,10 +2214,14 @@ async def test_tilt_position_altered_range( "tilt_max": 50, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_tilt_position_altered_range( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test tilt via method invocation with altered range.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( cover.DOMAIN, @@ -2164,6 +2235,7 @@ async def test_tilt_position_altered_range( ) +@pytest.mark.parametrize("hass_config", []) async def test_find_percentage_in_range_defaults(hass: HomeAssistant) -> None: """Test find percentage in range with default range.""" mqtt_cover = MqttCover( @@ -2546,13 +2618,9 @@ async def test_custom_availability_payload( ) -async def test_valid_device_class( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the setting of a valid device class.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2561,22 +2629,22 @@ async def test_valid_device_class( "state_topic": "test-topic", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_valid_device_class( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the setting of a valid device class.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.attributes.get("device_class") == "garage" -async def test_invalid_device_class( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture -) -> None: - """Test the setting of an invalid device class.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2585,8 +2653,17 @@ async def test_invalid_device_class( "state_topic": "test-topic", } } - }, - ) + } + ], +) +async def test_invalid_device_class( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test the setting of an invalid device class.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert "Invalid config for [mqtt]: expected CoverDeviceClass" in caplog.text @@ -2821,13 +2898,9 @@ async def test_entity_debug_info_message( ) -async def test_state_and_position_topics_state_not_set_via_position_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test state is not set via position topic when both state and position topics are set.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2842,10 +2915,14 @@ async def test_state_and_position_topics_state_not_set_via_position_topic( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_and_position_topics_state_not_set_via_position_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test state is not set via position topic when both state and position topics are set.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -2882,13 +2959,9 @@ async def test_state_and_position_topics_state_not_set_via_position_topic( assert state.state == STATE_CLOSED -async def test_set_state_via_position_using_stopped_state( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via position topic using stopped state.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2904,10 +2977,14 @@ async def test_set_state_via_position_using_stopped_state( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_state_via_position_using_stopped_state( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via position topic using stopped state.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN @@ -2939,13 +3016,9 @@ async def test_set_state_via_position_using_stopped_state( assert state.state == STATE_OPEN -async def test_position_via_position_topic_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test position by updating status via position template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2957,10 +3030,14 @@ async def test_position_via_position_topic_template( "position_template": "{{ (value | multiply(0.01)) | int }}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_topic_template( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test position by updating status via position template.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "99") @@ -2977,15 +3054,9 @@ async def test_position_via_position_topic_template( assert current_cover_position_position == 50 -async def test_position_via_position_topic_template_json_value( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test position by updating status via position template with a JSON value.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -2997,10 +3068,16 @@ async def test_position_via_position_topic_template_json_value( "position_template": "{{ value_json.Var1 }}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_topic_template_json_value( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test position by updating status via position template with a JSON value.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", '{"Var1": 9, "Var2": 60}') @@ -3023,13 +3100,9 @@ async def test_position_via_position_topic_template_json_value( ) in caplog.text -async def test_position_template_with_entity_id( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test position by updating status via position template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3046,10 +3119,14 @@ async def test_position_template_with_entity_id( {% endif %}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_template_with_entity_id( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test position by updating status via position template.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "10") @@ -3066,13 +3143,9 @@ async def test_position_template_with_entity_id( assert current_cover_position_position == 20 -async def test_position_via_position_topic_template_return_json( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test position by updating status via position template and returning json.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3084,10 +3157,14 @@ async def test_position_via_position_topic_template_return_json( "position_template": '{{ {"position" : value} | tojson }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_topic_template_return_json( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test position by updating status via position template and returning json.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "55") @@ -3097,15 +3174,9 @@ async def test_position_via_position_topic_template_return_json( assert current_cover_position_position == 55 -async def test_position_via_position_topic_template_return_json_warning( - hass: HomeAssistant, - caplog: pytest.LogCaptureFixture, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, -) -> None: - """Test position by updating status via position template returning json without position attribute.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3117,10 +3188,16 @@ async def test_position_via_position_topic_template_return_json_warning( "position_template": '{{ {"pos" : value} | tojson }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_topic_template_return_json_warning( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test position by updating status via position template returning json without position attribute.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "55") @@ -3130,13 +3207,9 @@ async def test_position_via_position_topic_template_return_json_warning( ) -async def test_position_and_tilt_via_position_topic_template_return_json( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test position and tilt by updating the position via position template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3149,10 +3222,14 @@ async def test_position_and_tilt_via_position_topic_template_return_json( {{ {"position" : value, "tilt_position" : (value | int / 2)| int } | tojson }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_and_tilt_via_position_topic_template_return_json( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test position and tilt by updating the position via position template.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "0") @@ -3174,13 +3251,9 @@ async def test_position_and_tilt_via_position_topic_template_return_json( assert current_cover_position == 99 and current_tilt_position == 49 -async def test_position_via_position_topic_template_all_variables( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test position by updating status via position template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3203,10 +3276,14 @@ async def test_position_via_position_topic_template_all_variables( {% endif %}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_topic_template_all_variables( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test position by updating status via position template.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "0") @@ -3222,13 +3299,9 @@ async def test_position_via_position_topic_template_all_variables( assert current_cover_position == 100 -async def test_set_state_via_stopped_state_no_position_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via stopped state when no position topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3244,10 +3317,14 @@ async def test_set_state_via_stopped_state_no_position_topic( "optimistic": False, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_set_state_via_stopped_state_no_position_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via stopped state when no position topic.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "state-topic", "OPEN") @@ -3275,15 +3352,9 @@ async def test_set_state_via_stopped_state_no_position_topic( assert state.state == STATE_CLOSED -async def test_position_via_position_topic_template_return_invalid_json( - hass: HomeAssistant, - caplog: pytest.LogCaptureFixture, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, -) -> None: - """Test position by updating status via position template and returning invalid json.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3295,23 +3366,25 @@ async def test_position_via_position_topic_template_return_invalid_json( "position_template": '{{ {"position" : invalid_json} }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_position_via_position_topic_template_return_invalid_json( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test position by updating status via position template and returning invalid json.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "get-position-topic", "55") assert ("Payload '{'position': Undefined}' is not numeric") in caplog.text -async def test_set_position_topic_without_get_position_topic_error( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture -) -> None: - """Test error when set_position_topic is used without position_topic.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3321,21 +3394,25 @@ async def test_set_position_topic_without_get_position_topic_error( "value_template": "{{100-62}}", } } - }, - ) + } + ], +) +async def test_set_position_topic_without_get_position_topic_error( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test error when set_position_topic is used without position_topic.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( f"'{CONF_SET_POSITION_TOPIC}' must be set together with '{CONF_GET_POSITION_TOPIC}'." ) in caplog.text -async def test_value_template_without_state_topic_error( - hass: HomeAssistant, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test error when value_template is used and state_topic is missing.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3344,20 +3421,25 @@ async def test_value_template_without_state_topic_error( "value_template": "{{100-62}}", } } - }, - ) + } + ], +) +async def test_value_template_without_state_topic_error( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test error when value_template is used and state_topic is missing.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( f"'{CONF_VALUE_TEMPLATE}' must be set together with '{CONF_STATE_TOPIC}'." ) in caplog.text -async def test_position_template_without_position_topic_error( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture -) -> None: - """Test error when position_template is used and position_topic is missing.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3366,22 +3448,26 @@ async def test_position_template_without_position_topic_error( "position_template": "{{100-52}}", } } - }, - ) + } + ], +) +async def test_position_template_without_position_topic_error( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test error when position_template is used and position_topic is missing.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( f"'{CONF_GET_POSITION_TEMPLATE}' must be set together with '{CONF_GET_POSITION_TOPIC}'." in caplog.text ) -async def test_set_position_template_without_set_position_topic( - hass: HomeAssistant, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test error when set_position_template is used and set_position_topic is missing.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3390,21 +3476,26 @@ async def test_set_position_template_without_set_position_topic( "set_position_template": "{{100-42}}", } } - }, - ) + } + ], +) +async def test_set_position_template_without_set_position_topic( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test error when set_position_template is used and set_position_topic is missing.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( f"'{CONF_SET_POSITION_TEMPLATE}' must be set together with '{CONF_SET_POSITION_TOPIC}'." in caplog.text ) -async def test_tilt_command_template_without_tilt_command_topic( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture -) -> None: - """Test error when tilt_command_template is used and tilt_command_topic is missing.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3413,21 +3504,26 @@ async def test_tilt_command_template_without_tilt_command_topic( "tilt_command_template": "{{100-32}}", } } - }, - ) + } + ], +) +async def test_tilt_command_template_without_tilt_command_topic( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test error when tilt_command_template is used and tilt_command_topic is missing.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( f"'{CONF_TILT_COMMAND_TEMPLATE}' must be set together with '{CONF_TILT_COMMAND_TOPIC}'." in caplog.text ) -async def test_tilt_status_template_without_tilt_status_topic_topic( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture -) -> None: - """Test error when tilt_status_template is used and tilt_status_topic is missing.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { cover.DOMAIN: { @@ -3436,8 +3532,17 @@ async def test_tilt_status_template_without_tilt_status_topic_topic( "tilt_status_template": "{{100-22}}", } } - }, - ) + } + ], +) +async def test_tilt_status_template_without_tilt_status_topic_topic( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test error when tilt_status_template is used and tilt_status_topic is missing.""" + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( f"'{CONF_TILT_STATUS_TEMPLATE}' must be set together with '{CONF_TILT_STATUS_TOPIC}'." in caplog.text diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index 41ff43aba87..9882b9102e1 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -32,7 +32,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, @@ -85,30 +84,24 @@ def fan_platform_only(): yield +@pytest.mark.parametrize("hass_config", [{mqtt.DOMAIN: {fan.DOMAIN: {"name": "test"}}}]) async def test_fail_setup_if_no_command_topic( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, ) -> None: """Test if command fails with command topic.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {fan.DOMAIN: {"name": "test"}}}, - ) + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( "Invalid config for [mqtt]: required key not provided @ data['mqtt']['fan'][0]['command_topic']" in caplog.text ) -async def test_controlling_state_via_topic( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -139,10 +132,16 @@ async def test_controlling_state_via_topic( "payload_reset_preset_mode": "rEset_preset_mode", } } - }, - ) - 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, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -224,15 +223,9 @@ async def test_controlling_state_via_topic( assert state.state == STATE_UNKNOWN -async def test_controlling_state_via_topic_with_different_speed_range( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic using an alternate speed range.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: [ @@ -262,10 +255,16 @@ async def test_controlling_state_via_topic_with_different_speed_range( }, ] } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic_with_different_speed_range( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic using an alternate speed range.""" + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "percentage-state-topic1", "100") state = hass.states.get("fan.test1") @@ -288,15 +287,9 @@ async def test_controlling_state_via_topic_with_different_speed_range( caplog.clear() -async def test_controlling_state_via_topic_no_percentage_topics( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic without percentage topics.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -314,10 +307,16 @@ async def test_controlling_state_via_topic_no_percentage_topics( ], } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic_no_percentage_topics( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic without percentage topics.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -347,15 +346,9 @@ async def test_controlling_state_via_topic_no_percentage_topics( caplog.clear() -async def test_controlling_state_via_topic_and_json_message( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic and JSON message (percentage mode).""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -384,10 +377,16 @@ async def test_controlling_state_via_topic_and_json_message( "speed_range_max": 100, } } - }, - ) - 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, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic and JSON message (percentage mode).""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -454,15 +453,9 @@ async def test_controlling_state_via_topic_and_json_message( assert state.attributes.get("preset_mode") is None -async def test_controlling_state_via_topic_and_json_message_shared_topic( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic and JSON message using a shared topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -491,10 +484,16 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic( "speed_range_max": 100, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic_and_json_message_shared_topic( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic and JSON message using a shared topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -544,15 +543,9 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic( caplog.clear() -async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test optimistic mode without state topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -572,10 +565,15 @@ async def test_sending_mqtt_commands_and_optimistic( ], } } - }, - ) - 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 optimistic mode without state topic.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -671,13 +669,9 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) -async def test_sending_mqtt_commands_with_alternate_speed_range( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via topic using an alternate speed range.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: [ @@ -707,10 +701,14 @@ async def test_sending_mqtt_commands_with_alternate_speed_range( }, ] } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_with_alternate_speed_range( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via topic using an alternate speed range.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await common.async_set_percentage(hass, "fan.test1", 0) mqtt_mock.async_publish.assert_called_once_with( @@ -777,15 +775,9 @@ async def test_sending_mqtt_commands_with_alternate_speed_range( assert state.attributes.get(ATTR_ASSUMED_STATE) -async def test_sending_mqtt_commands_and_optimistic_no_legacy( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test optimistic mode without state topic without legacy speed command topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -800,10 +792,16 @@ async def test_sending_mqtt_commands_and_optimistic_no_legacy( ], } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_and_optimistic_no_legacy( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test optimistic mode without state topic without legacy speed command topic.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -911,15 +909,9 @@ async def test_sending_mqtt_commands_and_optimistic_no_legacy( await common.async_turn_on(hass, "fan.test", preset_mode="freaking-high") -async def test_sending_mqtt_command_templates_( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test optimistic mode without state topic without legacy speed command topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -939,10 +931,15 @@ async def test_sending_mqtt_command_templates_( ], } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_command_templates_( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test optimistic mode without state topic without legacy speed command topic.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -1056,15 +1053,9 @@ async def test_sending_mqtt_command_templates_( await common.async_turn_on(hass, "fan.test", preset_mode="low") -async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test optimistic mode without state topic without percentage command topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -1080,10 +1071,15 @@ async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic( ], } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test optimistic mode without state topic without percentage command topic.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -1120,15 +1116,9 @@ async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic( assert state.attributes.get(ATTR_ASSUMED_STATE) -async def test_sending_mqtt_commands_and_explicit_optimistic( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test optimistic mode with state topic and turn on attributes.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -1149,10 +1139,15 @@ async def test_sending_mqtt_commands_and_explicit_optimistic( "optimistic": True, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_and_explicit_optimistic( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, +) -> None: + """Test optimistic mode with state topic and turn on attributes.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -1395,15 +1390,9 @@ async def test_encoding_subscribable_topics( ) -async def test_attributes( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test attributes.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { fan.DOMAIN: { @@ -1418,10 +1407,16 @@ async def test_attributes( ], } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_attributes( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test attributes.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("fan.test") assert state.state == STATE_UNKNOWN @@ -1452,13 +1447,17 @@ async def test_attributes( @pytest.mark.parametrize( - ("name", "config", "success", "features"), + ("name", "hass_config", "success", "features"), [ ( "test1", { - "name": "test1", - "command_topic": "command-topic", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test1", + "command_topic": "command-topic", + } + } }, True, 0, @@ -1466,29 +1465,41 @@ async def test_attributes( ( "test2", { - "name": "test2", - "command_topic": "command-topic", - "oscillation_command_topic": "oscillation-command-topic", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test2", + "command_topic": "command-topic", + "oscillation_command_topic": "oscillation-command-topic", + } + } }, True, - fan.SUPPORT_OSCILLATE, + fan.FanEntityFeature.OSCILLATE, ), ( "test3", { - "name": "test3", - "command_topic": "command-topic", - "percentage_command_topic": "percentage-command-topic", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test3", + "command_topic": "command-topic", + "percentage_command_topic": "percentage-command-topic", + } + } }, True, - fan.SUPPORT_SET_SPEED, + fan.FanEntityFeature.SET_SPEED, ), ( "test4", { - "name": "test4", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test4", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + } + } }, False, None, @@ -1496,100 +1507,136 @@ async def test_attributes( ( "test5", { - "name": "test5", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["eco", "auto"], + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test5", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["eco", "auto"], + } + } }, True, - fan.SUPPORT_PRESET_MODE, + fan.FanEntityFeature.PRESET_MODE, ), ( "test6", { - "name": "test6", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["eco", "smart", "auto"], + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test6", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["eco", "smart", "auto"], + } + } }, True, - fan.SUPPORT_PRESET_MODE, + fan.FanEntityFeature.PRESET_MODE, ), ( "test7", { - "name": "test7", - "command_topic": "command-topic", - "percentage_command_topic": "percentage-command-topic", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test7", + "command_topic": "command-topic", + "percentage_command_topic": "percentage-command-topic", + } + } }, True, - fan.SUPPORT_SET_SPEED, + fan.FanEntityFeature.SET_SPEED, ), ( "test8", { - "name": "test8", - "command_topic": "command-topic", - "oscillation_command_topic": "oscillation-command-topic", - "percentage_command_topic": "percentage-command-topic", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test8", + "command_topic": "command-topic", + "oscillation_command_topic": "oscillation-command-topic", + "percentage_command_topic": "percentage-command-topic", + } + } }, True, - fan.SUPPORT_OSCILLATE | fan.SUPPORT_SET_SPEED, + fan.FanEntityFeature.OSCILLATE | fan.FanEntityFeature.SET_SPEED, ), ( "test9", { - "name": "test9", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["Mode1", "Mode2", "Mode3"], + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test9", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["Mode1", "Mode2", "Mode3"], + } + } }, True, - fan.SUPPORT_PRESET_MODE, + fan.FanEntityFeature.PRESET_MODE, ), ( "test10", { - "name": "test10", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["whoosh", "silent", "auto"], + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test10", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["whoosh", "silent", "auto"], + } + } }, True, - fan.SUPPORT_PRESET_MODE, + fan.FanEntityFeature.PRESET_MODE, ), ( "test11", { - "name": "test11", - "command_topic": "command-topic", - "oscillation_command_topic": "oscillation-command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["Mode1", "Mode2", "Mode3"], + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test11", + "command_topic": "command-topic", + "oscillation_command_topic": "oscillation-command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["Mode1", "Mode2", "Mode3"], + } + } }, True, - fan.SUPPORT_PRESET_MODE | fan.SUPPORT_OSCILLATE, + fan.FanEntityFeature.PRESET_MODE | fan.FanEntityFeature.OSCILLATE, ), ( "test12", { - "name": "test12", - "command_topic": "command-topic", - "percentage_command_topic": "percentage-command-topic", - "speed_range_min": 1, - "speed_range_max": 40, + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test12", + "command_topic": "command-topic", + "percentage_command_topic": "percentage-command-topic", + "speed_range_min": 1, + "speed_range_max": 40, + } + } }, True, - fan.SUPPORT_SET_SPEED, + fan.FanEntityFeature.SET_SPEED, ), ( "test13", { - "name": "test13", - "command_topic": "command-topic", - "percentage_command_topic": "percentage-command-topic", - "speed_range_min": 50, - "speed_range_max": 40, + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test13", + "command_topic": "command-topic", + "percentage_command_topic": "percentage-command-topic", + "speed_range_min": 50, + "speed_range_max": 40, + } + } }, False, None, @@ -1597,11 +1644,15 @@ async def test_attributes( ( "test14", { - "name": "test14", - "command_topic": "command-topic", - "percentage_command_topic": "percentage-command-topic", - "speed_range_min": 0, - "speed_range_max": 40, + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test14", + "command_topic": "command-topic", + "percentage_command_topic": "percentage-command-topic", + "speed_range_min": 0, + "speed_range_max": 40, + } + } }, False, None, @@ -1609,10 +1660,14 @@ async def test_attributes( ( "test15", { - "name": "test7reset_payload_in_preset_modes_a", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["auto", "smart", "normal", "None"], + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test7reset_payload_in_preset_modes_a", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["auto", "smart", "normal", "None"], + } + } }, False, None, @@ -1620,39 +1675,37 @@ async def test_attributes( ( "test16", { - "name": "test16", - "command_topic": "command-topic", - "preset_mode_command_topic": "preset-mode-command-topic", - "preset_modes": ["whoosh", "silent", "auto", "None"], - "payload_reset_preset_mode": "normal", + mqtt.DOMAIN: { + fan.DOMAIN: { + "name": "test16", + "command_topic": "command-topic", + "preset_mode_command_topic": "preset-mode-command-topic", + "preset_modes": ["whoosh", "silent", "auto", "None"], + "payload_reset_preset_mode": "normal", + } + } }, True, - fan.SUPPORT_PRESET_MODE, + fan.FanEntityFeature.PRESET_MODE, ), ], ) async def test_supported_features( hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - name, - config, - success, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + name: str, + success: bool, features, ) -> None: """Test optimistic mode without state topic.""" - - assert ( - await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {fan.DOMAIN: config}} - ) - is success - ) if success: - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get(f"fan.{name}") assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features + return + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() @pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])