Total Noob: Created mail platform need to add configuration options

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.

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.

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:

2 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.

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:

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.

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.

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.

@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.

@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.