integration icon indicating copy to clipboard operation
integration copied to clipboard

New SL APIs

Open systemcrash opened this issue 1 year ago • 8 comments

Worth mentioning that the trafiklab has this announcement, and they're sunsetting older APIs end 2024.03.30

https://www.trafiklab.se/api/trafiklab-apis/sl/

https://www.trafiklab.se/api/trafiklab-apis/sl/transport/

systemcrash avatar Mar 06 '24 19:03 systemcrash

I don't mean to be rude and drop shadow on awesome work that @DSorlov did here, but I can't pass by without mentioning that I started working on the new SL API integration.

Here is the library that I made to expose both new APIs - Deviations and Transpoirt https://github.com/NecroKote/trafiklab-sl (#67 might be also related)

I plan to make a PR here in the coming weeks that's going to use it

NecroKote avatar Mar 09 '24 18:03 NecroKote

Any assistance would be awesome as my time right now is very limited! <3

DSorlov avatar Mar 09 '24 18:03 DSorlov

Good news all round. I'd prefer it to keep working after next Friday, so things need to happen here before then. 😅

systemcrash avatar Mar 09 '24 18:03 systemcrash

... and here is the rewritten card that should be easier to develop and maintain in the future (only compatible with #71's content)

NecroKote avatar Mar 18 '24 18:03 NecroKote

I have comitted to dev, making sure i can get it working and then will release

DSorlov avatar Mar 18 '24 18:03 DSorlov

It still requires some work thou. But it's a start. My biggest cocern is lack of migrations from older schemas - people would have to reconfigure from scratch. And if so, we at least need to provide a cleanup during migration. Oh, and I haven't yet touched UI for Deviations (Trafik Status) With new API is't not clear to me how to present all this

NecroKote avatar Mar 18 '24 20:03 NecroKote

Do we get a new release soon? :)

systemcrash avatar Apr 25 '24 22:04 systemcrash

I am looking for new maintainers as my time simply is not sufficient to maintain this project any more. Please reach out to me.

DSorlov avatar Jun 02 '24 12:06 DSorlov

@DSorlov I am interested in helping if you have the bandwith to get me up to speed

nemanjab17 avatar Nov 01 '24 11:11 nemanjab17

@DSorlov and @nemanjab17 I'm considering spending a bit of time on this as well. It's a fun little thing. One could also use or take inspiration from NetoKote/trafiklab-sl

Edit: Seems like @NecroKote has a good, working fork of this, ofc using his own library. Feeling a bit stupid.

frli4797 avatar Nov 28 '24 11:11 frli4797

@NecroKote, @nemanjab17 and @frli4797.. I see a brilliant future here.. are you up for adding you as maintainers? and also @NecroKote your code would be very welcome! <3

DSorlov avatar Dec 02 '24 07:12 DSorlov

I would be happy to donate my code to the hasl org.

NecroKote avatar Dec 02 '24 08:12 NecroKote

I have added you to all the required repos! Have fun! You are free to let it rip! =) <3

DSorlov avatar Dec 02 '24 14:12 DSorlov

@NecroKote I hate to the guy bumping people, but I still wanted to ask you if you could get a PR going for merging your changes/improvements to this repo?

frli4797 avatar Dec 11 '24 16:12 frli4797

At this point, could I be added, I am already a maintainer for other home assistant addons.

BoKKeR avatar Dec 11 '24 17:12 BoKKeR

I hate to the guy bumping people, but I still wanted to ask you if you could get a PR going for merging your changes/improvements to this repo?

"Soon" (tm)

But seriously, I'm trying to find time to dust off the changes, considering all the changes to HASS codebase. Not to mention, the new "deviations" do not have a card yet, and that is something I'd like to address before releasing.

Also, I expect rolling the changes out to be the trickiest part since my version is incompatible with the existing configuration or cards.

In the meantime, you can help by testing the fork (and the accompanying card) and providing feedback.

At this point, could I be added, I am already a maintainer for other home assistant addons.

We could use all the help we can get here 🙏

NecroKote avatar Dec 11 '24 18:12 NecroKote

I'm currently happily running your fork, so a sample of one is under way. Curious to what the right strategy here is? To me there are two main themes:

  1. Merge the fork after some additional testing, thus sacrificing some features represented in the older versions, that won't be supported under the new version. (Which is not a huge thing as very little of the original add-on is currently working.)
  2. Develop the new card and sensor (fork) in parallel to the existing version.

My very humble opinion favors alternative one here, as this would be directionally correct. Not perfect, but HASL users would get to a working version, even though with less functionality and most likely with some issues initially.

frli4797 avatar Dec 11 '24 19:12 frli4797

@NecroKote are you willing to open up issues or discussions on your fork or do you want to keep the info located in this discussion?

After updating HomeAssistant today, and running your fork for about a week or so, all of the departure cards and integrations stopped working with a statement that the HASL3 integration was no longer providing the data. Unfortunately, I don't have logs of this.

My assumption here, is that it is due to either migration from HASL3 to your fork or some other reason, that I don't know.

So to try and resolve that:

  • I have removed all of the previous HASL integrations, cleared out everything that I could with the existing configuration
  • dropped in your fork
  • Tried to reconfigure for my locations
  • Now I receive errors and no entities or devices after configuring

FireShot Capture 071 - Settings – Home Assistant - homeassistant doering network As an example of what I am configuring it to look like

Then I received this error:

2024-12-15 12:43:38.892 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry SL Pendeltag - Towards South for hasl3
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 855, in async_unload
    result = await component.async_unload_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/hasl3/__init__.py", line 310, in async_unload_entry
    hass.data[DOMAIN].pop(entry.entry_id)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: '01JEF9QD4A1DDAFRA830YXTRE2'
2024-12-15 12:43:47.658 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry SL Traffic - Departures - Bus 134 for hasl3
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 855, in async_unload
    result = await component.async_unload_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/hasl3/__init__.py", line 310, in async_unload_entry
    hass.data[DOMAIN].pop(entry.entry_id)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: '01JEF2Y79EER3GP0M8JSXY4QQ8'

When attempting to add on a fresh installation, there are no devices or entities that appear after a complete wipe of everything. Screenshot 2024-12-15 at 1 58 32 PM

2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Entered
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Processing RR Departure Sensor 9531 (SL Pendeltag - Towards Central)
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] Entered
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] No sensor specified, will return default
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.worker] [get_minutes_diff] diff 330.0, d1 2024-12-15 14:20:53, d2 2024-12-15 14:15:23
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Entered
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Processing key $APIKEY
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Processing stop 9531
2024-12-15 14:20:53.306 DEBUG (MainThread) [custom_components.hasl3.rrapi] Will call RRDB API
2024-12-15 14:20:53.430 ERROR (MainThread) [custom_components.hasl3.rrapi] RRAPI_API_Error API_AUTH: access denied for $APIKEY on departureBoard identified by stationBoard
2024-12-15 14:20:53.430 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Error occurred during update 9531
2024-12-15 14:20:53.430 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Completed stop 9531
2024-12-15 14:20:53.431 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Completed key $APIKEY
2024-12-15 14:20:53.431 DEBUG (MainThread) [custom_components.hasl3.worker] [process_rrd] Completed
2024-12-15 14:20:53.431 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Update processed
2024-12-15 14:20:53.431 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Completed
2024-12-15 14:20:53.431 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] Entered
2024-12-15 14:20:53.431 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] No sensor specified, will return default
2024-12-15 14:21:12.877 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up hasl3 platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 366, in _async_setup_platform
    await asyncio.shield(awaitable)
  File "/config/custom_components/hasl3/sensor.py", line 76, in async_setup_entry
    await coro(hass, entry, async_add_entities)
  File "/config/custom_components/hasl3/sensors/departure.py", line 122, in async_setup_entry
    coordinator = DepartureDataUpdateCoordinator(
        hass, websession, key, interval, sensor_id
    )
  File "/config/custom_components/hasl3/sensors/departure.py", line 157, in __init__
    self.client = TransportClient()
                  ~~~~~~~~~~~~~~~^^
TypeError: AsyncClient.__init__() missing 1 required positional argument: 'session'
2024-12-15 14:21:23.306 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Entered
2024-12-15 14:21:23.306 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Processing RR Departure Sensor 9531 (SL Pendeltag - Towards Central)
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] Entered
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] No sensor specified, will return default
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.worker] [get_minutes_diff] diff 30.0, d1 2024-12-15 14:21:23, d2 2024-12-15 14:20:53
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Not due for update, skipping
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.sensors] [async_update] Completed
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] Entered
2024-12-15 14:21:23.307 DEBUG (MainThread) [custom_components.hasl3.worker] [check_sensor_state] No sensor specified, will return default

And a different error when using the logger component:

2024-12-15 15:18:30.025 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up hasl3 platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 366, in _async_setup_platform
    await asyncio.shield(awaitable)
  File "/config/custom_components/hasl3/sensor.py", line 76, in async_setup_entry
    await coro(hass, entry, async_add_entities)
  File "/config/custom_components/hasl3/sensors/departure.py", line 122, in async_setup_entry
    coordinator = DepartureDataUpdateCoordinator(
        hass, websession, key, interval, sensor_id
    )
  File "/config/custom_components/hasl3/sensors/departure.py", line 157, in __init__
    self.client = TransportClient()
                  ~~~~~~~~~~~~~~~^^
TypeError: AsyncClient.__init__() missing 1 required positional argument: 'session'

Seems like it is related to the API key, but, not sure if it is a client or API key related problem. Looking in to Trafiklab's site, my API key is still valid and hasn't hit any limitations on their site.

delize avatar Dec 15 '24 12:12 delize

@delize, let's keep discussions in this repo. The problem you are experiencing is due to breaking API changes in the underlying library trafiklab-sl (v1.0.0) and a very permissive dependency string in my fork (>=0.2.0). I'm sorry for the inconvenience. I'm trying to prepare a proper release. Here

In the meantime, you can manually install the new version from this repo's feature/new-sl-api branch.

NecroKote avatar Dec 15 '24 14:12 NecroKote

I use HA through Docker, so my only understanding of how to do that would be via HACS.

It's all good, just took it as a surprise, and in the long run, it helps get rid of anyone else having this issue. :)

When trying this repo's branch, I can't due to the error that the integration is already available (due to it being in the HACS store by default).

Screenshot 2024-12-15 at 3 53 22 PM

If I add your's: https://github.com/NecroKote/HA-sl-integration/tree/feature/new-sl-api

I still see similar behavior of no entities/devices, and the same error in the log:

2024-12-15 16:04:03.187 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up hasl3 platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 366, in _async_setup_platform
    await asyncio.shield(awaitable)
  File "/config/custom_components/hasl3/sensor.py", line 76, in async_setup_entry
    await coro(hass, entry, async_add_entities)
  File "/config/custom_components/hasl3/sensors/departure.py", line 122, in async_setup_entry
    coordinator = DepartureDataUpdateCoordinator(
        hass, websession, key, interval, sensor_id
    )
  File "/config/custom_components/hasl3/sensors/departure.py", line 157, in __init__
    self.client = TransportClient()
                  ~~~~~~~~~~~~~~~^^
TypeError: AsyncClient.__init__() missing 1 required positional argument: 'session'

delize avatar Dec 15 '24 14:12 delize

@delize you can't really use both this version and my fork at the same time, since they both provide same custom component - hasl3. And I'm not really sure if HACS allows installing different branches if there is already a release version system in place

Anyways, I've just packaged a new pre-release version v3.2.0b0 that anyone can install via HACS.

It WILL NOT pop-up as a regular upgrade since I marked it with pre-release, so you need to select this specific version when installing.

(For anyone feeling adventurous, you can even enable the "Pre-release" switch in HASS, allowing these pre-releases to be treated as regular updates)

@frli4797 if you can help with trying this one here I would greatly appreciate it 🙏

If it works - I can finally kill my fork and continue working on v3.2.0 in this repo

NecroKote avatar Dec 15 '24 16:12 NecroKote

👍 All good, everything seems to be working again after installing the pre-release and reconfiguring my stops.

delize avatar Dec 15 '24 16:12 delize

I'm a beginner using HA. Trying to setup HASL. Where can I find the API key that is needed? According to the information at Trafiklab the API key is no longer needed to use the SL API.

danwie avatar Dec 16 '24 10:12 danwie

I'm a beginner using HA. Trying to setup HASL. Where can I find the API key that is needed? According to the information at Trafiklab the API key is no longer needed to use the SL API.

Hey @danwie, you don't need any keys to use the SL part of this integration. But if you want to have a stop search functionality, you would need a "SL Platsuppslag" key that you can get for free on Trafiklab

NecroKote avatar Dec 16 '24 11:12 NecroKote

Hey @danwie, you don't need any keys to use the SL part of this integration. But if you want to have a stop search functionality, you would need a "SL Platsuppslag" key that you can get for free on Trafiklab

Thanks. But "SL Platsuppslag" is not available when selecting key.

SCR-20241216-levg

danwie avatar Dec 16 '24 11:12 danwie

Try using the very last key in the list - Trafikverket öppet API.

Disregard the advice above. It's strange, since new API explicitly says that no API key is required while still requiring a "key" during qury. I'll take a look at this

Never mind, the new "Trafikverket öppet API" is the correct key for stop lookup api.

NecroKote avatar Dec 16 '24 11:12 NecroKote

Another, question. I've installed both cards using HACS. But they don't show in the list of cards when I try to add them to the dashboard. Do I need to manually write the YAML?

SCR-20241216-lmzv

danwie avatar Dec 16 '24 11:12 danwie

@danwie Can we please move this to another discussion ? This thread already became quite a mess and I would like to prevent it from getting even more out of hand.

NecroKote avatar Dec 16 '24 12:12 NecroKote

@danwie Can we please move this to another discussion ? This thread already became quite a mess and I would like to prevent it from getting even more out of hand.

Sorry. And it was my fault - I found the problem. I didn't use the new card that you created.

danwie avatar Dec 16 '24 15:12 danwie

Closing this one since new SL API are finally making it's way into pre-release version starting 3.2.0b0

NecroKote avatar Dec 17 '24 11:12 NecroKote