diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 0f5c7986..e96f18c5 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -190,13 +190,12 @@ def image_file_name( image_name = "no_deliveries.jpg" path = f"{hass.config.path()}/{config.get(const.CONF_PATH)}amazon" else: + path = f"{hass.config.path()}/{config.get(const.CONF_PATH)}" if config.get(const.CONF_CUSTOM_IMG): mail_none = config.get(const.CONF_CUSTOM_IMG_FILE) - path, image_name = os.path.split(mail_none) else: mail_none = f"{os.path.dirname(__file__)}/mail_none.gif" - image_name = "mail_none.gif" - path = f"{hass.config.path()}/{config.get(const.CONF_PATH)}" + not_used, image_name = os.path.split(mail_none) # Path check path_check = os.path.exists(path) @@ -236,13 +235,14 @@ def image_file_name( image_name = f"{str(uuid.uuid4())}{ext}" else: image_name = file - _LOGGER.debug("Image Name: %s", image_name) # If we find no images in the image directory generate a new filename if image_name in mail_none: image_name = f"{str(uuid.uuid4())}{ext}" + _LOGGER.debug("Image Name: %s", image_name) # Insert place holder image + _LOGGER.debug("Copying %s to %s", mail_none, os.path.join(path, image_name)) copyfile(mail_none, os.path.join(path, image_name)) return image_name diff --git a/tests/test_camera.py b/tests/test_camera.py index f7f51ffd..47b1a357 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -86,6 +86,12 @@ async def test_update_file_path( assert state.attributes.get("friendly_name") == "Mail USPS Camera" assert "images/test.gif" in state.attributes.get("file_path") + service_data = {"entity_id": "camera.mail_usps_camera"} + await hass.services.async_call(DOMAIN, "update_image", service_data) + await hass.async_block_till_done() + assert "images/test.gif" in state.attributes.get("file_path") + assert "Custom No Mail: images/test.gif" in caplog.text + # TODO: Add process_mail and check camera file path diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 033592e3..1d692b74 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -35,6 +35,7 @@ FAKE_CONFIG_DATA_BAD, FAKE_CONFIG_DATA_CORRECTED, FAKE_CONFIG_DATA_CORRECTED_EXTERNAL, + FAKE_CONFIG_DATA_CUSTOM_IMG, FAKE_CONFIG_DATA_EXTERNAL, FAKE_CONFIG_DATA_NO_RND, ) @@ -916,7 +917,7 @@ async def test_image_file_name_amazon( async def test_image_file_name( - hass, mock_listdir_nogif, mock_getctime_today, mock_hash_file, caplog + hass, mock_listdir_nogif, mock_getctime_today, mock_hash_file, mock_copyfile, caplog ): config = FAKE_CONFIG_DATA_CORRECTED @@ -927,6 +928,14 @@ async def test_image_file_name( assert ".gif" in result assert not result == "mail_none.gif" + # Test custom image settings + config = FAKE_CONFIG_DATA_CUSTOM_IMG + result = image_file_name(hass, config) + assert ".gif" in result + assert not result == "mail_none.gif" + assert len(mock_copyfile.mock_calls) == 2 + assert "Copying images/test.gif to" in caplog.text + async def test_amazon_exception(hass, mock_imap_amazon_exception, caplog): result = amazon_exception(mock_imap_amazon_exception, ['""'])