Re-read last imap_email_content email when no change (#36065)

pull/36155/head
isk0001y 2020-05-25 14:29:26 +02:00 committed by Franck Nijhof
parent 179e601966
commit b6c519e5df
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
1 changed files with 11 additions and 0 deletions

View File

@ -103,6 +103,8 @@ class EmailReader:
if message_data is None:
return None
if message_data[0] is None:
return None
raw_email = message_data[0][1]
email_message = email.message_from_bytes(raw_email)
return email_message
@ -126,13 +128,22 @@ class EmailReader:
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."""