emcniece / ha_imap_attachment

Home Assistant IMAP Attachment Component

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

An error occurs for emails with no subject

cjhesse opened this issue · comments

If my inbox contains emails without subjects, I get this error:

Logger: homeassistant.helpers.entity
Source: custom_components/ha_imap_attachment/sensor.py:218
Integration: ha_imap_attachment
First occurred: 22:00:43 (1 occurrences)
Last logged: 22:00:43

Update for sensor.inbound_text_messages fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 695, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 942, in async_device_update
    await hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/ha_imap_attachment/sensor.py", line 289, in update
    message = EmailContentSensor.get_msg_subject(email_message)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/ha_imap_attachment/sensor.py", line 218, in get_msg_subject
    decoded_header = email.header.decode_header(email_message["Subject"])
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/email/header.py", line 80, in decode_header
    if not ecre.search(header):
           ^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'

Hah, yep it's because https://github.com/emcniece/ha_imap_attachment/blob/master/sensor.py#L218 assumes that the header field will be present. Interesting that if a subject is left blank the header is omitted entirely. The solution will be to try/catch reading the Subject header.

Thanks for the report, I'll patch this asap.