Mail and Packages: Custom component for UPS, FEDEX, and USPS

Mail and Packages is an integration that creates sensors to show mail and package activity from USPS, UPS, and FEDEX. It does so by looking for known in transit and delivery email subject lines from the difference package vendors notification emails.

Your HASS instance will log in to the mail you provide and search for email from the known email address from the shipping vendors and the known subject lines indicating mail or package will be delivered or have been delivered current day.

For USPS Inform Delivery, the image of the mail items are extracted into an animated gif.

It is only capable showing current day activity, mail and packages that are in process of being delivered current day or have been delivered current day.

The integration has come a long way from my original post. Thanks to @firstof9 breathing new life into the project and contributing heavily to the code.

Read more and submit issues in the repository

Previous
As a solution to component such as USPS, UPS, and FEDEX breaking or creating connection issues (USPS). I have had a script cobbled together to watch an email inbox for UPS, USPS, and FEDEX notifications emails and then MQTT the information to HASS to track the number of mail pieces and packages to expect.

https://github.com/moralmunky/HomeAssistant_Mail_Card

There are other solution that do similar things with added software outside of HASS. I wanted to have something that is all contained inside HASS for the normal user. I have been, through trial and error, attempting to transform this into a platform instead of an external script. I have everything working except importing settings from the configuration yaml.

https://github.com/moralmunky/Home-Assistant-Mail-And-Packages

I would like to set up the configuration like this

mail_and_packages:
host: !secret mail_host
port: !secret mail_port
username: !secret mail_username
password: !secret mail_password
folder: ā€˜Inboxā€™
image_output_path: !secret mail_image_output_path

These options are currently hardcoded in the sensor.py file until I can figure out how to import configuration options. I have tried to mimic how other platforms that accept configuration options are set up but I have not been successful. You can see some the attempted commented out in the file. Essential the platform create a sensor for each delivery service and each state checking for the services email address and associates subject.

Any help would be appreciated to guide mean the right direction and even code review :slight_smile:

12 Likes

Iā€™d love to get this running in hassio!

@moralmunky This is awesome !! I was getting ready to install youā€™re previous version with MQTT but then found this one. Works great !
By the way, I found this other component https://github.com/custom-components/usps_mail/ that seems to have the configuration youā€™re looking for to pass the config options, although it is for hassio so not entirely sure if it will work for you. Also interesting is that it seems to use the builtin camera component instead of relying imagemagick.

@jonathanweinberg I got it to work on hassio, took me a little while to figure out but should be a quick fix. I was having issues with the Packages component, so I just broke down the mail_package.yaml and put everything in there in the respective spot. I got some errors with the customize items that didnā€™t have any customization, so I just put in some friendly names.
For the sensor.py I had the hardest time figuring out why I didnā€™t get the gif created and couldnā€™t figure out where the issue was as I had installed imagemagick component in Homeassistant container. I put some logger code in sensor.py and thought the problem was with the os,system command. Tried to use subprocess.run instead but didnā€™t get anywhere. Then figured out that homeassistant container is not used to run the python code. Installed imagemagic on the supervisor container with also no change. I then just added os.system(ā€˜apk add imagemagickā€™) before the gif maker command and it started working.

I just setup everything as it is today here: https://github.com/moralmunky/Home-Assistant-Mail-And-Packages. Weā€™ll see tomorrow when I receive emails what happens :wink:

There is one bug with path to lovelace cardā€¦double check path www/mail_and_packages/.

Regarding configuration.yaml, @moralmunky, Iā€™ve had success so far in creating a couple of custom components that take it configuration data fields, lists, etc. Iā€™d love to help!

1 Like

Thanks for the update. I really liked your old version, but the UPS hasnā€™t worked in a while for me. This new version is good and I can remove that extra service now. Thanks for your work.

I wound up removing an extra ā€œmail_and_packages:ā€ section in mail_package.yaml

I have not seen in update anything yet, but hopefully will on Mondayā€¦

FYI: For the sensor

I think you need to define a sensor in configuration.yaml

- platform: mail_and_packages
    host: !secret mail_host
    ...

Then in sensor.py you will be able access the host variable. I have only done this with a simple custom sensor. I have never used an includes/packages like you have, so perhaps it wonā€™t work the way you have it setup now.

Should be doable from the main configuration.yaml file example:

mail_and_packages:
  host: !secret mail_host
  username: !secret mail_user
...

Are all you guys using Gmail? From what I gather my issue is with authentication and Iā€™m unsure what port to use for gmail. Any thoughts?

Yes Gmail, use port 993 and setup an app password in your google account security section specifically for this.

1 Like

Yea thatā€™s what I did. I canā€™t figure out what is wrong. It just wonā€™t read from the email.

Sat Jul 06 2019 20:54:57 GMT-0400 (Eastern Daylight Time)
Error while setting up platform mail_and_packages
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 126, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 85, in setup_platform
    add_devices([MailCheck(),USPS_Mail(),USPS_Delivering(),USPS_Delivered(),UPS_Delivering(),UPS_Delivered(),FEDEX_Delivering(),FEDEX_Delivered()])
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 137, in __init__
    self.update()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/__init__.py", line 224, in wrapper
    result = method(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 161, in update
    self._state = get_mails(account)
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 402, in get_mails
    fp = open(filepath, 'wb')
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/.homeassistant/www/mail_and_packages/1033918669-007.jpg'

This is what I get as errors

Itā€™s reading your email fine, your problem is with the path you specified:

1 Like

What is that file? It says .jpg, but thatā€™s not in the directory or in github.

@codypet2002 in the instructions, part ā€˜Setup custom_components/mail_and_Packages/sensor.py fileā€™, the last one is ā€˜Line 27 The full path to the www/mail_and_packages/ folderā€™. You did not do this as the folder path in the error message has ā€˜path/to/ā€™ in it. The py file will download the mail images from the USPS informed delivery email to a temporary folder in order to combine and convert them to a single animated GIF. Please double check you have completed the required steps from the GitHub page.

1 Like

Thanks @jaaem. Iā€™m having trouble wrapping my head around the code difference between a platform, which I think I currently have it, and a single sensor. Looking at other components like weather you should be able to pass variables.

@firstof9 Yes, this is my intention with the example yaml configuration but I havenā€™t figured out the right python code to retrieve the variables.

1 Like

Oh I had done that before and thought that was my problem using /local/ previously and it still not working. I changed it back to /path/to because I thought that was the proper way to path to the folder. Even now updating that again to a full path, Iā€™m still getting the same error however.

My latest USPS digest has a package coming tomorrow, so it should show up.

1 Like

@moralmunky Iā€™ve made a little progress but I am still a novice with python:

At the moment it works via a platform like @jaaem suggested in sensors configuration.

1 Like

@ralfaro

Looks like this component is causing asyncio issues and the event loop eventually halts. What is your approach to testing thisā€¦perhaps without home assistant? Is there a driver example?

Moving the comments to the thread for the component.

Yes, there is an asyncio issue. I have added a test.py to the GitHub repository that can be run in the command line outside of HomeAssistant that only checks the email and reports back the findings.

Because of my limited knowledge my ā€˜methodā€™ of testing has been searching and throwing code at it :smile: Iā€™m open to suggestions.

1 Like

Thanks for the test scriptā€¦Iā€™ll begin testing my issues, asyncio, and get on to the yaml configuration you had struggles with.

1 Like

You may be experiencing the asyncio issue. In my system it worked for awhile and then the HASS would halt and I had to restart it. I have been busy relocating so I am still on 9.3.1 but have removed the component from my system until I had time to look it over. Help from @ralfaro seems promising.

I see the changed in your add_config branch. Great to see you got that working.

1 Like