emcniece / ha_imap_attachment

Home Assistant IMAP Attachment Component
7 stars 3 forks source link

Constantly pulling email from server #10

Open nmsoares opened 12 months ago

nmsoares commented 12 months ago

Why is the sensor constantly pulling email from server and writing it to the specified folder every minute ? Is this configurable to pull just once ? I want to delete the file and the sensor keeps grabbing it back and writing it in the folder again and again

emcniece commented 12 months ago

It's using the default sensor scan interval, and I agree that seems excessive for writing files. It might be ok for checking the inbox, so maybe we could improve the sensor by not re-writing the file after it has been placed on disk the first time.

You might be able to slow the sensor down by adding scan_interval, though I haven't tested this myself:

sensor:
  - platform: imap_attachment
     scan_interval: 300   # in seconds

If you try that, let me know if it works 😄

nmsoares commented 12 months ago

Let me try!

nmsoares commented 12 months ago

Yep you got it ! It works by using the scan_interval setting! thanks for the tip!

emcniece commented 12 months ago

Cool, thanks for confirming. I don't think it'll actually solve the problem of constantly writing files, I'd guess it will write the attachment to disk again when it runs next time.

Sounds like you could set the scan_interval to a huge number, like 10 years: https://community.home-assistant.io/t/what-is-the-maximum-scan-interval-that-i-can-use/81384/3

If you did that, maybe the sensor could be triggered by an automation. That might allow better control over having the attachment file present or deleted.

nmsoares commented 12 months ago

How can I trigger the sensor with an automation ? Can you give some pointers ? Thanks!

emcniece commented 12 months ago

Looks like it's possible (https://community.home-assistant.io/t/add-easier-solution-for-loops-in-automations-scripts/108508/47) but it gets messy fast. An automation that triggers homeassistant.update_entity might work.

The cleaner option would be to split this integration into 3 pieces: an action that reads the IMAP inbox, an action that fetches the attachments, and a sensor that runs these actions on scan_interval. With this configuration the sensor could be disabled outright, and the inbox_read and attachment_fetch actions could be executed at any time by any other entity or automation.

That's a significant change to the integration, but it seems like a good direction to head in!