Fix imap_email_content unknown status and replaying stale states#89563
Conversation
|
|
||
| if self.sender_allowed(email_message): | ||
| message = EmailContentSensor.get_msg_subject(email_message) | ||
|
|
||
| if self._value_template is not None: | ||
| message = self.render_template(email_message) | ||
|
|
||
| self._message = message | ||
| self._state_attributes = { | ||
| ATTR_FROM: EmailContentSensor.get_msg_sender(email_message), | ||
| ATTR_SUBJECT: EmailContentSensor.get_msg_subject(email_message), | ||
| ATTR_DATE: email_message["Date"], | ||
| ATTR_BODY: EmailContentSensor.get_msg_text(email_message), | ||
| } | ||
|
|
There was a problem hiding this comment.
It seems like this should only happen if we are going to break out of the loop instead of processing it and than throwing it away when we hit the next one?
There was a problem hiding this comment.
We do not know if an unseen mail has an allowed sender unless we process it. The current code base uses a custom EmailReader class as backend.
The code in dev needs several update iterations before it reaches the last one.
The improvement here is to iterate the messages until we reach the last one in a single update cycle.
| ATTR_BODY: EmailContentSensor.get_msg_text(email_message), | ||
| } | ||
|
|
||
| if self._last_id == self._email_reader.last_unread_id: |
There was a problem hiding this comment.
How can this conditional ever be false?
There was a problem hiding this comment.
I'll have a look at that later today.
There was a problem hiding this comment.
self._email_reader.last_id is the last message processed, self._email_reader.last_unread_id is the last message to process. The condition is false when we are at startup and we searched all messages for the last 2 days. There might be more messages that match the IMAP search, but we do not known if the sender matches. When the last message is processed the last state set will be come the new state. If the last message processed is not the last message to process, then we read a new message until we reach the last one. To simulate this, place to matching new message in the mailbox and startup home assistant.
There was a problem hiding this comment.
Makes sense now that I'm back at my desktop. Apparently I just needed a slightly larger monitor to see that 😉
|
Working as expected. Please see #89563 (comment) |
|
Thanks @jbouwh |

Breaking change
Only for the last valid mail the state is updated at startup.
During startup emails received since
yesterdayare examined, this was sincetoday.The state will not become
unknownif no new messages are received after a previous valid state, unless home_assistant is restarted.Proposed change
This PR changes the startup behaviour for the
imap_email_contentsensor platform.todayandyesterday, this avoidsunknownstates when the date is changing and there is not an update email to process yet.Note
The
imap_email_contentsensor misses an option that allows to set wat retain period the last state message should have. Now this is min 1 day, and max 2 days.To allow further improvements this integration should be updated to be set up using config flow. A PR was opened for this : #89707.
Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.To help with the load of incoming pull requests: