This is cool! If you were to extend to search capabilities, you could trigger events based on incoming emails. For example, I currently use IFTTT to pick up emails sent by my alarm system to trigger events using the Maker channel. Unfortunately, IFTTT is a bit slow (Alarm sends email that the house was set to Stay mode, IFTTT picks up the email ~ 5 minutes later, triggers the Night Mode scene on HA). This sensor could really speed things up!
I have no idea! I don’t have a google apps account handy to test against, worth a shot! If it throws some error paste it and we can probably figure it out.
That bit there is searching for unread emails, could make an optional config that searches for something else.
Actually for your case actually consuming the email would be ideal which would be quite a bit of extra python/imap code. You’d want to search for alarm emails → inspect the most recent and verify that it is set/unset/etc, then trigger night mode and mark it as read.
To make this into a “real” component, I’d actually recommend just going with an imap sensor and providing easy documentation for setting up gmail accounts. I could see this being pretty nice to have for non-gmail e-mail accounts too.
Maybe also fire an event (with to/from/subject) every time a new e-mail is received?
Move imap.gmail.com and 993 to the configuration (meaning make it configurable), update the naming (perhaps just imap), and check in def setup_platform() if the given configuration values are valid (if not, don’t setup the platform). The configuration values can be check with [PLATFORM_SCHEMA](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/sensor/google_travel_time.py#L41) and I think that this sensor platform is ready for submitting as a PR because it reached the the level of a Minimum Viable Product (MVP).
I am working on a PR of this, not sure what to do as far as re-log strategies goes. In the initial component it just skips updating, an alternative I am testing is:
def update(self):
"""Check the number of unread emails."""
try:
self._connection.select()
self._unread_count = len(self._connection.search(
None,'UnSeen')[1][0].split())
except imaplib.abort:
_LOGGER.info("Connection to {} lost, attempting to reconnect"
.format(self._server))
try:
self._login(self._user, self._password)
self._update()
except:
_lOGGER.error("Failed to reconnect.")
I think you’d need to use something like self._connection = self._login(self._user, self._password), right? self._login didn’t look like it saves the new connection it creates, just returns it.
For your actual question though:
In your last except, I would try to keep track of how many attempts (in a row) have failed, so if the last X attempts failed, skip updating altogether and log an error/warning. Or if the last X attempts failed, wait for X minutes before trying again. I doubt either of these would be needed for a MVP, but it’d be nice to keep someone’s account from getting locked out if they changed the password for example.
I really like the idea of having a Gmail Sensor like this. Just a quick remark regarding utilization of IMAP: Gmail also provides a RESTful API, which could be used to query the unread count using simple HTTPS-Requests. Read more about & try it out here: https://developers.google.com/gmail/api/v1/reference/users/labels/get