diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 3d7ae3de..e0e7ab37 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1163,10 +1163,12 @@ def get_amazon_image( if img_url is not None: # Download the image we found - hass.add_job(download_img(img_url, image_path, image_name)) + hass.add_job(download_img(hass, img_url, image_path, image_name)) -async def download_img(img_url: str, img_path: str, img_name: str) -> None: +async def download_img( + hass: HomeAssistant, img_url: str, img_path: str, img_name: str +) -> None: """Download image from url.""" img_path = f"{img_path}amazon/" filepath = f"{img_path}{img_name}" @@ -1181,9 +1183,9 @@ async def download_img(img_url: str, img_path: str, img_name: str) -> None: if "image" in content_type: data = await resp.read() _LOGGER.debug("Downloading image to: %s", filepath) - with open(filepath, "wb") as the_file: - the_file.write(data) - _LOGGER.debug("Amazon image downloaded") + the_file = await hass.async_add_executor_job(open, filepath, "wb") + the_file.write(data) + _LOGGER.debug("Amazon image downloaded") def _process_amazon_forwards(email_list: Union[List[str], None]) -> list: diff --git a/tests/test_helpers.py b/tests/test_helpers.py index a727abb9..c52d87b0 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -948,6 +948,7 @@ async def test_usps_exception(hass, mock_imap_usps_exception): @pytest.mark.asyncio async def test_download_img( + hass, aioclient_mock, mock_osremove, mock_osmakedir, @@ -960,6 +961,7 @@ async def test_download_img( m_open = mock_open() with patch("builtins.open", m_open, create=True): await download_img( + hass, "http://fake.website.com/not/a/real/website/image.jpg", "/fake/directory/", "testfilename.jpg", @@ -971,10 +973,11 @@ async def test_download_img( @pytest.mark.asyncio -async def test_download_img_error(aioclient_mock_error, caplog): +async def test_download_img_error(hass, aioclient_mock_error, caplog): m_open = mock_open() with patch("builtins.open", m_open, create=True): await download_img( + hass, "http://fake.website.com/not/a/real/website/image.jpg", "/fake/directory/", "testfilename.jpg",