core/homeassistant/components/sensor/usps.py

138 lines
3.8 KiB
Python
Raw Normal View History

"""
Sensor for USPS packages.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.usps/
"""
from collections import defaultdict
import logging
from datetime import timedelta
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, CONF_USERNAME, CONF_PASSWORD,
ATTR_ATTRIBUTION)
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
from homeassistant.util.dt import now, parse_datetime
import homeassistant.helpers.config_validation as cv
2017-06-17 16:42:56 +00:00
REQUIREMENTS = ['myusps==1.1.2']
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'usps'
SCAN_INTERVAL = timedelta(minutes=30)
Fixes #5357 which especify absolute path to save cookie used by USPS sensor. (#5358) Traceback (most recent call last): File "/home/hass/.virtualenvs/home_assistant/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 151, in _async_setup_platform entity_platform.add_entities, discovery_info File "/usr/local/lib/python3.5/asyncio/futures.py", line 361, in __iter__ yield self # This tells Task to wait for completion. File "/usr/local/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup future.result() File "/usr/local/lib/python3.5/asyncio/futures.py", line 274, in result raise self._exception File "/usr/local/lib/python3.5/concurrent/futures/thread.py", line 55, in run result = self.fn(*self.args, **self.kwargs) File "/home/hass/.virtualenvs/home_assistant/lib/python3.5/site-packages/homeassistant/components/sensor/usps.py", line 48, in setup_platform add_devices([USPSSensor(session, config.get(CONF_UPDATE_INTERVAL))]) File "/home/hass/.virtualenvs/home_assistant/lib/python3.5/site-packages/homeassistant/components/sensor/usps.py", line 58, in __init__ self._profile = myusps.get_profile(session) File "/home/hass/.homeassistant/deps/myusps/__init__.py", line 100, in wrapped _login(*args) File "/home/hass/.homeassistant/deps/myusps/__init__.py", line 90, in _login _save_cookies(session.cookies, session.auth.cookie_path) File "/home/hass/.homeassistant/deps/myusps/__init__.py", line 41, in _save_cookies with open(filename, 'wb') as handle: PermissionError: [Errno 13] Permission denied: './usps_cookies.pickle'
2017-01-16 16:36:35 +00:00
COOKIE = 'usps_cookies.pickle'
STATUS_DELIVERED = 'delivered'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_NAME): cv.string
})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the USPS platform."""
import myusps
try:
Fixes #5357 which especify absolute path to save cookie used by USPS sensor. (#5358) Traceback (most recent call last): File "/home/hass/.virtualenvs/home_assistant/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 151, in _async_setup_platform entity_platform.add_entities, discovery_info File "/usr/local/lib/python3.5/asyncio/futures.py", line 361, in __iter__ yield self # This tells Task to wait for completion. File "/usr/local/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup future.result() File "/usr/local/lib/python3.5/asyncio/futures.py", line 274, in result raise self._exception File "/usr/local/lib/python3.5/concurrent/futures/thread.py", line 55, in run result = self.fn(*self.args, **self.kwargs) File "/home/hass/.virtualenvs/home_assistant/lib/python3.5/site-packages/homeassistant/components/sensor/usps.py", line 48, in setup_platform add_devices([USPSSensor(session, config.get(CONF_UPDATE_INTERVAL))]) File "/home/hass/.virtualenvs/home_assistant/lib/python3.5/site-packages/homeassistant/components/sensor/usps.py", line 58, in __init__ self._profile = myusps.get_profile(session) File "/home/hass/.homeassistant/deps/myusps/__init__.py", line 100, in wrapped _login(*args) File "/home/hass/.homeassistant/deps/myusps/__init__.py", line 90, in _login _save_cookies(session.cookies, session.auth.cookie_path) File "/home/hass/.homeassistant/deps/myusps/__init__.py", line 41, in _save_cookies with open(filename, 'wb') as handle: PermissionError: [Errno 13] Permission denied: './usps_cookies.pickle'
2017-01-16 16:36:35 +00:00
cookie = hass.config.path(COOKIE)
session = myusps.get_session(
config.get(CONF_USERNAME), config.get(CONF_PASSWORD),
cookie_path=cookie)
except myusps.USPSError:
_LOGGER.exception('Could not connect to My USPS')
return False
add_devices([USPSPackageSensor(session, config.get(CONF_NAME)),
USPSMailSensor(session, config.get(CONF_NAME))], True)
class USPSPackageSensor(Entity):
"""USPS Package Sensor."""
def __init__(self, session, name):
"""Initialize the sensor."""
self._session = session
self._name = name
self._attributes = None
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return '{} packages'.format(self._name or DOMAIN)
@property
def state(self):
"""Return the state of the sensor."""
return self._state
def update(self):
"""Update device state."""
import myusps
status_counts = defaultdict(int)
for package in myusps.get_packages(self._session):
status = slugify(package['primary_status'])
if status == STATUS_DELIVERED and \
parse_datetime(package['date']).date() < now().date():
continue
status_counts[status] += 1
self._attributes = {
ATTR_ATTRIBUTION: myusps.ATTRIBUTION
}
self._attributes.update(status_counts)
self._state = sum(status_counts.values())
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._attributes
@property
def icon(self):
"""Icon to use in the frontend."""
return 'mdi:package-variant-closed'
class USPSMailSensor(Entity):
"""USPS Mail Sensor."""
def __init__(self, session, name):
"""Initialize the sensor."""
self._session = session
self._name = name
self._attributes = None
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return '{} mail'.format(self._name or DOMAIN)
@property
def state(self):
"""Return the state of the sensor."""
return self._state
def update(self):
"""Update device state."""
import myusps
self._state = len(myusps.get_mail(self._session))
@property
def device_state_attributes(self):
"""Return the state attributes."""
import myusps
return {
ATTR_ATTRIBUTION: myusps.ATTRIBUTION
}
@property
def icon(self):
"""Icon to use in the frontend."""
return 'mdi:mailbox'