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

You are running 0.1.5?

Yes, I have also tried the beta version, same error.

Can you turn on debugging please?

In configuration.yaml example:

logger:
  default: warning
   logs:
     custom_components.mail_and_packages.sensor: debug

Here is what the log is showing with debug

2020-04-26 10:48:44 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Config scan interval: 0:05:00
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find Informed Delivery mail
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Cleaning up image directory: /home/homeassistant/.homeassistant/images/mail_and_packages/
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Informed Delivery email found processing...
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Removing duplicate images.
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Removing USPS announcement images.
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Image Count: 0
2020-04-26 10:48:45 INFO (MainThread) [custom_components.mail_and_packages.sensor] No mail found.
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Copying nomail gif
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject Item Delivered
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject Expected Delivery on
2020-04-26 10:48:45 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject Your UPS Package was delivered
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject UPS Update: Package Scheduled for Delivery Today
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject UPS Update: Follow Your Delivery on a Live Map
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject Your package has been delivered
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject Delivery scheduled for today
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find mail from [email protected] with subject Your package is scheduled for delivery today
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Attempting to find Amazon email with item list ...
2020-04-26 10:48:46 DEBUG (MainThread) [custom_components.mail_and_packages.sensor] Amazon emails found: 5
2020-04-26 10:48:47 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up mail_and_packages platform for sensor
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 69, in async_setup_entry
    async_add_entities([MailCheck(data), USPS_Mail(hass, data),
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 185, in __init__
    self.update()
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 225, in update
    self.data.update()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/__init__.py", line 240, in wrapper
    result = method(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 161, in update
    self._amazon_packages = get_items(account, "count")
  File "/home/homeassistant/.homeassistant/custom_components/mail_and_packages/sensor.py", line 1248, in get_items
    email_msg = str(msg.get_payload(0))
  File "/usr/lib/python3.7/email/message.py", line 257, in get_payload
    raise TypeError('Expected list, got %s' % type(self._payload))
TypeError: Expected list, got <class 'str'>

Is it the amazon sensor causing the issue? What is supposed to be in the item list?

Yes that’s the Amazon bit.
The error is when it’s trying to read the email. Doesn’t make sense.

Can you get a screenshot of your System Health page on the Info tab?

image

The emails are not forwarded emails?

No, they come in from Amazon.com [email protected]
with subject Your Amazon.com order #XXXX

So I changed the days back in the sensor code to 1

past_date = datetime.date.today() - datetime.timedelta(days=1)

It it completed and created my sensors, 0 amazon emails found. If I set it back to 3 days, I get the same error as before, it finds 5 amazon emails.

and you have 5 emails from amazon in the past 3 days right?

Yes I had 5 emails, but they were not all order notifications, only 1 was. I change the code to look for this specific email from, [email protected]:

def get_items(account, param):
    _LOGGER.debug("Attempting to find Amazon email with item list ...")
    # Limit to past 3 days (plan to make this configurable)
    past_date = datetime.date.today() - datetime.timedelta(days=3)
    tfmt = past_date.strftime('%d-%b-%Y')
    deliveriesToday = []
    orderNum = []

    try:
        (rv, sdata) = account.search(None, '(FROM "[email protected]" SINCE ' + tfmt +
                                     ')')

It finds the 1 and all is working. It was finding emails from things like this one Amazon.com [email protected] which was an ad for pill pack.

Could you please post a bug report on github? Thanks.

1 Like

So I cannot get this component to work anymore. A few days ago I lost all of my apps and settings within HACS so I had to reinstall all of them to get them to show again. When I reinstalled this component I forgot to include beta versions in the settings again so the non-beta version installed over the beta and all the sensors changed to unavailable. Then I decided to remove it completely from HACS and the integrations page so I can start fresh. Now after going through the initial configuration in integrations nothing shows up at all, just says “This integration has no devices.” I’ve tried over 10 times to update the config and rebooted a few times as well and still nothing. This is the error that shows up in the logs:

Logger: homeassistant.components.sensor
Source: custom_components/mail_and_packages/sensor.py:622
Integration: Sensor ([documentation](https://www.home-assistant.io/integrations/sensor), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+sensor%22))
First occurred: 9:26:31 AM (2 occurrences)
Last logged: 11:07:07 AM

Error while setting up mail_and_packages platform for sensor

Traceback (most recent call last): 
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT) 
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for 
    return fut.result() 
  File "/config/custom_components/mail_and_packages/sensor.py", line 146, in async_setup_entry 
      sensors.append(PackagesSensor(data, variable)) 
  File "/config/custom_components/mail_and_packages/sensor.py", line 261, in __init__ 
    self.update() 
  File "/config/custom_components/mail_and_packages/sensor.py", line 309, in update 
    self.data.update() 
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 240, in wrapper 
    result = method(*args, **kwargs) 
  File "/config/custom_components/mail_and_packages/sensor.py", line 201, in update 
    count[sensor] = get_items(account, "count") 
  File "/config/custom_components/mail_and_packages/sensor.py", line 622, in get_items 
    email_msg = str(msg.get_payload(0)) 
  File "/usr/local/lib/python3.7/email/message.py", line 257, in get_payload 
    raise TypeError('Expected list, got %s' % type(self._payload)) 
TypeError: Expected list, got <class 'str'>

There is a bug report for this already.

It seems to steam from Amazon emails. You could try to delete amazon emails that are not order related and that should get it working again. We’re working on a fix. Maybe even a toggle for Amazon.

1 Like

Just to add to this, in your amazon account settings you can turn off all emails except order related ones. I did it to simple clean up my inbox.

1 Like

Latest beta should fix this up now.

I am getting this error. Any help?

2020-05-03 21:37:54 WARNING (SyncWorker_11) [homeassistant.components.local_file.camera] Could not read camera mail_usps image from file: /config/images/mail_and_packages/mail_none.gif

Note: I have no clue what I’m doing, so this could be something absolutely moronic.

mail_none.gif is the incorrect file. The integration creates a single file named mail_today.gif, unless you are using a recent beta and have the random file name turned on. Please check for the file in the path you provided the integration and update your camera component settings.

1 Like

Change the path to: /config/custom_component/mail_and_packages/mail_none.gif

This automation should update the path as needed (adjust paths to your install):

- alias: 'Update USPS Mail Camera'
  trigger:
    - platform: state
      entity_id: sensor.mail_usps_mail

  action:
    - service: local_file.update_file_path
      data_template:
        entity_id: camera.mail_usps
        file_path: >
          {% if state_attr('sensor.mail_usps_mail','image') == None  %}
          /home/homeassistant/.homeassistant/custom_components/mail_and_packages/mail_none.gif
          {% else %}
          /home/homeassistant/.homeassistant/www/mail_and_packages/{{ state_attr('sensor.mail_usps_mail','image') }}
          {% endif %}
1 Like

This appears to be working now. I will know for sure when I actually get some mail. Thanks for your help.

1 Like