From 10f98029ca45fa622bf5c7b8e885363669ba3bc1 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Fri, 22 Nov 2024 07:24:42 -0700 Subject: [PATCH 1/7] fix: adjust decoding for amazon emails --- custom_components/mail_and_packages/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index c9475eda..8e5ba105 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1278,7 +1278,7 @@ def amazon_hub(account: Type[imaplib.IMAP4_SSL], fwds: Optional[str] = None) -> # Get combo number from message body try: email_msg = quopri.decodestring( - str(msg.get_payload(0)) + msg.get_payload(0) ) # msg.get_payload(0).encode('utf-8') except Exception as err: _LOGGER.debug("Problem decoding email message: %s", str(err)) @@ -1445,7 +1445,7 @@ def get_items( order_number.append(found[0]) try: - email_msg = quopri.decodestring(str(msg.get_payload(0))) + email_msg = quopri.decodestring(msg.get_payload(0)) except Exception as err: _LOGGER.debug( "Problem decoding email message: %s", str(err) From ae0862975ed5ef887e79155f8d889f92447b24e1 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Mon, 25 Nov 2024 10:48:29 -0700 Subject: [PATCH 2/7] adjust ssl context --- custom_components/mail_and_packages/helpers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 8e5ba105..3a0d70ea 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -127,7 +127,10 @@ async def _test_login( context = ssl.client_context() account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) elif security == "startTLS": - context = ssl.client_context() + if not verify: + context = ssl.client_context_no_verify() + else: + context = ssl.client_context() account = imaplib.IMAP4(host=host, port=port) account.starttls(context) else: @@ -464,7 +467,10 @@ def login( context = ssl.client_context() account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) elif security == "startTLS": - context = ssl.client_context() + if not verify: + context = ssl.client_context_no_verify() + else: + context = ssl.client_context() account = imaplib.IMAP4(host=host, port=port) account.starttls(context) else: From 41400d13b001f9b8f67ea97d1e9a7b9c5489ff43 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Fri, 29 Nov 2024 20:22:44 -0700 Subject: [PATCH 3/7] attempt alternative processing method --- custom_components/mail_and_packages/helpers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 3a0d70ea..b0a7ba8b 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1456,7 +1456,11 @@ def get_items( _LOGGER.debug( "Problem decoding email message: %s", str(err) ) - continue + _LOGGER.debug("Attempting alternative method.") + email_msg = msg.get_payload(0, decode=True) + if email_msg is None: + _LOGGER.error("Unable to process this email. Skipping.") + continue email_msg = email_msg.decode("utf-8", "ignore") # Check message body for order number From 509e756ef772bf81ed04875cd319b398e5580243 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Tue, 3 Dec 2024 10:00:40 -0700 Subject: [PATCH 4/7] attempt to get_payload with no value --- custom_components/mail_and_packages/helpers.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index b0a7ba8b..5dc27156 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1451,16 +1451,16 @@ def get_items( order_number.append(found[0]) try: - email_msg = quopri.decodestring(msg.get_payload(0)) + if msg.is_multipart(): + email_msg = quopri.decodestring(msg.get_payload(0)) + else: + email_msg = quopri.decodestring(msg.get_payload()) except Exception as err: _LOGGER.debug( "Problem decoding email message: %s", str(err) ) - _LOGGER.debug("Attempting alternative method.") - email_msg = msg.get_payload(0, decode=True) - if email_msg is None: - _LOGGER.error("Unable to process this email. Skipping.") - continue + _LOGGER.error("Unable to process this email. Skipping.") + continue email_msg = email_msg.decode("utf-8", "ignore") # Check message body for order number From c4e652b6aec6cd72593a171ad0e67fa648e2c340 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Wed, 4 Dec 2024 08:35:40 -0700 Subject: [PATCH 5/7] adjust processing for amazon hub emails as well --- custom_components/mail_and_packages/helpers.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 5dc27156..8ca1cad1 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1283,9 +1283,10 @@ def amazon_hub(account: Type[imaplib.IMAP4_SSL], fwds: Optional[str] = None) -> # Get combo number from message body try: - email_msg = quopri.decodestring( - msg.get_payload(0) - ) # msg.get_payload(0).encode('utf-8') + if msg.is_multipart(): + email_msg = quopri.decodestring(msg.get_payload(0)) + else: + email_msg = quopri.decodestring(msg.get_payload()) except Exception as err: _LOGGER.debug("Problem decoding email message: %s", str(err)) continue From e7959fa8d7ff651077943835ec22c2bb279aa40d Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Wed, 4 Dec 2024 08:43:24 -0700 Subject: [PATCH 6/7] formatting --- custom_components/mail_and_packages/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 8ca1cad1..1335d1c7 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -469,7 +469,7 @@ def login( elif security == "startTLS": if not verify: context = ssl.client_context_no_verify() - else: + else: context = ssl.client_context() account = imaplib.IMAP4(host=host, port=port) account.starttls(context) From c4cb7c5cfde8730ee8ef99bbf2d674f2daef6c00 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Wed, 4 Dec 2024 09:24:12 -0700 Subject: [PATCH 7/7] update tests to py3.13 --- custom_components/mail_and_packages/helpers.py | 9 +++++---- setup.cfg | 2 +- tests/test_helpers.py | 1 + tox.ini | 5 +++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 1335d1c7..29f06951 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1191,6 +1191,7 @@ def get_amazon_image( break if img_url is not None: + _LOGGER.debug("Attempting to download Amazon image.") # Download the image we found hass.add_job(download_img(hass, img_url, image_path, image_name)) @@ -1284,9 +1285,9 @@ def amazon_hub(account: Type[imaplib.IMAP4_SSL], fwds: Optional[str] = None) -> # Get combo number from message body try: if msg.is_multipart(): - email_msg = quopri.decodestring(msg.get_payload(0)) + email_msg = quopri.decodestring(str(msg.get_payload(0))) else: - email_msg = quopri.decodestring(msg.get_payload()) + email_msg = quopri.decodestring(str(msg.get_payload())) except Exception as err: _LOGGER.debug("Problem decoding email message: %s", str(err)) continue @@ -1453,9 +1454,9 @@ def get_items( try: if msg.is_multipart(): - email_msg = quopri.decodestring(msg.get_payload(0)) + email_msg = quopri.decodestring(str(msg.get_payload(0))) else: - email_msg = quopri.decodestring(msg.get_payload()) + email_msg = quopri.decodestring(str(msg.get_payload())) except Exception as err: _LOGGER.debug( "Problem decoding email message: %s", str(err) diff --git a/setup.cfg b/setup.cfg index 67eac7b3..7b4680a2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [mypy] -python_version = 3.12 +python_version = 3.13 show_error_codes = true ignore_errors = true follow_imports = silent diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 42d4bcf3..2f565eb5 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -818,6 +818,7 @@ async def test_amazon_search_delivered( "testfilename.jpg", "amazon.com", ) + await hass.async_block_till_done() assert ( "Amazon email search address: ['order-update@amazon.com', 'update-bestelling@amazon.com', 'versandbestaetigung@amazon.com']" in caplog.text diff --git a/tox.ini b/tox.ini index 8b48c3da..edfbc091 100644 --- a/tox.ini +++ b/tox.ini @@ -1,12 +1,13 @@ [tox] skipsdist = true -envlist = py312, lint, mypy +envlist = py312, py313, lint, mypy skip_missing_interpreters = True ignore_basepython_conflict = True [gh-actions] python = - 3.12: py312, lint, mypy + 3.12: py312 + 3.13: py313, lint, mypy [testenv] pip_version = pip>=21.0,<22.1