core/homeassistant/components/usps/__init__.py

97 lines
2.8 KiB
Python
Raw Normal View History

"""Support for USPS packages and mail."""
from datetime import timedelta
import logging
import voluptuous as vol
2019-07-31 19:25:30 +00:00
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
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DOMAIN = "usps"
DATA_USPS = "data_usps"
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30)
2019-07-31 19:25:30 +00:00
COOKIE = "usps_cookies.pickle"
CACHE = "usps_cache"
CONF_DRIVER = "driver"
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,
vol.Optional(CONF_DRIVER): cv.string,
}
)
},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
"""Use config values to set up a function enabling status retrieval."""
_LOGGER.warning(
"The usps integration is deprecated and will be removed "
"in Home Assistant 0.100.0. For more information see ADR-0004:"
"https://github.com/home-assistant/architecture/blob/master/adr/0004-webscraping.md"
)
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)
import myusps
2019-07-31 19:25:30 +00:00
try:
cookie = hass.config.path(COOKIE)
2017-09-24 06:28:11 +00:00
cache = hass.config.path(CACHE)
2019-07-31 19:25:30 +00:00
session = myusps.get_session(
username, password, cookie_path=cookie, cache_path=cache, driver=driver
)
except myusps.USPSError:
2019-07-31 19:25:30 +00:00
_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:
"""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):
"""Initialize the data object."""
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
2019-07-31 19:25:30 +00:00
self.packages = myusps.get_packages(self.session)
self.mail = myusps.get_mail(self.session, now().date())
self.attribution = myusps.ATTRIBUTION
2019-07-31 19:25:30 +00:00
_LOGGER.debug("Mail, request date: %s, list: %s", now().date(), self.mail)
_LOGGER.debug("Package list: %s", self.packages)