Skip to content
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

Add AfterShip sensor for packages #18034

Merged
merged 2 commits into from
Dec 27, 2018

Conversation

maxandersen
Copy link
Contributor

@maxandersen maxandersen commented Oct 30, 2018

Why:

  • I receive a lot of packages from many different shipping companies.
  • I would like to see in haas how many packages are being delivered.

This change addreses the need by:

  • Adding a sensor for AfterShip (aftership.com)
  • AfterShip supports ~490 couriers world wide thus should cover
    almost any sensible tracking.

Notes:

  • For now this sensor assumes you somehow have added trackings to
    aftership manually
  • Future idea is to expose service that allows adding a tracking
    based on incoming mails.
  • Other improvments would be to add map markers for package locations.

Related:

homeassistant/components/sensor/aftership.py Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
@vMeph
Copy link

vMeph commented Nov 2, 2018

doesnt this sensor already exists
https://github.com/custom-cards/aftership-card

@ludeeus
Copy link
Member

ludeeus commented Nov 2, 2018

@vMeph That is a custom card for Lovelace and not a sensor platfrom like this PR.
That card uses another custom_component to fetch the data since (currently) there is no dedicated sensor in Home Assistant for this.

@vMeph
Copy link

vMeph commented Nov 2, 2018

ohh ok cool
ty for explanation

@maxandersen
Copy link
Contributor Author

yes, I learned after publishing this that https://github.com/custom-components/sensor.aftership exists and I think I'll merge its features.

I do though think it would be nice to have a generic all-purpose package tracker (rather than the fairly limited/dedicated ones in there) - hence why suggesting to add it officially.

@maxandersen
Copy link
Contributor Author

one thing i'm wondering - is the best way to expose details about the current incoming packages really to put it as a json dictionary as data on the sensor or would it be better to have these as some kind of temporary tracking entities or similar ?

@maxandersen
Copy link
Contributor Author

fyi - delay in updating the PR is due to that after moving to hassio I'm battling this issue: https://community.home-assistant.io/t/runtimeerror-the-processor-time-used-is-not-available-or-its-value-cannot-be-represented/77659

alternatively I could just use the api directly (as its trivial) and avoid the use of clock() in the aftership sdk - suggestions welcome.

@frenck
Copy link
Member

frenck commented Nov 19, 2018

Could not find a related PR on our documentation repository. Adding docs-missing label.

homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
@maxandersen maxandersen force-pushed the aftership branch 2 times, most recently from b1f742a to d4001eb Compare November 21, 2018 23:09
@maxandersen
Copy link
Contributor Author

Now cleaned this up based on feedback.

I also had to move to using pyaftership rather than aftership's own python sdk as the latter fails when running over a day on a hass.io raspberry (some issue using .clock() calls).

In addition I added a service to call for adding packages - I wanted to add service description but did not find services.yaml for sensors - is that expected ? should I create a new ?

I'll make a corresponding doc tomorrow - time to sleep now ;)

homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
from homeassistant.util import Throttle
from homeassistant.exceptions import PlatformNotReady

REQUIREMENTS = ['pyaftership==0.0.5']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like that the latest release is 0.1.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, new update came out based on this PR - pushed update using latest that is also async.

but just noticed the service is failing for this version so looking into that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the service code to make the initial PR simpler.

@maxandersen maxandersen force-pushed the aftership branch 3 times, most recently from 25cb2fb to f4c1258 Compare November 27, 2018 21:05
homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
_LOGGER.error("Unkown errors when querying AfterShip. See logs for details.")
return
elif self.aftership.meta['code'] != 200:
_LOGGER.error("Errors when querying AfterShip. %s", str(self.aftership.meta))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)

homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
async def async_update(self):
"""Get the latest data from the AfterShip API."""
await self.aftership.get_trackings()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved

if not aftership.meta or aftership.meta['code'] != 200:
_LOGGER.error("No tracking data found. Check AfterShip API key is correct: %s", aftership.meta)
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
aftership = Tracking(hass.loop, session, apikey)

await aftership.get_trackings()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

homeassistant/components/sensor/aftership.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/aftership.py Show resolved Hide resolved
@maxandersen
Copy link
Contributor Author

apparently my githook isn't properly running lint ;/ fix those issues and pushed updated that has async and latest pyaftership usage.

@maxandersen
Copy link
Contributor Author

maxandersen commented Nov 30, 2018

Just noticed the 17track component which does the sensor per package I wanted to do.

I know you prefer small PRS over big ones. Could we commit the current state for this one and then I add the package sensors or you prefer I do it in one go ? Thanks!

@MartinHjelmare
Copy link
Member

This is almost ready for merge. Please don't extend it more in this PR, unless it would be a breaking change later.

@maxandersen
Copy link
Contributor Author

I'm cool with leave as is if that gets it included; then I can add the actual individual package tracking in additional PR's.

Why:

 * I receive a lot of packages from many different shipping companies.
 * I would like to see in haas how many packages are being delivered.

This change addreses the need by:

 * Adding a sensor for AfterShip (aftership.com)
 * AfterShip supports ~490 couriers world wide thus should cover
   almost any sensible tracking.

Notes:
  - For now this sensor assumes you somehow have added trackings to
    aftership manually.
  - Future idea is to expose service that allows adding a tracking
    based on incoming mails.
  - Other improvments would be to add map markers for package locations.

Related:
- https://community.home-assistant.io/t/package-tracking/858
- https://community.home-assistant.io/t/aftership-package-tracking/24068
- https://community.home-assistant.io/t/aftership-shipment-tracking-platform/14074
- https://community.home-assistant.io/t/aftership-state-card/57912
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

For the future, please don't squash commits after review has started to make it easier for readers to track changes. We always squash upon merge via github anyway.

Can be merged when build passes.

@ghost ghost assigned fabaff Dec 27, 2018
@MartinHjelmare
Copy link
Member

Thanks @fabaff!

@MartinHjelmare MartinHjelmare merged commit b32e6fe into home-assistant:dev Dec 27, 2018
@ghost ghost removed the in progress label Dec 27, 2018
@balloob balloob mentioned this pull request Jan 10, 2019
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants