Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ omit =
homeassistant/components/sensor/plex.py
homeassistant/components/sensor/pocketcasts.py
homeassistant/components/sensor/pollen.py
homeassistant/components/sensor/postnl.py
homeassistant/components/sensor/pushbullet.py
homeassistant/components/sensor/pvoutput.py
homeassistant/components/sensor/pyload.py
Expand Down
109 changes: 109 additions & 0 deletions homeassistant/components/sensor/postnl.py
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

Copy link
Copy Markdown
Member

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.


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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

@iMicknl iMicknl May 1, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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..
Changed it to 30 minutes again.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Comment thread
iMicknl marked this conversation as resolved.
Outdated
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,9 @@ regenmaschine==0.4.1
# homeassistant.components.python_script
restrictedpython==4.0b2

# homeassistant.components.sensor.postnl
postnl_api==1.0.1

# homeassistant.components.rflink
rflink==0.0.34

Expand Down