PostNL Integration

The browser extension is only needed on install of the integration to retrieve the access and refresh token. After that it’s not needed anymore. It’s just oAuth.

If you want to learn about oAuth, then you can have a look here:

Thanks, that’s better. I was thinking with my limited oauth knowledge that you normally provide the redirect_uri as a url parameter in the authentication request?

You need to provide the redirect_uri in the auth request, but that’s only when you are authorizing the client the first time. To retrieve a new access token you need to POST the refresh token (and some more parameters) to the token endpoint.

This wrapper always has been the base for the integration :slight_smile: The author of the wrapper is also the one that implemented it within Home Assistant in the first place,

Alright, but the wrapper you linked is not a Home Assistant Integration. When I’m done, then I can abstract API classes from the integration and maybe create a pull request to the wrapper.

I pushed my (very work in progress) project to Github. You can have a look here: GitHub - arjenbos/ha-postnl: Custom PostNL Home Assistant integration

1 Like

Hi Marius
I did a little googling, and it should be possible to use the e-mails as a trigger using the IMAP integration:

Then it’s a matter of parsing the payload using templates - I guess this can be a struggle but should be achievable.

It would be great if this solution (when it’s made into a proper / HACS integration) can leverage the new 2023.11 to-do list functionality, that sounds like it’s appropriate to use dynamically for a constantly updating list of incoming parcels.

1 Like

@ptnijssen still work in progress, but I’m getting there.

Screenshot 2023-11-23 at 09.16.17

2 Likes

do you think this would also go for regular mail, so letters etc, and not only packages which are delivered via the PostNL expedition.

when logged in into the app, we get a daily notification when mail is on the way. That is what I was looking for.
seems another PostNL service altogether?

I released the first “beta”. It’s more an alpha. So, if people would like to test, then please feel free to do so. Also, pull requests are of course welcome!

I didn’t add support for letters, because the old mobile API is dead. With a man in the middle attack I saw how PostNL retrieves the data for their mobile app. This is (again) another API. It will take some time to figure out what the best approach is.

Some other things I want to do:

  • Less API requests.
    • It’s not needed to fetch all data for already delivered packages.
    • The track and trace details JSON api endpoint returns a lastObservation timestamp. Hopefully I can get the same data from the GraphQL API and determine if a call to the track and trace details endpoint is needed.
  • Reduce the amount of data that GraphQL returns.
  • Restructure the various API classes and use structs for packages.
  • Some code clean up.
  • Add tests.
4 Likes

Hi Arjen,

Thanks for all your efforts in this. I do get the integration linked with my PostNL account however, it does not create any sensors. Here’s some debug logs. Hope this helps.

2023-11-23 22:29:12.578 DEBUG (MainThread) [custom_components.postnl.sensor] Update shipments
2023-11-23 22:29:12.579 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.postnl_delivery_2 fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 696, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 957, in async_device_update
    await self.async_update()
  File "/config/custom_components/postnl/sensor.py", line 98, in async_update
    await self.update_access_token()
  File "/config/custom_components/postnl/sensor.py", line 83, in update_access_token
    implementation = await async_get_config_entry_implementation(self.hass, self.entry)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 405, in async_get_config_entry_implementation
    raise ValueError("Implementation not available")
ValueError: Implementation not available

Thanks for sharing the logs! I created an issue on Github to track the issue.

I changed a couple of things in the integration (Use coordinator by arjenbos · Pull Request #2 · arjenbos/ha-postnl · GitHub). Could you fetch the latest beta version and test again? I would suggest to do a fresh install, just to be sure.

If it doesn’t work, then please message me in a DM how you implemented the integration.

Yes! That did work! Great job. Thanks a lot Arjen :grinning: :facepunch:

1 Like

Works! but the whole weekend I tried end ended with blank screen because i didn’t saw the line that I must download the chrome extension :stuck_out_tongue: hehe

At my installation the integration worked through the whole weekend, but now the sensor is ‘unavailable’. See the logs below (btw: I replaced the actual numbers with 1s)

Deze fout is ontstaan door een aangepaste integratie.

Logger: custom_components.postnl.coordinator
Source: helpers/update_coordinator.py:290
Integration: PostNL (documentation, issues)
First occurred: 26 november 2023 om 18:12:09 (383 occurrences)
Last logged: 11:44:43

Unexpected error fetching PostNL data: '3SBOL1111111111'
Unexpected error fetching PostNL data: '3SBUSD11111111'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 290, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/postnl/coordinator.py", line 48, in _async_update_data
    colli = track_and_trace_details['colli'][shipment['barcode']]
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
KeyError: '3SBOL1111111111'

Tried adding this integration several times but it just doesn’t want to create sensors.

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:359
Integration: Sensor (documentation, issues)
First occurred: 15:18:38 (2 occurrences)
Last logged: 15:25:27

Error while setting up postnl platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 290, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/postnl/coordinator.py", line 47, in _async_update_data
    colli = track_and_trace_details['colli'].get(shipment['barcode'], {})
            ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'colli'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/postnl/sensor.py", line 19, in async_setup_entry
    await coordinator.async_config_entry_first_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 262, in async_config_entry_first_refresh
    raise ex
homeassistant.exceptions.ConfigEntryNotReady: 'colli'

@Kliko could you please update to the latest beta release?

@schumi2004 ah, I see that I made a little mistake. Hopefully I fixed it. Could you please update to the latest beta release?

Both issues are related to responses from the PostNL API. For some reason the track and trace API doesn’t return any data for the track and trace codes.

I am very curious what the PostNL track and trace API returns for those barcodes. Is there a way you can send me privately more details? It’s very hard to debug without the response from the API.

1 Like

Works great! Thanks for the good work!

1 Like

@arjenbos , is this only working with the browser extension? Because I don’t use Chrome (but Opera)… Great you’re working on this!

The browser extension is only needed when you install the integration. You do not need to use it constantly.

I am not experienced with Opera, but the chrome extension uses a common browser API. According to this documentation Opera supports the API. So, creating an Opera extension should be possible.

1 Like