8000 update usps by happyleavesaoc · Pull Request #7655 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

update usps #7655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 53 additions & 21 deletions homeassistant/components/sensor/usps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,63 @@
ATTR_ATTRIBUTION)
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
from homeassistant.util import Throttle
from homeassistant.util.dt import now, parse_datetime
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['myusps==1.0.5']
REQUIREMENTS = ['myusps==1.1.1']

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'usps'
SCAN_INTERVAL = timedelta(minutes=30)
COOKIE = 'usps_cookies.pickle'
CONF_UPDATE_INTERVAL = 'update_interval'
ICON = 'mdi:package-variant-closed'
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,
vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=1800)): (
vol.All(cv.time_period, cv.positive_timedelta)),
vol.Optional(CONF_NAME): cv.string
})


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the USPS platform."""
"""Setup the USPS platform."""
import myusps
try:
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")
_LOGGER.exception('Could not connect to My USPS')
return False

add_devices([USPSSensor(session, config.get(CONF_NAME),
config.get(CONF_UPDATE_INTERVAL))])
add_devices([USPSPackageSensor(session, config.get(CONF_NAME)),
USPSMailSensor(session, config.get(CONF_NAME))], True)


class USPSSensor(Entity):
"""USPS Sensor."""
class USPSPackageSensor(Entity):
"""USPS Package Sensor."""

def __init__(self, session, name, interval):
def __init__(self, session, name):
"""Initialize the sensor."""
import myusps
self._session = session
self._name = name
self._profile = myusps.get_profile(session)
self._attributes = None
self._state = None
self.update = Throttle(interval)(self._update)
self.update()

@property
def name(self):
"""Return the name of the sensor."""
return self._name or self._profile.get('address')
return '{} packages'.format(self._name or DOMAIN)

@property
def state(self):
"""Return the state of the sensor."""
return self._state

def _update(self):
def update(self):
"""Update device state."""
import myusps
status_counts = defaultdict(int)
Expand All @@ -102,4 +95,43 @@ def device_state_attributes(self):
@property
def icon(self):
"""Icon to use in the frontend."""
return ICON
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'
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ miflora==0.1.16
mutagen==1.37.0

# homeassistant.components.sensor.usps
myusps==1.0.5
myusps==1.1.1

# homeassistant.components.discovery
netdisco==1.0.0
Expand Down
0