-
-
Notifications
You must be signed in to change notification settings - Fork 38k
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8d719c1
Add basic PostNL sensor (WIP)
iMicknl 3453a0e
Update PostNL sensor
iMicknl 4fd10de
Merge remote-tracking branch 'upstream/dev' into dev
iMicknl f13e709
Bump version
iMicknl 833237e
Small updates to PostNL package based on feedback
iMicknl b2077c1
Remove unused import
iMicknl f0d67e7
Pass api to sensor
iMicknl 642075c
Refactor based on feedback
iMicknl d226104
Update based on feedback
iMicknl 88882ca
Fix feedback
iMicknl 2fa93ed
Clean up
iMicknl 8645ccc
Bugfiix
iMicknl 1478716
Bugfix
iMicknl 409429d
SCAN_INTERVAL fix
iMicknl 85758ac
Remove unused import
iMicknl bce46c2
Refactor for new wrapper implementation
iMicknl d68edb1
Update postnl package requirement
iMicknl a88f9d3
Change throttle logic
iMicknl b9cfd17
Update package version
iMicknl 44f316a
Add new line
iMicknl c6523a6
Minor changes
fabaff 9171873
Change refresh time to 30 minutes
iMicknl 9454e7a
Merge branch 'dev' of github.com:iMicknl/home-assistant into dev
iMicknl f213d09
Update requirements_all.txt
iMicknl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """ | ||
| Sensor for PostNL packages. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.postnl/ | ||
| """ | ||
| from datetime import timedelta | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import ( | ||
| ATTR_ATTRIBUTION, CONF_NAME, CONF_PASSWORD, CONF_USERNAME) | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.util import Throttle | ||
|
|
||
| REQUIREMENTS = ['postnl_api==1.0.1'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| ATTRIBUTION = 'Information provided by PostNL' | ||
|
|
||
| DEFAULT_NAME = 'postnl' | ||
|
|
||
| ICON = 'mdi:package-variant-closed' | ||
|
|
||
| MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) | ||
|
|
||
| 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 sensor platform.""" | ||
| from postnl_api import PostNL_API, UnauthorizedException | ||
|
|
||
| username = config.get(CONF_USERNAME) | ||
| password = config.get(CONF_PASSWORD) | ||
| name = config.get(CONF_NAME) | ||
|
|
||
| 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): | ||
| """Representation of a PostNL sensor.""" | ||
|
|
||
| def __init__(self, api, name): | ||
| """Initialize the PostNL 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)' | ||
|
|
||
| @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 | ||
|
|
||
| @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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.