diff --git a/tests/components/melnor/conftest.py b/tests/components/melnor/conftest.py index e030e198787..943018fae88 100644 --- a/tests/components/melnor/conftest.py +++ b/tests/components/melnor/conftest.py @@ -1,6 +1,7 @@ """Tests for the melnor integration.""" from __future__ import annotations +from collections.abc import Generator from unittest.mock import AsyncMock, patch from bleak.backends.device import BLEDevice @@ -141,12 +142,13 @@ def mock_melnor_device(): return device -def patch_async_setup_entry(return_value=True): +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: """Patch async setup entry to return True.""" - return patch( - "homeassistant.components.melnor.async_setup_entry", - return_value=return_value, - ) + with patch( + "homeassistant.components.melnor.async_setup_entry", return_value=True + ) as mock_setup: + yield mock_setup # pylint: disable=dangerous-default-value diff --git a/tests/components/melnor/test_config_flow.py b/tests/components/melnor/test_config_flow.py index e0f7a21bff0..95a67644606 100644 --- a/tests/components/melnor/test_config_flow.py +++ b/tests/components/melnor/test_config_flow.py @@ -1,4 +1,6 @@ """Test the melnor config flow.""" +from unittest.mock import AsyncMock + import pytest import voluptuous as vol @@ -13,15 +15,14 @@ from .conftest import ( FAKE_SERVICE_INFO_1, FAKE_SERVICE_INFO_2, patch_async_discovered_service_info, - patch_async_setup_entry, ) -async def test_user_step_no_devices(hass: HomeAssistant) -> None: +async def test_user_step_no_devices( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test we handle no devices found.""" - with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info( - [] - ): + with patch_async_discovered_service_info([]): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, @@ -30,13 +31,15 @@ async def test_user_step_no_devices(hass: HomeAssistant) -> None: assert result["type"] == FlowResultType.ABORT assert result["reason"] == "no_devices_found" - assert len(mock_setup_entry.mock_calls) == 0 + mock_setup_entry.assert_not_called() -async def test_user_step_discovered_devices(hass: HomeAssistant) -> None: +async def test_user_step_discovered_devices( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test we properly handle device picking.""" - with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info(): + with patch_async_discovered_service_info(): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, @@ -57,13 +60,15 @@ async def test_user_step_discovered_devices(hass: HomeAssistant) -> None: assert result2["type"] == FlowResultType.CREATE_ENTRY assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1} - assert len(mock_setup_entry.mock_calls) == 1 + mock_setup_entry.assert_called_once() -async def test_user_step_with_existing_device(hass: HomeAssistant) -> None: +async def test_user_step_with_existing_device( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test we properly handle device picking.""" - with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info( + with patch_async_discovered_service_info( [FAKE_SERVICE_INFO_1, FAKE_SERVICE_INFO_2] ): # Create the config flow @@ -95,48 +100,50 @@ async def test_user_step_with_existing_device(hass: HomeAssistant) -> None: result["flow_id"], user_input={CONF_ADDRESS: FAKE_ADDRESS_1} ) - assert len(mock_setup_entry.mock_calls) == 0 + mock_setup_entry.assert_not_called() -async def test_bluetooth_discovered(hass: HomeAssistant) -> None: +async def test_bluetooth_discovered( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test we short circuit to config entry creation.""" - with patch_async_setup_entry() as mock_setup_entry: - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_BLUETOOTH}, - data=FAKE_SERVICE_INFO_1, - ) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_BLUETOOTH}, + data=FAKE_SERVICE_INFO_1, + ) - assert result["type"] == FlowResultType.FORM - assert result["step_id"] == "bluetooth_confirm" - assert result["description_placeholders"] == {"name": FAKE_ADDRESS_1} + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "bluetooth_confirm" + assert result["description_placeholders"] == {"name": FAKE_ADDRESS_1} - assert len(mock_setup_entry.mock_calls) == 0 + mock_setup_entry.assert_not_called() -async def test_bluetooth_confirm(hass: HomeAssistant) -> None: +async def test_bluetooth_confirm( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test we short circuit to config entry creation.""" - with patch_async_setup_entry() as mock_setup_entry: - # Create the config flow - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_BLUETOOTH, - "step_id": "bluetooth_confirm", - "user_input": {CONF_MAC: FAKE_ADDRESS_1}, - }, - data=FAKE_SERVICE_INFO_1, - ) + # Create the config flow + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={ + "source": config_entries.SOURCE_BLUETOOTH, + "step_id": "bluetooth_confirm", + "user_input": {CONF_MAC: FAKE_ADDRESS_1}, + }, + data=FAKE_SERVICE_INFO_1, + ) - # Interact with it like a user would - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={} - ) + # Interact with it like a user would + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={} + ) - assert result2["type"] == FlowResultType.CREATE_ENTRY - assert result2["title"] == FAKE_ADDRESS_1 - assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1} + assert result2["type"] == FlowResultType.CREATE_ENTRY + assert result2["title"] == FAKE_ADDRESS_1 + assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1} - assert len(mock_setup_entry.mock_calls) == 1 + mock_setup_entry.assert_called_once()