Skip to content

Commit

Permalink
fix: adjust image_file_name function (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Jun 16, 2021
2 parents 31fed6f + 76d8c97 commit a980d99
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
8 changes: 4 additions & 4 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 10 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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

Expand All @@ -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, ['""'])
Expand Down

0 comments on commit a980d99

Please sign in to comment.