Skip to content
Merged
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
11 changes: 11 additions & 0 deletions homeassistant/components/imap_email_content/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def _fetch_message(self, message_uid):

if message_data is None:
return None
if message_data[0] is None:
Comment thread
MartinHjelmare marked this conversation as resolved.
return None
raw_email = message_data[0][1]
email_message = email.message_from_bytes(raw_email)
return email_message
Expand All @@ -126,13 +128,22 @@ def read_next(self):
self._last_id = int(message_uid)
return self._fetch_message(message_uid)

return self._fetch_message(str(self._last_id))

except imaplib.IMAP4.error:
_LOGGER.info("Connection to %s lost, attempting to reconnect", self._server)
try:
self.connect()
_LOGGER.info(
"Reconnect to %s succeeded, trying last message", self._server
)
if self._last_id is not None:
return self._fetch_message(str(self._last_id))
except imaplib.IMAP4.error:
_LOGGER.error("Failed to reconnect")

return None


class EmailContentSensor(Entity):
"""Representation of an EMail sensor."""
Expand Down