Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imap email parser #13362

Closed
wants to merge 8 commits into from
Closed

Imap email parser #13362

wants to merge 8 commits into from

Conversation

kuzin2006
Copy link
Contributor

Description:

This PR adds email body parsing functionality with use of regular expressions. If option 'body_regex' is present, the named groups of given regexp are exposed as sensor attributes, so further processing of complex email responses is possible.

My case was to process Surveillance DVR's response. The only way to retrieve data is email, in unchangeable format:

Alarm event: Motion DetectEnd
Alarm Begin Time: 2018-03-21 08:17:25
Alarm input channel No.: Corridor(1)
Alarm device name: home-dvr
Sender IP address: 192.168.0.4


Example entry for configuration.yaml :

# Home DVR IMAP
- platform: imap_email_content
  name: home_dvr_alert
  server: localhost
  port: 993
  username: !secret mail_login
  password: !secret mail_pwd
  senders:
    - home-dvr
    - garden-nvr    
  body_regex: "\
    Alarm event: (?P<event>[\\w\\s]+)\\W+\
    Alarm Begin Time: (?P<time>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})\\W+\
    Alarm input channel No\\.: (?P<channel>.+)\\W+\
    Alarm device name: (?P<device>.+)\\W+\
    Sender IP address: (?P<ip>[\\d\\.]+)\
    "

As a result, sensor attributes 'event', 'time', 'channel', 'device', 'ip' appear.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

@kuzin2006 kuzin2006 requested a review from emlove as a code owner March 21, 2018 12:15
@homeassistant homeassistant added platform: media_player.kodi small-pr PRs with less than 30 lines. labels Mar 21, 2018
"""Initialize the sensor."""
self.hass = hass
self._email_reader = email_reader
self._name = name
self._allowed_senders = [sender.upper() for sender in allowed_senders]
self._value_template = value_template
self._body_regex = re.compile(body_regex) if body_regex is not None else None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

@tinloaf
Copy link
Contributor

tinloaf commented Mar 21, 2018

Why does this change the kodi platform?

@kuzin2006
Copy link
Contributor Author

it doesn't. I do not use Git often, so forgot to learn what does rebase mean )))

Copy link
Contributor

@tinloaf tinloaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the two inline comments: This PR does include changes to the file homeassistant/components/media_player/kodi.py - which it shouldn't. Please remove that from this PR.

EmailContentSensor.get_msg_text(email_message))
if parsed_body is not None:
for attr in parsed_body.groupdict().keys():
self._state_attributes[attr] = parsed_body.\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that the names of the groups should just be used as keys here. What if a user has the bright idea of calling his group last_updated? Or friendly_name? I'm not sure in which subtle way this will break HA. How about instead having some ATTR_PARSED_BODY or something, and then set self._state_attributes[ATTR_PARSED_BODY] = {attr : parsed_body.group(attr) for attr in parsed_body.groupdict().keys()}

"""Initialize the sensor."""
self.hass = hass
self._email_reader = email_reader
self._name = name
self._allowed_senders = [sender.upper() for sender in allowed_senders]
self._value_template = value_template
self._body_regex = re.compile(body_regex) if body_regex is not None \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user does specify an invalid regex, this should not simply crash. Either an error should be emitted here, or (even better) validation should take care of this. I don't see a regex validator in config_validation.py, but that shouldn't be too hard.

@balloob
Copy link
Member

balloob commented Mar 23, 2018

We should not add regex support. We should use a value_template, like in the rest of Home Assistant.

@kuzin2006
Copy link
Contributor Author

Well, I agree that system should be as simple as possible, but how could look my case with several values in msg body? Is it possible to use Python string routines for such parsing?

@balloob
Copy link
Member

balloob commented Mar 24, 2018

I think that's too complex to expose via configuration. This should be kept in a custom component or something.

@cmsimike
Copy link
Contributor

@kuzin2006 Can you just email yourself json and use value_template?

@balloob
Copy link
Member

balloob commented Mar 28, 2018

I'm going to close this as it's too complicated to be part of Home Assistant. Consider using a custom component.

@balloob balloob closed this Mar 28, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants