Mallonbacka / custom-component-digitransit

HSL, Waltti and other public transit data in Finland in Home Assistant using the Digitransit API
MIT License
1 stars 0 forks source link

Station with multiple stops #25

Closed quenthal closed 4 months ago

quenthal commented 4 months ago

Checklist

Is your feature request related to a problem? Please describe.

As stated, current limitation is that stations are not currently supported, only stops. This means you can't monitor all departures from a station with multiple stops or platforms.

However, is there way to monitor single track from train station? For example many small train stations have only two platforms, A and B, track 1 and 2, or track 3 and 4. Can these be somehow appended to stop code?

Describe the solution you'd like

While waiting for release, possibility to monitor single stops/platforms from train stations.

Describe alternatives you've considered

-

Additional context

-

Mallonbacka commented 4 months ago

It's possible to monitor a single track - they have stop codes, just the same as bus and tram stops do.

For example, if I search for 'Pasila' on the HSL homepage, I see a list which includes all of these individual stops. Here, the stop codes are visible, for example, track 2 has the stop code 'H0086'. It can be added using this integration, just like any other stop.

Screenshot of a list of train tracks, each with

You can also find them by zooming right in on the map and clicking a specific track.

I'll close this issue now, but feel free to comment if something is still uncler.

quenthal commented 4 months ago

Oh, I see. This probably out of the scope of this integration in its current version, but I'm having trouble with for example Tapanila station: image

Both tracks (raide 3 and 4) are H0076 and I can't figure out where to find individual stops for those tracks like in Pasila station.

However, even with this current form I'm very happy - trains usually go every 10-15 minutes so they are not that high priority. So already having the nearby bus stops is great to have Home Assistant in this clear and neat way of your integration!

Mallonbacka commented 4 months ago

Aha, I wasn't aware that any stations contain stops with the same stop code, that's annoying. I'll dig into this a little more and see if there is a quick workaround, if not, I'll try and make some progress on the station support soon.

Mallonbacka commented 4 months ago

Hello @quenthal , I've released a new version (v0.2.0) which allows you to add a stop either by stop code (the current way) or with the GTFS ID. Finding the GTFS ID is a bit more involved, but as far as I can tell, these should always be unique.

For Tapanila station, track 3 has the GTFS ID HSL:1392501 and track 4 is HSL:1392551. I hope this helps!

quenthal commented 4 months ago

Thanks! Using GTFS ID allows these to be added, however sensor stays without value, and I notice also these in the log:

(I think the first log entry, custom_components/digitransit/utils.py, might point to something else because of the timestamp and only 1 occurance).

Lokikirjaaja: homeassistant Lähde: custom_components/digitransit/utils.py:15 integraatio: digitransit (dokumentaatio, ongelmat) Ensimmäistä kertaa ilmennyt: 05.02.26 (1 tapahtumat) Viimeksi kirjattu: 05.02.26

Error doing job: Task exception was never retrieved Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1529, in _async_process_registry_update_or_remove self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1009, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1132, in _async_write_ha_state state, attr, capabilities, shadowed_attr = self.async_calculate_state() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1015, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/sensor/init.py", line 543, in state value = self.native_value ^^^^^^^^^^^^^^^^^ File "/config/custom_components/digitransit/sensor.py", line 47, in native_value return departureToNumberOfMinutes(nextDeparture) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/digitransit/utils.py", line 15, in departureToNumberOfMinutes departsAt = datetime.fromtimestamp(row['serviceDay'] + row['realtimeDeparture'])


KeyError: 'serviceDay'

and

Lokikirjaaja: homeassistant
Lähde: custom_components/digitransit/sensor.py:46
integraatio: digitransit ([dokumentaatio](https://github.com/Mallonbacka/custom-component-digitransit), [ongelmat](https://github.com/Mallonbacka/custom-component-digitransit/issues))
Ensimmäistä kertaa ilmennyt: 04.23.24 (88 tapahtumat)
Viimeksi kirjattu: 05.07.15

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 258, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 414, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 492, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1009, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1132, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1015, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 543, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/digitransit/sensor.py", line 46, in native_value
    nextDeparture = self.coordinator.data.get("data").get("stop").get("stoptimesWithoutPatterns")[0]
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

and

Lokikirjaaja: homeassistant.components.sensor
Lähde: helpers/entity_platform.py:580
integraatio: Sensori ([dokumentaatio](https://www.home-assistant.io/integrations/sensor), [ongelmat](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+sensor%22))
Ensimmäistä kertaa ilmennyt: 04.22.24 (3 tapahtumat)
Viimeksi kirjattu: 04.24.12

Error adding entity sensor.tapanila_h0076_next_departure for domain sensor with platform digitransit
Error adding entity sensor.tapanila_h0076_next_departure_2 for domain sensor with platform digitransit
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 580, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 892, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1359, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1009, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1132, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1015, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 543, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/digitransit/sensor.py", line 46, in native_value
    nextDeparture = self.coordinator.data.get("data").get("stop").get("stoptimesWithoutPatterns")[0]
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

There might be some other issues in my installation, need to fiddle with it.

EDIT: tried with random bus stop, i.e. stop where there is only single  point of departure using GTFS ID and it works, no error in log and sensor value (next departure) is seen. With stops having multiple points (but naturally individual GTFS IDs) I get that error in log and sensor value not available nor provided by the integration.
Mallonbacka commented 4 months ago

Thanks for the information. I'm struggling to get any information from the API about train departures, so this looks a bit more complex than I was imagining. It returns a valid Stop, but an empty list for stoptimesWithoutPatterns.

While I hadn't prepared for it yet, an empty stoptimesWithoutPatterns should set the sensor to Unknown, instead of causing a fatal error in the integration, so I've fixed that with a new version (v0.2.1). There are no new features, only error handling, but it should avoid logging these errors if the response is valid, but there are no departures.

I'll make notes about how to get the upcoming train departures in issue #33, but I'm currently not able to estimate how much work this will require, as I didn't find the root cause yet.