mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
parent
f65cc68705
commit
e479324db9
@ -15,31 +15,28 @@ from homeassistant.const import (CONF_NAME, CONF_USERNAME, CONF_PASSWORD,
|
|||||||
ATTR_ATTRIBUTION)
|
ATTR_ATTRIBUTION)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
from homeassistant.util import Throttle
|
|
||||||
from homeassistant.util.dt import now, parse_datetime
|
from homeassistant.util.dt import now, parse_datetime
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['myusps==1.0.5']
|
REQUIREMENTS = ['myusps==1.1.1']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DOMAIN = 'usps'
|
||||||
|
SCAN_INTERVAL = timedelta(minutes=30)
|
||||||
COOKIE = 'usps_cookies.pickle'
|
COOKIE = 'usps_cookies.pickle'
|
||||||
CONF_UPDATE_INTERVAL = 'update_interval'
|
|
||||||
ICON = 'mdi:package-variant-closed'
|
|
||||||
STATUS_DELIVERED = 'delivered'
|
STATUS_DELIVERED = 'delivered'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_USERNAME): cv.string,
|
vol.Required(CONF_USERNAME): cv.string,
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
vol.Optional(CONF_NAME): 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)),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the USPS platform."""
|
"""Setup the USPS platform."""
|
||||||
import myusps
|
import myusps
|
||||||
try:
|
try:
|
||||||
cookie = hass.config.path(COOKIE)
|
cookie = hass.config.path(COOKIE)
|
||||||
@ -47,38 +44,34 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD),
|
config.get(CONF_USERNAME), config.get(CONF_PASSWORD),
|
||||||
cookie_path=cookie)
|
cookie_path=cookie)
|
||||||
except myusps.USPSError:
|
except myusps.USPSError:
|
||||||
_LOGGER.exception("Could not connect to My USPS")
|
_LOGGER.exception('Could not connect to My USPS')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
add_devices([USPSSensor(session, config.get(CONF_NAME),
|
add_devices([USPSPackageSensor(session, config.get(CONF_NAME)),
|
||||||
config.get(CONF_UPDATE_INTERVAL))])
|
USPSMailSensor(session, config.get(CONF_NAME))], True)
|
||||||
|
|
||||||
|
|
||||||
class USPSSensor(Entity):
|
class USPSPackageSensor(Entity):
|
||||||
"""USPS Sensor."""
|
"""USPS Package Sensor."""
|
||||||
|
|
||||||
def __init__(self, session, name, interval):
|
def __init__(self, session, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
import myusps
|
|
||||||
self._session = session
|
self._session = session
|
||||||
self._name = name
|
self._name = name
|
||||||
self._profile = myusps.get_profile(session)
|
|
||||||
self._attributes = None
|
self._attributes = None
|
||||||
self._state = None
|
self._state = None
|
||||||
self.update = Throttle(interval)(self._update)
|
|
||||||
self.update()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return self._name or self._profile.get('address')
|
return '{} packages'.format(self._name or DOMAIN)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
def _update(self):
|
def update(self):
|
||||||
"""Update device state."""
|
"""Update device state."""
|
||||||
import myusps
|
import myusps
|
||||||
status_counts = defaultdict(int)
|
status_counts = defaultdict(int)
|
||||||
@ -102,4 +95,43 @@ class USPSSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Icon to use in the frontend."""
|
"""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'
|
||||||
|
@ -382,7 +382,7 @@ miflora==0.1.16
|
|||||||
mutagen==1.37.0
|
mutagen==1.37.0
|
||||||
|
|
||||||
# homeassistant.components.sensor.usps
|
# homeassistant.components.sensor.usps
|
||||||
myusps==1.0.5
|
myusps==1.1.1
|
||||||
|
|
||||||
# homeassistant.components.discovery
|
# homeassistant.components.discovery
|
||||||
netdisco==1.0.0
|
netdisco==1.0.0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user