Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust amazon domains and forwarding parser function #989

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,17 +1216,20 @@ async def download_img(
_LOGGER.debug("Amazon image downloaded")


def _process_amazon_forwards(email_list: Union[List[str], None]) -> list:
def _process_amazon_forwards(email_list: str | list | None) -> list:
"""Process amazon forward emails.

Returns list of email addresses
"""
result = []
if email_list:
if email_list is not None:
if not isinstance(email_list, list):
email_list = email_list.split()
for fwd in email_list:
if fwd and fwd != '""' and fwd not in result:
result.append(fwd)

_LOGGER.debug("Processed forwards: %s", result)
return result


Expand All @@ -1235,7 +1238,8 @@ def amazon_hub(account: Type[imaplib.IMAP4_SSL], fwds: Optional[str] = None) ->

Returns dict of sensor data
"""
email_addresses = _process_amazon_forwards(fwds)
email_addresses = []
email_addresses.extend(_process_amazon_forwards(fwds))
body_regex = AMAZON_HUB_BODY
subject_regex = AMAZON_HUB_SUBJECT_SEARCH
info = {}
Expand Down Expand Up @@ -1388,9 +1392,10 @@ def get_items(
tfmt = past_date.strftime("%d-%b-%Y")
deliveries_today = []
order_number = []
domains = _process_amazon_forwards(fwds)
domains = []
domains.extend(_process_amazon_forwards(fwds))
the_domain = the_domain.split()
domains.append(the_domain)
domains.extend(the_domain)

_LOGGER.debug("Amazon email list: %s", str(domains))

Expand Down
12 changes: 10 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,12 @@ async def test_royal_out_for_delivery(hass, mock_imap_royal_out_for_delivery):

@freeze_time("2020-09-11")
@pytest.mark.asyncio
async def test_amazon_shipped_count(hass, mock_imap_amazon_shipped):
async def test_amazon_shipped_count(hass, mock_imap_amazon_shipped, caplog):
result = get_items(mock_imap_amazon_shipped, "count", the_domain="amazon.com")
assert (
"Amazon email search address: ['shipment-tracking@amazon.com', 'conferma-spedizione@amazon.com', 'confirmar-envio@amazon.com', 'versandbestaetigung@amazon.com', 'confirmation-commande@amazon.com', 'verzending-volgen@amazon.com', 'update-bestelling@amazon.com']"
in caplog.text
)
assert result == 1


Expand Down Expand Up @@ -1136,6 +1140,10 @@ async def test_email_search_none(mock_imap_search_error_none, caplog):

@pytest.mark.asyncio
async def test_amazon_shipped_fwd(hass, mock_imap_amazon_fwd, caplog):
result = get_items(mock_imap_amazon_fwd, "order", the_domain="amazon.com")
result = get_items(mock_imap_amazon_fwd, "order", fwds="testuser@test.com" ,the_domain="amazon.com")
assert (
"Amazon email list: ['testuser@test.com', 'amazon.com']"
in caplog.text
)
assert result == ["123-1234567-1234567"]
assert "First pass: Tuesday, January 11" in caplog.text
Loading