Skip to content

Commit

Permalink
fix: adjust amazon domains and forwarding parser function
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Nov 5, 2024
1 parent 52340d7 commit e9511af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
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
1 change: 0 additions & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ disable=
too-many-lines,
too-many-locals,
abstract-method,
too-many-positional-arguments,

[REFACTORING]

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

0 comments on commit e9511af

Please sign in to comment.