diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 50685aa0..8aae808c 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -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 @@ -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 = {} @@ -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)) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index dba743e3..47848175 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -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 @@ -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