-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add PostNL sensor (Dutch Postal Services) #12366
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
Changes from 19 commits
8d719c1
3453a0e
4fd10de
f13e709
833237e
b2077c1
f0d67e7
642075c
d226104
88882ca
2fa93ed
8645ccc
1478716
409429d
85758ac
bce46c2
d68edb1
a88f9d3
b9cfd17
44f316a
c6523a6
9171873
9454e7a
f213d09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """ | ||
| Sensor for PostNL packages. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.postnl/ | ||
| """ | ||
| 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 Throttle | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| REQUIREMENTS = ['postnl_api==1.0.1'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| DEFAULT_NAME = 'postnl' | ||
| ICON = 'mdi:package-variant-closed' | ||
| ATTRIBUTION = 'Information provided by PostNL' | ||
| MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be less aggressive polling for new packages. Let's change this to 30 minutes.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. In the beginning this was different, where the default was around 30. But where the user could change it himself. This changed after feedback..
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. USPS (American equivalent) has been IP banning people using the HASS integration… |
||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string | ||
| }) | ||
|
|
||
| # pylint: disable=unused-argument | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Set up the PostNL platform.""" | ||
|
|
||
| username = config.get(CONF_USERNAME) | ||
| password = config.get(CONF_PASSWORD) | ||
| name = config.get(CONF_NAME) | ||
|
|
||
| from postnl_api import PostNL_API, UnauthorizedException | ||
|
|
||
| try: | ||
| api = PostNL_API(username, password) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. local variable 'api' is assigned to but never used |
||
|
|
||
| except UnauthorizedException: | ||
| _LOGGER.exception("Can't connect to the PostNL webservice") | ||
| return | ||
|
|
||
| add_devices([PostNLSensor(api, name)], True) | ||
|
|
||
|
|
||
| class PostNLSensor(Entity): | ||
| """PostNL Sensor.""" | ||
|
|
||
| def __init__(self, api, name): | ||
| """Initialize the sensor.""" | ||
| self._name = name | ||
| self._attributes = None | ||
| self._state = None | ||
| self._api = api | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the sensor.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit of measurement of this entity, if any.""" | ||
| return 'package(s)' | ||
|
|
||
| @Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
| def update(self): | ||
| """Update device state.""" | ||
|
|
||
| shipments = self._api.get_relevant_shipments() | ||
| status_counts = {} | ||
|
|
||
| for shipment in shipments: | ||
| status = shipment['status']['formatted']['short'] | ||
| status = self._api.parse_datetime(status, '%d-%m-%Y', '%H:%M') | ||
|
|
||
| name = shipment['settings']['title'] | ||
| status_counts[name] = status | ||
|
|
||
| self._attributes = { | ||
| ATTR_ATTRIBUTION: ATTRIBUTION, | ||
| **status_counts | ||
| } | ||
|
|
||
| self._state = len(status_counts) | ||
|
|
||
| @property | ||
| def device_state_attributes(self): | ||
| """Return the state attributes.""" | ||
| return self._attributes | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Icon to use in the frontend.""" | ||
| return ICON | ||
|
iMicknl marked this conversation as resolved.
Outdated
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a blank line between standard library and 3rd party imports.