Let me first admit that this is very, very hacky, but it works! This code modification and a corresponding local file camera entity will allow you to see today’s received mail, cycled through as a gif.
UPDATE: @mezz64 has created a more elegant version of this feature published to Github: USPS Mail Camera. I will continue to update the code below when changes are required, but his solution might be a better choice.
-
Install ImageMagick using your package manager of choice.
-
Modify
path_to_homeassistant_directory/deps/myusps/__init__.py
[version 0.50+ has this file atpath_to_homeassistant_directory/deps/lib/python3.?/site-packages/myusps/__init__.py
] and replace theget_mail
function (really just modify the last few lines) then add adownload_images
function:
Modify get_mail
function (you can use http://imgur.com/a/bZc6n for your nomail.gif):
def get_mail(session, date=None):
"""Get mail data."""
_LOGGER.info("attempting to get informed delivery data")
if not date:
date = datetime.datetime.now().date()
mail = []
response = session.post(INFORMED_DELIVERY_URL, {
'selectedDate': '{0:%m}/{0:%d}/{0:%Y}'.format(date)
})
container = _require_elem(response, INFORMED_DELIVERY_TAG, INFORMED_DELIVERY_ATTRS)
for row in container.find_all(MAIL_IMAGE_TAG, MAIL_IMAGE_ATTRS):
img = row.find('img').get('src')
mail.append({
'id': img.split('=')[1],
'date': str(date),
'image': '{}{}'.format(INFORMED_DELIVERY_IMAGE_URL, img)
})
"""Download and compile images if we got mail, otherwise copy nomail image over GIF"""
if len(mail) >= 1:
download_images(session, mail)
else:
os.system("cp /opt/homeassistant/mail/nomail /opt/homeassistant/mail/mail.gif")
return mail
Add download_images
function (modify the paths below to actual paths on your system):
def download_images(session, mail):
os.system("rm /opt/homeassistant/mail/*.jpg")
count = 1
total = len(mail)
for mailpiece in mail:
img_data = session.get(mailpiece['image']).content
"""Download the image"""
with open('/opt/homeassistant/mail/' + mailpiece['id'] + '.jpg', 'wb') as handler:
handler.write(img_data)
"""Annotate image with it's number over the total so we can keep track in the final GIF"""
os.system("convert /opt/homeassistant/mail/" + mailpiece['id'] + ".jpg -fill white -undercolor \
'#00000080' -gravity SouthWest -pointsize 18 -annotate +0+0 '" + str(count) + "/" + \
str(total) + "' /opt/homeassistant/mail/" + str(count) + ".jpg 2>/dev/null")
"""Remove un-annotated image"""
os.system("rm /opt/homeassistant/mail/" + mailpiece['id'] + ".jpg")
count += 1
"""Create GIF of all mail images"""
os.system("convert -delay 300 -loop 0 -dispose previous /opt/homeassistant/mail/*.jpg /opt/homeassistant/mail/mail.gif")
Now add a camera entity that reflects the location of mail.gif:
- platform: local_file
name: Mail
file_path: /opt/homeassistant/mail/mail.gif
That’s it! This could probably be done much more elegantly (stop doing IO in sensor updates!!!) but it works well for me. I’d love to see this integrated into HA somehow but I can’t think of an appropriate way for that to happen, maybe this will inspire someone!