2017-08-18 21:47:36 +00:00
|
|
|
"""
|
|
|
|
Support for USPS packages and mail.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/usps/
|
|
|
|
"""
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME, CONF_USERNAME, CONF_PASSWORD)
|
|
|
|
from homeassistant.helpers import (config_validation as cv, discovery)
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
from homeassistant.util.dt import now
|
|
|
|
|
2018-02-18 05:49:32 +00:00
|
|
|
REQUIREMENTS = ['myusps==1.3.2']
|
2017-08-18 21:47:36 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'usps'
|
|
|
|
DATA_USPS = 'data_usps'
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30)
|
|
|
|
COOKIE = 'usps_cookies.pickle'
|
2017-09-24 06:28:11 +00:00
|
|
|
CACHE = 'usps_cache'
|
2018-02-18 05:49:32 +00:00
|
|
|
CONF_DRIVER = 'driver'
|
2017-08-18 21:47:36 +00:00
|
|
|
|
|
|
|
USPS_TYPE = ['sensor', 'camera']
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DOMAIN): cv.string,
|
2018-02-18 05:49:32 +00:00
|
|
|
vol.Optional(CONF_DRIVER): cv.string
|
2017-08-18 21:47:36 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Use config values to set up a function enabling status retrieval."""
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
username = conf.get(CONF_USERNAME)
|
|
|
|
password = conf.get(CONF_PASSWORD)
|
|
|
|
name = conf.get(CONF_NAME)
|
2018-02-18 05:49:32 +00:00
|
|
|
driver = conf.get(CONF_DRIVER)
|
2017-08-18 21:47:36 +00:00
|
|
|
|
|
|
|
import myusps
|
|
|
|
try:
|
|
|
|
cookie = hass.config.path(COOKIE)
|
2017-09-24 06:28:11 +00:00
|
|
|
cache = hass.config.path(CACHE)
|
|
|
|
session = myusps.get_session(username, password,
|
2018-02-18 05:49:32 +00:00
|
|
|
cookie_path=cookie, cache_path=cache,
|
|
|
|
driver=driver)
|
2017-08-18 21:47:36 +00:00
|
|
|
except myusps.USPSError:
|
|
|
|
_LOGGER.exception('Could not connect to My USPS')
|
|
|
|
return False
|
|
|
|
|
|
|
|
hass.data[DATA_USPS] = USPSData(session, name)
|
|
|
|
|
|
|
|
for component in USPS_TYPE:
|
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class USPSData(object):
|
|
|
|
"""Stores the data retrieved from USPS.
|
|
|
|
|
|
|
|
For each entity to use, acts as the single point responsible for fetching
|
|
|
|
updates from the server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, session, name):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Initialize the data object."""
|
2017-08-18 21:47:36 +00:00
|
|
|
self.session = session
|
|
|
|
self.name = name
|
|
|
|
self.packages = []
|
|
|
|
self.mail = []
|
|
|
|
self.attribution = None
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self, **kwargs):
|
|
|
|
"""Fetch the latest info from USPS."""
|
|
|
|
import myusps
|
|
|
|
self.packages = myusps.get_packages(self.session)
|
|
|
|
self.mail = myusps.get_mail(self.session, now().date())
|
|
|
|
self.attribution = myusps.ATTRIBUTION
|
|
|
|
_LOGGER.debug("Mail, request date: %s, list: %s",
|
|
|
|
now().date(), self.mail)
|
|
|
|
_LOGGER.debug("Package list: %s", self.packages)
|