Skip to content

Commit

Permalink
Handle additional date formats in Amazon emails (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Mar 2, 2021
2 parents ea0976f + d94ae26 commit b733625
Show file tree
Hide file tree
Showing 4 changed files with 1,293 additions and 6 deletions.
19 changes: 13 additions & 6 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,15 +962,22 @@ def get_items(
arrive_date = arrive_date[0:3]
arrive_date[2] = arrive_date[2][:2]
arrive_date = " ".join(arrive_date).strip()
time_format = None

_LOGGER.debug("Arrive Date: %s", arrive_date)

if "today" in arrive_date or "tomorrow" in arrive_date:
arrive_date = arrive_date.split(",")[1].strip()
dateobj = datetime.datetime.strptime(
arrive_date, "%B %d"
)
time_format = "%B %d"
elif arrive_date.endswith(","):
arrive_date = arrive_date.rstrip(",")
time_format = "%A, %B %d"
else:
dateobj = datetime.datetime.strptime(
arrive_date, "%A, %B %d"
)
time_format = "%A, %B %d"

dateobj = datetime.datetime.strptime(
arrive_date, time_format
)

if (
dateobj.day == datetime.date.today().day
Expand Down
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,31 @@ def mock_imap_amazon_shipped_it():
yield mock_conn


@pytest.fixture()
def mock_imap_amazon_shipped_alt_timeformat():
""" Mock imap class values. """
with patch(
"custom_components.mail_and_packages.helpers.imaplib"
) as mock_imap_amazon_shipped:
mock_conn = mock.Mock(spec=imaplib.IMAP4_SSL)
mock_imap_amazon_shipped.IMAP4_SSL.return_value = mock_conn

mock_conn.login.return_value = (
"OK",
[b"[email protected] authenticated (Success)"],
)
mock_conn.list.return_value = (
"OK",
[b'(\\HasNoChildren) "/" "INBOX"'],
)
mock_conn.search.return_value = ("OK", [b"1"])
f = open("tests/test_emails/amazon_shipped_alt_timeformat.eml", "r")
email_file = f.read()
mock_conn.fetch.return_value = ("OK", [(b"", email_file.encode("utf-8"))])
mock_conn.select.return_value = ("OK", [])
yield mock_conn


@pytest.fixture()
def mock_imap_amazon_delivered():
""" Mock imap class values. """
Expand Down
Loading

0 comments on commit b733625

Please sign in to comment.