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 decoding for amazon emails #1025

Merged
merged 7 commits into from
Dec 4, 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
24 changes: 18 additions & 6 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -1185,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))

Expand Down Expand Up @@ -1277,9 +1284,10 @@ 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).encode('utf-8')
if msg.is_multipart():
email_msg = quopri.decodestring(str(msg.get_payload(0)))
else:
email_msg = quopri.decodestring(str(msg.get_payload()))
except Exception as err:
_LOGGER.debug("Problem decoding email message: %s", str(err))
continue
Expand Down Expand Up @@ -1445,11 +1453,15 @@ def get_items(
order_number.append(found[0])

try:
email_msg = quopri.decodestring(str(msg.get_payload(0)))
if msg.is_multipart():
email_msg = quopri.decodestring(str(msg.get_payload(0)))
else:
email_msg = quopri.decodestring(str(msg.get_payload()))
except Exception as err:
_LOGGER.debug(
"Problem decoding email message: %s", str(err)
)
_LOGGER.error("Unable to process this email. Skipping.")
continue
email_msg = email_msg.decode("utf-8", "ignore")

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.12
python_version = 3.13
show_error_codes = true
ignore_errors = true
follow_imports = silent
Expand Down
1 change: 1 addition & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading